package com.zj365.mime.act

import android.graphics.Color
import android.view.View
import androidx.core.view.isVisible
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.zxing.client.android.utils.ZXingUtils
import com.xty.base.act.BaseVmAct
import com.xty.common.BuildConfig
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.setImageUser
import com.xty.common.util.CommonUtils
import com.xty.common.util.PermissionUtils
import com.xty.common.util.ViewScreenshotUtils
import com.xty.network.model.SettingBean
import com.zj365.mime.R
import com.zj365.mime.databinding.ActInviteFriendsBinding
import com.zj365.mime.vm.InviteFriendsVm
import com.zj365.mime.weight.InviteFriendShareDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * 邀请好友
 */
@Route(path = ARouterUrl.INVITE_FRIENDS)
class InviteFriendsAct : BaseVmAct<InviteFriendsVm>() {

    val binding by lazy { ActInviteFriendsBinding.inflate(layoutInflater) }
    override fun setLayout() = binding.root

    private val defaultIcon by lazy {
        arrayOf(
            R.mipmap.ic_default_man,
            R.mipmap.ic_default_women
        )
    }

    private var user: SettingBean.User? = null

    override fun liveObserver() {
    }

    override fun initData() {
        super.initData()
        val userBean = intent.extras!!.getSerializable("user")
        if (userBean != null) {
            user = userBean as SettingBean.User
        }

        user?.let {
            binding.styleOne.tvUsername.text = it.name
            binding.styleOne.tvUserTel.text = CommonUtils.replaceMiddleNumber(it.phone)

            binding.styleOne.tvUsernameSave.text = it.name
            binding.styleOne.tvUserTelSave.text = CommonUtils.replaceMiddleNumber(it.phone)

            binding.styleTwo.tvUsernameTwo.text = it.name
            binding.styleTwo.tvUserTelTwo.text = CommonUtils.replaceMiddleNumber(it.phone)
            binding.styleTwo.tvUsernameTwoSave.text = it.name
            binding.styleTwo.tvUserTelTwoSave.text = CommonUtils.replaceMiddleNumber(it.phone)

            val url = "${BuildConfig.url}shareApi/register/${it.phone}"
            val whitBitmap = ZXingUtils.createQRCodeImage(url,200,0, Color.parseColor("#FFFFFF"),Color.parseColor("#00000000"),"L",null)
                //XQRCode.createQRCodeWithLogo(url, null)

            val bitmap = ZXingUtils.createQRCodeImage(url)
            binding.styleOne.imgQr.setImageBitmap(bitmap)
            binding.styleOne.imgQrSave.setImageBitmap(bitmap)

            binding.styleTwo.imgQrTwo.setImageBitmap(whitBitmap)
            binding.styleTwo.imgQrTwoSave.setImageBitmap(whitBitmap)

            if (it.avatarUrl.isNullOrEmpty()) {
                binding.styleOne.mImage.setImageResource(defaultIcon[if (it.sex - 1 < 0) 0 else it.sex - 1])
                binding.styleOne.mImageSave.setImageResource(defaultIcon[if (it.sex - 1 < 0) 0 else it.sex - 1])

                binding.styleTwo.mImageTwo.setImageResource(defaultIcon[if (it.sex - 1 < 0) 0 else it.sex - 1])
                binding.styleTwo.mImageTwoSave.setImageResource(defaultIcon[if (it.sex - 1 < 0) 0 else it.sex - 1])
            } else {
                binding.styleOne.mImage.setImageUser(
                    this,
                    it.avatarUrl,
                    it.sex.toString()
                )
                binding.styleOne.mImageSave.setImageUser(
                    this,
                    it.avatarUrl,
                    it.sex.toString()
                )

                binding.styleTwo.mImageTwo.setImageUser(
                    this,
                    it.avatarUrl,
                    it.sex.toString()
                )
                binding.styleTwo.mImageTwoSave.setImageUser(
                    this,
                    it.avatarUrl,
                    it.sex.toString()
                )
            }
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "邀请好友"

        binding.title.mTvRight.apply {
            visibility = View.VISIBLE
            text = "邀请记录"
            setTextColor(resources.getColor(R.color.col_02c))
            setOnClickListener {
                user?.let {
                    bundle.clear()
                    bundle.putSerializable("user", it)
                    RouteManager.goAct(ARouterUrl.LNVITATION_MESSAGE, bundle)
                }
            }
        }
        binding.title.mIvBack.setOnClickListener { finish() }

        //更换样式
        binding.styleOne.tvChange.setOnClickListener {
            if (binding.styleOne.fmOne.isVisible) {
                binding.styleOne.fmOne.visibility = View.GONE
                binding.styleTwo.fmTwo.visibility = View.VISIBLE
            }
        }

        binding.styleTwo.tvChange.setOnClickListener {
            if (binding.styleTwo.fmTwo.isVisible) {
                binding.styleTwo.fmTwo.visibility = View.GONE
                binding.styleOne.fmOne.visibility = View.VISIBLE
            }
        }

        //保存图片
        binding.styleOne.tvSave.setOnClickListener {
            mainScope.launch(Dispatchers.IO) {

              /*  PermissionUtils.requestPermission(this@InviteFriendsAct,
                   {ViewScreenshotUtils.viewSaveToImage(
                       binding.styleOne.clOneSave,
                       this@InviteFriendsAct
                   )}, Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE)*/

            }
        }

        binding.styleTwo.tvSave.setOnClickListener {
            mainScope.launch(Dispatchers.IO) {

               /* PermissionUtils.requestPermission(this@InviteFriendsAct,
                    {ViewScreenshotUtils.viewSaveToImage(
                        binding.styleOne.clOneSave,
                        this@InviteFriendsAct
                    )}, Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE)*/

            }
        }

        //邀请好友
        binding.styleOne.llInvite.setOnClickListener {
            user?.let {
                InviteFriendShareDialog(
                    this@InviteFriendsAct,0,
                    it,
                    ViewScreenshotUtils.loadBitmapFromView(binding.styleOne.clOneSave)
                ).showDialog()
            }
        }

        binding.styleTwo.llInviteTwo.setOnClickListener {
            user?.let {
                InviteFriendShareDialog(
                    this@InviteFriendsAct,0,
                    it,
                    ViewScreenshotUtils.loadBitmapFromView(binding.styleTwo.clTwoSave)
                ).showDialog()
            }
        }
    }
}