package com.bc_chat.bc_base.view

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.support.v4.content.FileProvider
import android.view.View
import com.bc_chat.bc_base.R
import com.bc_chat.bc_base.databinding.DialogPictureSelectBinding
import com.zhaohaoting.framework.abs.AbsBottomDialogFragment
import com.zhaohaoting.framework.abs.contract.BaseContract
import com.zhaohaoting.framework.utils.file.FileUtils
import com.zhaohaoting.framework.utils.file.PhotoUtils
import java.io.File

/**
 * author：Created by zhaohaoting on 2019/2/22
 * email：526309416@qq.com
 * desc：获取图片对话框
 */
class PictureDialogFragment : AbsBottomDialogFragment<BaseContract.BasePresenter>(), View.OnClickListener {

    private var photoPath: String? = null////拍照路径
    private var cropImagePath: String? = null//裁剪路径
    private var photoUri: Uri? = null
    private var isCrop: Boolean = false
    private var aspectX: Int = 1000//X方向的比例
    private var aspectY: Int = 1001//Y方向的比例

    private var width: Int = 800//宽度
    private var height: Int = 800//高度

    override fun initialize() {
        val binding = rootDataBinding as DialogPictureSelectBinding
        binding.onClickEvent = this
        arguments?.let {
            isCrop = it.getBoolean("isCrop", false)
            aspectX = it.getInt("aspectX", 1000)
            aspectY = it.getInt("aspectY", 1001)
            width = it.getInt("width", 800)
            height = it.getInt("height", 800)
        }
        if (isCrop) {
            val fileCropUri = FileUtils.createImgFile("crop_photo")
            cropImagePath = fileCropUri.absolutePath
        }
    }

    override fun initPresenter(): BaseContract.BasePresenter? = null

    override fun getLayoutResId(): Int = R.layout.dialog_picture_select

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_take_pictures -> {
                val photoFile = FileUtils.createImgFile("photo")
                photoPath = photoFile.absolutePath
                photoUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //通过FileProvider创建一个content类型的Ur
                    FileProvider.getUriForFile(context!!, activity?.packageName!! + ".provider", photoFile)
                } else {
                    Uri.fromFile(photoFile)
                }
                PhotoUtils.takePicture(null, this, photoUri, TAKE_PHOTO)
            }
            R.id.tv_album -> {
                PhotoUtils.openPic(null, this, PICK_PHOTO)
            }
            R.id.tv_cancel -> {
                dismissAllowingStateLoss()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        when (requestCode) {
            PICK_PHOTO -> {
                photoPath = PhotoUtils.getPath(context, data?.data)
                photoUri = Uri.fromFile(File(photoPath))
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    photoUri = FileProvider.getUriForFile(context!!, activity?.packageName!! + ".provider", File(photoPath))
                }
                if (!isCrop) {
                    photoPathListener?.picturePhotoPath(photoPath!!)
                } else {
                    val cropImageUri = Uri.fromFile(File(cropImagePath))
                    PhotoUtils.cropImageUri(null, this, photoUri, cropImageUri, aspectX, aspectY, width, height, PHONE_CROP, false)
                }
            }
            TAKE_PHOTO -> if (!isCrop) {
                photoPathListener?.picturePhotoPath(photoPath!!)
            } else {
                val cropImageUri = Uri.fromFile(File(cropImagePath))
                PhotoUtils.cropImageUri(null, this, photoUri, cropImageUri, aspectX, aspectY, width, height, PHONE_CROP, false)
            }
            PHONE_CROP -> photoPathListener?.picturePhotoPath(cropImagePath!!)
        }
    }

    fun setPicturePhotoPathLinstener(listener: (photoPath: String) -> Unit) {
        photoPathListener = object : OnPicturePhotoPathListener {
            override fun picturePhotoPath(photoPath: String) {
                dismissAllowingStateLoss()
                listener(photoPath)
            }
        }
    }

    private var photoPathListener: OnPicturePhotoPathListener? = null

    interface OnPicturePhotoPathListener {
        fun picturePhotoPath(photoPath: String)
    }

    companion object {
        const val PICK_PHOTO = 603//相册
        const val TAKE_PHOTO = 604//拍照
        const val PHONE_CROP = 605//裁剪
    }
}