package com.bc_chat.bc_base.view

import com.bc_chat.bc_base.R
import com.bc_chat.bc_base.databinding.FragmentQrCodeBinding
import com.bc_chat.bc_base.utils.CacheUtils
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.bc_base.utils.QrUtils
import com.share.utils.ShareUtils
import com.umeng.socialize.bean.SHARE_MEDIA
import com.zhaohaoting.framework.abs.AbsDialogFragment
import com.zhaohaoting.framework.abs.contract.BaseContract
import com.zhaohaoting.framework.utils.RxSchedulers
import com.zhaohaoting.framework.utils.file.FileUtils
import com.zhaohaoting.framework.utils.glid.GlideUtils
import com.zhaohaoting.framework.utils.md5.MD5
import java.io.File

/**
 * author：Created by zhaohaoting on 2019/2/22
 * email：526309416@qq.com
 * desc：二维码保存到应用目录下 保存到相册走copy方法
 */
class QrCodeDialogFragment : AbsDialogFragment<BaseContract.BasePresenter>() {

    private var qrcode_name: String? = null
    private var input: String? = null
    private var photo: String? = null
    private var qrfile: File? = null

    private val binding by lazy {
        rootDataBinding as FragmentQrCodeBinding
    }

    override fun initialize() {
        photo = arguments?.getString("photo")
        qrcode_name = MD5.getStringMD5(photo + "_" + arguments?.getString("qrCodeName")) + ".jpg"
        input = arguments?.getString("url")
        qrfile = FileUtils.isHaveFile(qrcode_name!!)
        if (qrfile == null) {
            judgePermission(Const.Permission.Key.STORAGE, *Const.Permission.Value.STORAGE)
        } else {
            GlideUtils.loadNoCache(activity, "file://${qrfile?.absolutePath}", binding.ivQrCode)
        }
        binding.tvSaveAlbum.setOnClickListener {
            if (qrfile == null) return@setOnClickListener
            val copyPictureAlbum = CacheUtils.copyPictureAlbum(qrfile?.absolutePath!!, qrcode_name!!)
            if (copyPictureAlbum != null) {
                FileUtils.insertIntoMediaStore(activity, false, copyPictureAlbum, System.currentTimeMillis())
            }
        }
        binding.tvShare.setOnClickListener {
            if (qrfile == null) return@setOnClickListener
            ShareUtils.share(activity, SHARE_MEDIA.WEIXIN, qrfile, null)
        }
    }

    override fun requestPermissionSuccess(requestCode: Int) {
        showLoading()
        val subscribe = QrUtils.createQrCode(getActivity()!!, photo!!, input!!, qrcode_name!!)
            .compose<String>(RxSchedulers.applyObservableAsync()).subscribe {
                dismissLoading()
                qrfile = FileUtils.isHaveFile(qrcode_name!!)
                GlideUtils.loadNoCache(activity, "file://$it", binding.ivQrCode)
            }
    }

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

    override fun getLayoutResId(): Int {
        return R.layout.fragment_qr_code
    }
//
//    override fun onActivityCreated(savedInstanceState: Bundle?) {
//        super.onActivityCreated(savedInstanceState)
//        dialog.window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//    }
}
