package com.zhaohaoting.framework.utils.file;


import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import com.zhaohaoting.framework.abs.AbsV4Fragment;
import com.zhaohaoting.framework.abs.presenter.AbsActivity;

import java.io.File;

/**
 * Created by zhaoting on 2018/11/17.
 * Desc：
 */
public class LocationPictureProvider {

    public static final int PERMISSION_PICK_PHOTO = 601;//sdcard读取权限
    public static final int PERMISSION_CAMERA = 602;//相机权限
    public static final int PICK_PHOTO = 603;//相册
    public static final int TAKE_PHOTO = 604;//拍照
    public static final int PHONE_CROP = 605;//裁剪
    /**
     * 返回数据
     */
    public static final int PHONE_PIC_REQUEST = 606;//
    public static final int PHONE_CROP_REQUEST = 607;//
    private int requestCode;

    private String photoPath;////拍照路径
    private String cropImagePath;//裁剪路径
    private Uri photoUri;
    private AbsActivity activity;
    private AbsV4Fragment fragment;
    private OnPicturePhotoPathListener photoPathListener;

    public LocationPictureProvider(AbsV4Fragment fragment, int requestCode, OnPicturePhotoPathListener photoPathListener) {
        if (fragment == null) {
            throw new RuntimeException("fragment 不能为null");
        }
        this.fragment = fragment;
        this.requestCode = requestCode;
        this.photoPathListener = photoPathListener;
    }

    public LocationPictureProvider(AbsActivity activity, int requestCode, OnPicturePhotoPathListener photoPathListener) {
        if (activity == null) {
            throw new RuntimeException("activity 不能为null");
        }
        this.activity = activity;
        this.requestCode = requestCode;
        this.photoPathListener = photoPathListener;
    }

    public interface OnPicturePhotoPathListener {
        void picturePhotoPath(String photoPath);
    }

    public void toAlbum() {
        if (activity != null) {
            activity.judgePermission(PERMISSION_PICK_PHOTO, Manifest.permission.READ_EXTERNAL_STORAGE);
        }
        if (fragment != null) {
            fragment.judgePermission(PERMISSION_PICK_PHOTO, Manifest.permission.READ_EXTERNAL_STORAGE);
        }
    }

    public void toTakePictures() {
        if (activity != null) {
            activity.judgePermission(PERMISSION_CAMERA, Manifest.permission.CAMERA);
        }
        if (fragment != null) {
            fragment.judgePermission(PERMISSION_CAMERA, Manifest.permission.CAMERA);
        }
    }

    public void requestPermissionSuccess(int requestCode) {
        if (requestCode == PERMISSION_PICK_PHOTO) {
            PhotoUtils.openPic(activity, fragment, PICK_PHOTO);

            File fileCropUri = FileUtils.createImgFile("crop_photo");
            cropImagePath = fileCropUri.getAbsolutePath();

        } else if (requestCode == PERMISSION_CAMERA) {
            File photoFile = FileUtils.createImgFile("photo");
            photoPath = photoFile.getAbsolutePath();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                //通过FileProvider创建一个content类型的Ur
                photoUri = FileProvider.getUriForFile(getContext(), getPackageName() + ".provider", photoFile);
            } else {
                photoUri = Uri.fromFile(photoFile);
            }
            PhotoUtils.takePicture(activity, fragment, photoUri, TAKE_PHOTO);

            File fileCropUri = FileUtils.createImgFile("crop_photo");
            cropImagePath = fileCropUri.getAbsolutePath();
        }
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data, int aspectX, int aspectY, int width, int height) {
        if (resultCode != Activity.RESULT_OK) return;
        switch (requestCode) {
            case PICK_PHOTO:
                photoPath = PhotoUtils.getPath(getContext(), data.getData());
                photoUri = Uri.fromFile(new File(photoPath));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    photoUri = FileProvider.getUriForFile(getContext(), getPackageName() + ".provider", new File(photoPath));
                }
            case TAKE_PHOTO:
                if (this.requestCode == PHONE_PIC_REQUEST) {
                    photoPathListener.picturePhotoPath(photoPath);
                } else {
                    Uri cropImageUri = Uri.fromFile(new File(cropImagePath));
                    PhotoUtils.cropImageUri(activity, fragment, photoUri, cropImageUri, aspectX, aspectY, width, height, PHONE_CROP, false);
                }
                break;
            case PHONE_CROP:
                photoPathListener.picturePhotoPath(cropImagePath);
                break;
        }
    }

    private String getPackageName() {
        if (activity != null) {
            return activity.getPackageName();
        }
        if (fragment != null) {
            return fragment.getContext().getPackageName();
        }
        return null;
    }

    private Context getContext() {
        if (activity != null) {
            return activity;
        }
        if (fragment != null) {
            return fragment.getContext();
        }
        return null;
    }
}
