package com.gamedog.vididin.main

import android.app.Activity
import android.widget.FrameLayout
import androidx.core.graphics.toColorInt
import com.ama.core.architecture.highlightpro.HighlightPro
import com.ama.core.architecture.highlightpro.parameter.Constraints
import com.ama.core.architecture.highlightpro.parameter.HighlightParameter
import com.ama.core.architecture.highlightpro.parameter.MarginOffset
import com.ama.core.architecture.highlightpro.shape.RectShape
import com.ama.core.architecture.util.ResUtil.dp
import com.ama.core.architecture.util.SpUtil
import com.ama.core.architecture.util.setOnClickBatch
import com.ama.core.architecture.widget.BindingDialog
import com.gamedog.statisticreporter.StatisticUtil
import com.gamedog.vididin.VidiConst
import com.gamedog.vididin.VidiConst.GUIDE_INDEX_GIFT
import com.gamedog.vididin.beans.RECORD_GOLD_PLUS_NEWBIE_GIFT
import com.gamedog.vididin.beans.RecordGold
import com.gamedog.vididin.core.login.login.AccountManager
import com.gamedog.vididin.manager.GuideManager
import com.viddin.videos.free.R
import com.viddin.videos.free.databinding.DialogBeginnerGiftBinding


class BeginnerGiftDialog(activity: Activity) : BindingDialog<DialogBeginnerGiftBinding>(activity, DialogBeginnerGiftBinding::inflate) {

    init {
        build()
    }


    private fun build() {
        with()
        setCenter()
        setMaskValue(0.93f)
        setCanCancel(false)

        mBinding.run {
            setOnClickBatch(tvAction) {
                when (this) {
                    tvAction ->  {
                        handleClickEvent()
                    }
                }
            }
        }

        showGuide()
    }

    private fun handleClickEvent() {
        if (mActivity is MainActivity) {
            (mActivity as MainActivity).switchTab(1)
        }
        if (!SpUtil.instance().getBoolean(SpUtil.KEY_GUIDE_HAS_GOT_NEWBIE_GOLD)) {
            AccountManager.adjustGold(VidiConst.NEWBIE_GIFT_GOLD_NUM, RecordGold(RECORD_GOLD_PLUS_NEWBIE_GIFT, VidiConst.NEWBIE_GIFT_GOLD_NUM))
            SpUtil.instance().putBoolean(SpUtil.KEY_GUIDE_HAS_GOT_NEWBIE_GOLD, true)
        }

        dismiss()
    }


    private fun showGuide() {
        HighlightPro.with(window?.decorView as FrameLayout)
            .setHighlightParameter {
                HighlightParameter.Builder()
                    .setHighlightView(mBinding.tvAction, {
                        val temp = 111
                    })
                    .setTipsViewId(R.layout.guide_step_withdraw)
                    .setHighlightShape(RectShape(22.dp, 22.dp, 22.dp))
                    .setHighlightHorizontalPadding(0.dp)
                    .setConstraints(Constraints.TopToBottomOfHighlight + Constraints.EndToEndOfHighlight)
                    .setMarginOffset(MarginOffset(top = -22.dp.toInt(), end = -10.dp.toInt()))
                    .build()
            }
            .setBackgroundColor("#00000000".toColorInt())
            .setOnShowCallback { index ->

            }
            .setOnDismissCallback {
                GuideManager.instance().setGuideIndex(GUIDE_INDEX_GIFT)
                handleClickEvent()
            }
            .interceptBackPressed(true)
            .show()


        StatisticUtil.reportEvents(StatisticUtil.KEY_Guide, mapOf("Guide" to 1))
    }



}

