package com.ahau.againstpest.utils;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.ahau.againstpest.R;
import com.ahau.againstpest.activity.AgainPestActivity.Type;

import java.io.File;

/**
 * Created by fanchunchun on 2018/3/28.
 */

public class TakingPhotoUtils extends PopupWindow implements View.OnClickListener{
    public Context mContext;

    private Type type;

    public Activity mActivity;

    private File file;
    private Uri ImgUri;

    private RelativeLayout mTakePhoto, mAlbumPhoto;
    private Button mCancel;

    public TakingPhotoUtils(Context context, Activity mActivity) {
        initView(context);
        this.mActivity = mActivity;
    }

    private void initView(Context mContext) {
        this.mContext = mContext;

        View v = LayoutInflater.from(mContext).inflate(R.layout.personpicchange, null);
        setContentView(v);

        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new BitmapDrawable());
        //因为某些机型是虚拟按键的,所以要加上以下设置防止挡住按键.
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        mTakePhoto = (RelativeLayout) v.findViewById(R.id.photo_take);
        mAlbumPhoto = (RelativeLayout) v.findViewById(R.id.photo_album);
        mCancel = (Button) v.findViewById(R.id.photo_cancel);

        mTakePhoto.setOnClickListener(this);
        mAlbumPhoto.setOnClickListener(this);
        mCancel.setOnClickListener(this);



        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setTouchable(true);
        this.setFocusable(true);
        this.setOutsideTouchable(true);

        // 刷新状态
        this.update();
        // 设置SelectPicPopupWindow弹出窗体动画效果
        // this.setAnimationStyle(R.style.popuwindow_from_bottom);

        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x50000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    public void showPopupWindow(View parent) {

        if (!this.isShowing()) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        } else {
            this.dismiss();

        }
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.photo_take:
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                ContentValues contentValues = new ContentValues(1);
                file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); // 图片名称为当前时间.jpg
                contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
                ImgUri = mContext.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);

                intent.putExtra(MediaStore.EXTRA_OUTPUT, ImgUri);// 将拍摄的照片保存至imguri
                mActivity.startActivityForResult(intent, 1);
                type = Type.CAMERA;
                if (listener != null) {
                    listener.getType(type);
                    listener.getImgUri(ImgUri, file);
                }

                this.dismiss();
                break;
            case R.id.photo_album:
                Intent intent2 = new Intent("android.intent.action.PICK");
                intent2.setType("image/*");// audio/* 选择音频 video/* 选择视频
                // viedo/*;image/*同时选择视频和图片
                mActivity.startActivityForResult(intent2, 2);
                type = Type.PHONE;
                if (listener != null) {
                    listener.getType(type);
                }
                this.dismiss();
                break;
            case R.id.photo_cancel:
                this.dismiss();
                break;
            default:
                break;
        }
    }

    public void onPhoto(int k,Uri uri, int outputX, int outputY) { // 裁剪照片
        Intent intent = null;
        String padress="";
        intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");



        if(k==1)   //判断当前添加的是第几张照片
        {
            padress="/temp1.jpg";
        }else if(k==2){
            padress="/temp2.jpg";
        }
        Uri tempPhotoUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() +""+padress));
        // tempPhotoUri =
        // Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/" +
        // "temp.jpg");

        intent.putExtra(MediaStore.EXTRA_OUTPUT, tempPhotoUri);

        // intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//输出至URI
        // intent.putExtra("return-data", true);//是否将数据保留在BITMAP中返回
//		intent.putExtra("circleCrop", false);// 圆形 裁剪区域
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        // intent.putExtra("outputFormat",
        // Bitmap.CompressFormat.JPEG.toString());
        // intent.putExtra("noFaceDetection", true); // no face detection
        Log.d("tempPhotoUri",tempPhotoUri.toString());

        mActivity.startActivityForResult(intent, 3);
    }

    public interface onGetTypeClckListener {
        void getType(Type type);

        void getImgUri(Uri ImgUri, File file);
    }

    private onGetTypeClckListener listener;

    public void setOnGetTypeClckListener(onGetTypeClckListener listener) {
        this.listener = listener;
    }
}

