package com.base.locationsharewhite.ui.main

import android.graphics.Color
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.locationsharewhite.R
import com.base.locationsharewhite.ads.AdsMgr
import com.base.locationsharewhite.ads.AdsShowCallBack
import com.base.locationsharewhite.databinding.ActivityLocationCodeBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.location.LocationLoginUtils
import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.ClipboardUtils.copyText
import com.base.locationsharewhite.utils.LogEx
import com.base.locationsharewhite.utils.ToastUtils.toast


class LocationCodeActivity : BaseActivity<ActivityLocationCodeBinding>() {

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


    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        val recreated = changeLanguage(this)
        if (recreated) return

        if (AdsMgr.adsConfigBean.functionInShowAd) {
            AdsMgr.showInsert(this, showCallBack = object : AdsShowCallBack() {
                override fun close(where: Int) {
                    LogEx.logDebug(TAG, "where=$where")
                }

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

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

        binding.tvCode.text = LocationLoginUtils.invitationCodeSp
        binding.tvCopy.setOnClickListener {
//            AdsMgr.showInsert(this, false, object : AdsShowCallBack() {
//                override fun close(where: Int) {
//                    copy()
//                }
//
//                override fun failed(where: Int) {
//                    copy()
//                }
//
//                override fun googleFailed(where: Int) {
//                    copy()
//                }
//            })
            copy()
        }
        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_app_exit)
    }

    override fun onResumeOneShoot() {
        super.onResumeOneShoot()
        val recreated = changeLanguage(this)
        if (recreated) return
    }

    private fun copy() {
        copyText("MyLocationCode", binding.tvCode.text.toString())
        toast(resources.getString(R.string.copy), true)
    }

    override fun initListener() {
        super.initListener()

        onBackPressedDispatcher.addCallback {
            if (AdsMgr.adsConfigBean.functionBackShowAd) {
                AdsMgr.showInsert(this@LocationCodeActivity, false, object : AdsShowCallBack() {
                    override fun close(where: Int) {
                        finishToMain()
                    }

                    override fun failed(where: Int) {
                        finishToMain()
                    }

                    override fun googleFailed(where: Int) {
                        finishToMain()
                    }
                })
            } else {
                finishToMain()
            }
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
    }

}