package com.base.browserwhite.ui.activity.splash

import CloseNotificationReceiver
import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Handler
import android.view.KeyEvent
import androidx.activity.result.contract.ActivityResultContracts
import com.base.browserwhite.R
import com.base.browserwhite.ads.AdmobMaxHelper
import com.base.browserwhite.bean.ConstObject.ifAgreePrivacy
import com.base.browserwhite.databinding.ActivitySplash2Binding
import com.base.browserwhite.fcm.NotificationUtil
import com.base.browserwhite.help.EventUtils
import com.base.browserwhite.help.WeatherUtils
import com.base.browserwhite.service.StayNotificationService.Companion.startStayNotification
import com.base.browserwhite.ui.activity.BaseActivity
import com.base.browserwhite.utils.BarUtils

@SuppressLint("CustomSplashScreen")
class Splash2Activity : BaseActivity<ActivitySplash2Binding>(),
    PrivacyManager.onUserPrivacyAggreementListener, TaskManager.ProgressListener {
    private val TAG = "NewStartActivity"
    private fun initStatusBar() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        if (Build.VERSION.SDK_INT >= 33) {
            registerForActivityResult(ActivityResultContracts.RequestPermission()) {}.launch(
                Manifest.permission.POST_NOTIFICATIONS
            )
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            EventUtils.event("back")
        }
        return super.onKeyDown(keyCode, event)
    }


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

    private var mTaskManager: TaskManager? = null
    var jumpType = 0

    @SuppressLint("SetTextI18n")
    override fun initView() {
        initStatusBar()
        if (isDestroyed) {
            return
        }

        NotificationUtil.stopNotificationHandler()
        jumpType = intent.getIntExtra("actionId", 0)
        closeNotification()
        mTaskManager = TaskManager(binding, this)
        if (ifAgreePrivacy) {
            onAgreePrivacy()
        } else {
            PrivacyManager(binding, this, this)
        }

        Thread {

            if (!WeatherUtils.hasWeatherDataToday()) {
                WeatherUtils.requestWeatherData()
            }

            val weatherCityBean = WeatherUtils.getWeatherEntity()
            val weather = weatherCityBean?.list?.random()
            if (weather != null) {
                binding.root.post {
                    binding.tvDate.text = weather.fxDate
                    binding.tvTemperature.text = weather.tempMin + "℃" + " / " + weather.tempMax + "℃"

                    val icon = when (weather.iconDay) {
                        "Sunny day" -> R.mipmap.x_qing
                        "Cloudy day" -> R.mipmap.x_yin
                        "Rainy day" -> R.mipmap.x_dayu
                        "Snowy day" -> R.mipmap.x_xiaxue
                        "Greasy day" -> R.mipmap.x_wumaishachengbao
                        "Unknown" -> R.mipmap.x_qing
                        else -> R.mipmap.x_qing
                    }
                    binding.ivWeather.setImageResource(icon)
                }
            }


        }.start()

    }

    private fun closeNotification() {
        sendBroadcast(Intent(this, CloseNotificationReceiver::class.java).apply {
            this.action = CloseNotificationReceiver.Action
            this.putExtra(CloseNotificationReceiver.NotificationId, jumpType)
        })
    }

    override fun onAgreePrivacy() {
        EventUtils.event("app_start")
        if (jumpType == 0) {
            startStayNotification()
        }
        AdmobMaxHelper.preloadAd(this)
        mTaskManager?.startProgress()
        loadAd()
    }


    override fun onProgressMax() {
        Handler().postDelayed({
            StartUtils.jumpNextPage(this)
        }, 500)
    }

    private fun loadAd() {
        AdmobMaxHelper.admobMaxShowOpenAd(this, {
            mTaskManager?.pauseProgress()
        }, {
            mTaskManager?.pauseProgress()
            runOnUiThread {
                mTaskManager?.maxProgress()
            }

        })
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
    }

    private var isPause = false

    override fun onStart() {
        super.onStart()
        isPause = false
    }

    override fun onPause() {
        super.onPause()
        isPause = true
        EventUtils.event("onPause")
    }

    override fun onResume() {
        super.onResume()
        isPause = false
        EventUtils.event("onResume")
    }

    override fun onStop() {
        super.onStop()
        isPause = true
    }


}