package com.zxhl.main.page.view


import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color

import android.view.Gravity
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.umeng.socialize.utils.DeviceConfigInternal.context
import com.zxhl.cms.AppContext
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.utils.EventUtils
import com.zxhl.cms.utils.PhoneUtils
import com.zxhl.main.R


object DialogUtils {
    fun showBottomDialog(
        layout: Int,
        activity: Context,
        setHeadDialog: Dialog,
        animation: Int,
        gravity: Int
    ): View {

        val mDialogView = View.inflate(activity, layout, null)
        setHeadDialog.setContentView(mDialogView)
        val window = setHeadDialog.window
        window?.setGravity(gravity)
        val lp = window?.attributes
        window?.decorView?.setPadding(0, 0, 0, 0)
        lp?.width = PhoneUtils.getScreenWidth(AppContext.get())
        if (gravity == Gravity.TOP) {
            lp?.height = PhoneUtils.getScreenHeight(AppContext.get())
        }
        window?.attributes = lp
        if (animation > 0)
            window?.setWindowAnimations(animation)//设置动画
        return mDialogView
    }

    fun showFirstGuide(
        context: Activity,
        listener: View.OnClickListener
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_newpeople,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        val img_free_get = mDialogView.findViewById<ImageView>(R.id.id_img_free_get)

        img_free_get.setOnClickListener {
            mDialog.dismiss()
            SettingPreference.setShowGuide()
            listener.onClick(it)
        }
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog?.show()
    }

    /**
     * 1重抽卡:重新抽取当前盲盒,每次抽取最多只能使用一次重抽卡
    ·触发条件:每抽4次可获得

    2暴击卡:盲盒抽取获取商品后,使用该卡片可再获得该盲盒下的随机一商品
    ·触发条件:每抽5次可获得

    3回血卡:提升回收商品获取的幸运币数额,默认70%,使用后可增加10%-20%
    ·触发条件:每抽6次可获得
     */
    //道具卡
    fun showPropsCard(
        context: Activity?,
        cardType: Int,
        cardNum: Int,
        useStatus: Int,
        desc1: String,
        desc2: String, leftClick: View.OnClickListener, rightClick: View.OnClickListener
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_props_card,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        var dialog_bg = mDialogView.findViewById<LinearLayout>(R.id.id_props_card_bg)
        var desc1_tv1 = mDialogView.findViewById<TextView>(R.id.id_props_card_desc1)
        var desc1_tv2 = mDialogView.findViewById<TextView>(R.id.id_props_card_desc2)
        var leftBtn = mDialogView.findViewById<TextView>(R.id.btn_left_tv)
        var rightBtn = mDialogView.findViewById<TextView>(R.id.btn_right_tv)
        var tips = mDialogView.findViewById<TextView>(R.id.id_dialog_tips)
        desc1_tv1.text = desc1;
        desc1_tv2.text = desc2;

        //-1 隐藏右边按钮 0 不可使用 1刚刚获取 2 可以使用
        if (useStatus == -1) {
            leftBtn.text = context.resources.getString(R.string.box_detail_open_box_result_btn_konw)
            leftBtn.setTextColor(Color.parseColor("#4C84FF"))
            leftBtn.background = ContextCompat.getDrawable(
                context,
                R.drawable.shape_4c84ff_r25_stroke
            )
            rightBtn.text = context.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
            rightBtn.visibility = View.GONE

            tips.visibility = View.GONE
        } else if (useStatus == 0) {
            leftBtn.text = context.resources.getString(R.string.box_detail_open_box_result_btn_konw)
            leftBtn.setTextColor(Color.parseColor("#4C84FF"))
            leftBtn.background = ContextCompat.getDrawable(
                context,
                R.drawable.shape_4c84ff_r25_stroke
            )
            rightBtn.text = context.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
            rightBtn.visibility = View.VISIBLE
            tips.visibility = View.GONE
        } else if (useStatus == 1) {
            leftBtn.text = context.resources.getString(R.string.box_detail_open_box_result_btn_receive)
            leftBtn.setTextColor(Color.parseColor("#4C84FF"))
            leftBtn.background = ContextCompat.getDrawable(
                context,
                R.drawable.shape_4c84ff_r25_stroke
            )
            rightBtn.text = context.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
            rightBtn.visibility = View.VISIBLE
            tips.visibility = View.GONE
        } else {
            leftBtn.text = "下次使用"
            leftBtn.setTextColor(Color.parseColor("#4C84FF"))
            leftBtn.background = ContextCompat.getDrawable(
                context,
                R.drawable.shape_ff2e31_r25_stoke
            )
            rightBtn.text = "立即使用"
            rightBtn.visibility = View.VISIBLE
//            tips.visibility = View.VISIBLE
        }

        if (cardNum > 0) {
            when (cardType) {
                1 -> {
                    dialog_bg.background = ContextCompat.getDrawable(
                        context,
                        R.drawable.props_card_bj_dialog_chongchou
                    )
                }
                2 -> {
                    dialog_bg.background = ContextCompat.getDrawable(
                        context,
                        R.drawable.props_card_bj_dialog_baoji
                    )
                }
                3 -> {
                    dialog_bg.background = ContextCompat.getDrawable(
                        context,
                        R.drawable.props_card_bj_dialog_huixue
                    )
                }
            }
        } else {
            when (cardType) {
                1 -> {
                    dialog_bg.background = ContextCompat.getDrawable(
                        context,
                        R.drawable.props_card_bj_dialog_chongchou_weihuode
                    )
                }
                2 -> {
                    dialog_bg.background = ContextCompat.getDrawable(
                        context,
                        R.drawable.props_card_bj_dialog_baoji_weihuode
                    )
                }
                3 -> {
                    dialog_bg.background = ContextCompat.getDrawable(
                        context,
                        R.drawable.props_card_bj_dialog_huixue_weihuode
                    )
                }
            }
        }
        when (cardType) {
            1 -> {
                EventUtils.onEvent("chongchou_show")
            }
            2 -> {
                EventUtils.onEvent("baoji_card_show")
            }
            3 -> {
                EventUtils.onEvent("huixue_card_show")
            }
        }
        leftBtn.setOnClickListener {
            mDialog.dismiss()
            leftClick.onClick(it)
        }
        rightBtn.setOnClickListener {
            mDialog.dismiss()
            rightClick.onClick(it)
        }
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog.show()
    }

    fun showShopTips(
        context: Activity
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_shop_tips,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        mDialogView.findViewById<TextView>(R.id.id_makesure_btn).setOnClickListener {
            mDialog.dismiss()
        }
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog?.show()
    }
}