package com.zj365.mime.weight

import android.content.Context
import android.graphics.Bitmap
import android.view.Gravity
import android.view.View
import com.androidkun.xtablayout.XTabLayout
import com.androidkun.xtablayout.XTabLayout.OnTabSelectedListener
import com.xty.base.dialog.BaseDialog
import com.xty.common.BuildConfig
import com.xty.common.WxUtils
import com.zj365.mime.databinding.DialogInviteFriendChooseBinding
import com.xty.network.model.SettingBean

class InviteFriendShareDialog(
    context: Context,
    var type:Int = 0 ,//默认0 图片分享跟链接分享都有，1 只有链接邀请
    var user: SettingBean.User,
    private var bitmap: Bitmap?
) : BaseDialog(context) {

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

    private var tabName = arrayOf("二维码邀请", "链接邀请")

    var isLink = false

    override fun initView() {
        super.initView()
        initTab()
        if(type == 1){
            binding.xTablayout.visibility = View.GONE
            isLink = true
        }
        binding.imgClose.setOnClickListener {
            dismiss()
        }

        binding.llCircle.setOnClickListener {
            if (isLink) {
                val subtitle = "${user.name}邀请您注册"
                val url = "${BuildConfig.url}shareApi/register/${user.phone}"
                WxUtils.shareWebPageForCircle("中健三六五健康，因爱而生", subtitle, context, url)
                dismiss()
            } else {
                bitmap?.let {
                    WxUtils.shareBitmapForCircle(context, it)
                    dismiss()
                }
            }
        }

        binding.llWx.setOnClickListener {
            if (isLink) {
                val subtitle = "${user.name}邀请您注册"
                val url = "${BuildConfig.url}shareApi/register/${user.phone}"
                WxUtils.shareWebPage("邀请注册", subtitle, context, url)
                dismiss()
            } else {
                bitmap?.let {
                    WxUtils.shareBitmapTwo(context, it)
                    dismiss()
                }
            }
        }

        window!!.setGravity(Gravity.BOTTOM)
    }

    private fun initTab() {
        tabName.forEach {
            val tab = binding.xTablayout.newTab()
            tab.text = it
            binding.xTablayout.addTab(tab)
        }

        binding.xTablayout.setOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: XTabLayout.Tab?) {
                isLink = tab!!.position != 0
            }

            override fun onTabUnselected(tab: XTabLayout.Tab?) {
            }

            override fun onTabReselected(tab: XTabLayout.Tab?) {
            }

        })
    }
}