package com.base.locationsharewhite.ui.howuse

import android.graphics.Color
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.viewpager2.widget.MarginPageTransformer
import com.base.locationsharewhite.R
import com.base.locationsharewhite.ads.AdsMgr
import com.base.locationsharewhite.ads.AdsShowCallBack
import com.base.locationsharewhite.bean.BannerBean
import com.base.locationsharewhite.databinding.ActivityHowUseShareBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.LogEx
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.util.BannerUtils

class HowUseShareActivity : BaseActivity<ActivityHowUseShareBinding>() {

    override val binding: ActivityHowUseShareBinding by lazy {
        ActivityHowUseShareBinding.inflate(layoutInflater)
    }

    private lateinit var adapter: ImageBannerAdapter
    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        initBanner()
        step1ui()
        AdsMgr.showBanner(binding.flBanner, false)
    }

    private fun initBanner() {
        adapter = ImageBannerAdapter(
            listOf(
                BannerBean(R.mipmap.how_share_tu1),
                BannerBean(R.mipmap.how_share_tu2)
            )
        )
        binding.banner.setAdapter(adapter)
        binding.banner.isAutoLoop(false)

        binding.banner.addBannerLifecycleObserver(this)
        binding.banner.setIndicator(binding.indicator, false)
        binding.banner.setPageTransformer(MarginPageTransformer(BannerUtils.dp2px(10f)))
        binding.indicator.indicatorConfig.height = resources.getDimensionPixelSize(R.dimen.dp_5)
        binding.indicator.indicatorConfig.selectedWidth = resources.getDimensionPixelSize(R.dimen.dp_25)
        binding.indicator.indicatorConfig.normalWidth = resources.getDimensionPixelSize(R.dimen.dp_5)
        binding.banner.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    step1ui()
                } else {
                    step2ui()
                }
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })

    }

    override fun initListener() {
        super.initListener()
        val activity = this
        onBackPressedDispatcher.addCallback {
//            if (AdsMgr.adsConfigBean.functionBackShowAd) {
//                AdsMgr.showInsert(activity, showCallBack = object : AdsShowCallBack() {
//                    override fun close(where: Int) {
//                        LogEx.logDebug(TAG, "where=$where")
//                        finishToMainTop()
//                    }
//
//                    override fun failed(where: Int) {
//                        LogEx.logDebug(TAG, "where=$where")
//                        finishToMainTop()
//                    }
//
//                    override fun googleFailed(where: Int) {
//                        LogEx.logDebug(TAG, "where=$where")
//                        finishToMainTop()
//                    }
//                })
//            } else {
//                finishToMainTop()
//            }
            finish()
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.tvBtn.setOnClickListener {
            if (uiType == 1) {
                step2ui()
            } else {
                AdsMgr.showInsert(activity, showCallBack = object : AdsShowCallBack() {
                    override fun close(where: Int) {
                        LogEx.logDebug(TAG, "where=$where")
                        finishToMainTop()
                    }

                    override fun failed(where: Int) {
                        LogEx.logDebug(TAG, "where=$where")
                        finishToMainTop()
                    }

                    override fun googleFailed(where: Int) {
                        LogEx.logDebug(TAG, "where=$where")
                        finishToMainTop()
                    }
                })
            }
        }
    }

    private var uiType = 0
    private fun step1ui() {
        uiType = 1
        binding.tvStep.text = resources.getString(R.string.how_share_location_step_1)
        if (binding.banner.currentItem != 0) {
            binding.banner.currentItem = 0
        }
        binding.tvBtn.text = getString(R.string.next)
    }

    private fun step2ui() {
        uiType = 2
        binding.tvStep.text = resources.getString(R.string.how_share_location_step_2)
        if (binding.banner.currentItem != 1) {
            binding.banner.currentItem = 1
        }
        binding.tvBtn.text = getString(R.string.let_s_go)
    }
}