package com.bc_chat.bc_base.view

import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.bc_chat.bc_base.R
import com.bc_chat.bc_base.config.RouteConfig
import com.bc_chat.bc_base.databinding.DialogAddContactBinding
import com.bc_chat.bc_base.http.Api
import com.bc_chat.bc_base.utils.AppConfig
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.AbsBottomDialogFragment
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.md5.MD5
import java.io.File

/**
 * author：Created by zhaohaoting on 2019/2/22
 * email：526309416@qq.com
 * desc：添加联系人
 */
class AddFriendDialogFragment : AbsBottomDialogFragment<BaseContract.BasePresenter>(), View.OnClickListener {

    private var qrcodeName: String? = null
    private var input: String? = null
    private var photo: String? = null

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

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

    override fun initialize() {
        val binding = rootDataBinding as DialogAddContactBinding
        binding.onClickEvent = this
        val user = AppConfig.instance.getUser()
        photo = user.photo
        qrcodeName = MD5.getStringMD5(user.photo + "_" + "user_${user.userId}_qr_code") + ".jpg"
        input = "${Api.SHARE_PATH}?source=bc_chat&type=${Const.QrType.USER_TYPE}&id=${user.userId}"
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_input_phone -> {
                ARouter.getInstance().build(RouteConfig.PHONE_SEARCH_FRIEND_ACTIVITY).navigation()
                dismissAllowingStateLoss()
            }
            R.id.tv_wei_chat_friend -> {
                judgePermission(Const.Permission.Key.STORAGE, *Const.Permission.Value.STORAGE)
            }
            R.id.tv_scanner_ -> {
                judgePermission(Const.Permission.Key.STORAGE_CAMERA, *Const.Permission.Value.STORAGE_CAMERA)
            }
        }
    }

    override fun requestPermissionSuccess(requestCode: Int) {
        if (Const.Permission.Key.STORAGE_CAMERA == requestCode) {
            ARouter.getInstance().build(RouteConfig.CAPTURE_ACTIVITY).navigation(activity, Const.RequestCode.SCANNER)
            dismissAllowingStateLoss()
        } else {
            var qrfile: File? = FileUtils.isHaveFile(qrcodeName!!)
            if (qrfile == null) {
                showLoading()
                val subscribe = QrUtils.createQrCode(getActivity()!!, photo!!, input!!, qrcodeName!!)
                    .compose<String>(RxSchedulers.applyObservableAsync()).subscribe {
                        dismissLoading()
                        qrfile = FileUtils.isHaveFile(qrcodeName!!)
                        ShareUtils.share(activity, SHARE_MEDIA.WEIXIN, qrfile, null)
                    }
            } else {
                ShareUtils.share(activity, SHARE_MEDIA.WEIXIN, qrfile, null)
            }
        }
    }
}