package com.xty.common.xqr_code

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.Fragment
import com.hjq.toast.ToastUtils
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.xty.common.R
import com.xty.common.picture.PictureUtils
import com.xty.common.util.PathUtils
import com.xuexiang.xqrcode.XQRCode
import com.xuexiang.xqrcode.ui.CaptureActivity
import com.xuexiang.xqrcode.util.QRCodeAnalyzeUtils.AnalyzeCallback
import com.xuexiang.xutil.app.IntentUtils


class XQRCodeAct : CaptureActivity(), View.OnClickListener {

    companion object {
        /**
         * 扫描跳转Activity RequestCode
         */
        const val QRCODE_AGREEMENT = 3001 //二维码扫描

        /**
         * 选择系统图片Request Code
         */
        const val REQUEST_IMAGE = 112

        /**
         * 开始二维码扫描
         *
         * @param fragment
         * @param requestCode 请求码
         * @param theme       主题
         */
        fun start(fragment: Fragment, requestCode: Int, theme: Int) {
            val intent = Intent(
                fragment.context,
                XQRCodeAct::class.java
            )
            intent.putExtra(KEY_CAPTURE_THEME, theme)
            fragment.startActivityForResult(intent, requestCode)
        }

        /**
         * 开始二维码扫描
         *
         * @param activity
         * @param requestCode 请求码
         * @param theme       主题
         */
        fun start(activity: Activity, requestCode: Int, theme: Int) {
            val intent = Intent(activity, XQRCodeAct::class.java)
            intent.putExtra(KEY_CAPTURE_THEME, theme)
            activity.startActivityForResult(intent, requestCode)
        }
    }

//    override fun initCaptureFragment() {
//        super.initCaptureFragment()
//    }

    override fun getCaptureLayoutId(): Int {
        return R.layout.act_xqr_code
    }

    //做二维码采集之前需要做的事情
    override fun beforeCapture() {
        val ivCoolAlbum = findViewById<ImageView>(R.id.iv_xqr_code_cool_album)
        ivCoolAlbum.setOnClickListener(this)
    }

    //相机初始化成功
    override fun onCameraInitSuccess() {
    }

    //相机初始化失败
    override fun onCameraInitFailed() {
        ToastUtils.show("相机初始化失败！请检查设置")
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode === RESULT_OK) {
            when (requestCode) {
//                REQUEST_IMAGE -> {
//                    getAnalyzeQRCodeResult(data?.data!!)
//                }
                PictureConfig.CHOOSE_REQUEST -> {
                    val result = PictureSelector.obtainMultipleResult(data)
                    if (result.size > 0) {
                        if (result.isNotEmpty()) {
                            var imgPath = ""
                            for (item in result) {
                                imgPath = item.compressPath.ifEmpty {
                                    item.androidQToPath
                                }
                            }
                            getAnalyzeQRCodeResult(imgPath)
                        }
                    }
                }
            }
        }
    }

    /**
     * 进行二维码解析
     * @param imgPath
     */
    @SuppressLint("MissingPermission")
    private fun getAnalyzeQRCodeResult(imgPath: String) {
//    private fun getAnalyzeQRCodeResult(uri),: Uri) {
//            PathUtils.getFilePathByUri(this, uri),
        XQRCode.analyzeQRCode(
            imgPath,
            object : AnalyzeCallback {
                override fun onAnalyzeSuccess(mBitmap: Bitmap, result: String) {
                    val bundle = Bundle()
                    bundle.putInt(XQRCode.RESULT_TYPE, XQRCode.RESULT_SUCCESS)
                    bundle.putString(XQRCode.RESULT_DATA, result)
                    intent.putExtras(bundle)
                    setResult(RESULT_OK, intent)
                    onBackPressed()
                }

                override fun onAnalyzeFailed() {
                    ToastUtils.show("解析二维码失败")
                }
            })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            //点击相册
            R.id.iv_xqr_code_cool_album -> {
                PictureUtils.openCamera(this, PictureMimeType.ofImage(), 1)
//                startActivityForResult(
//                    IntentUtils.getDocumentPickerIntent(IntentUtils.DocumentType.IMAGE),
//                    REQUEST_IMAGE
//                )
            }
        }
    }
}