package com.input.board.appskey.keyboard.viewUi

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.widget.ImageView
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.input.board.appskey.keyboard.KeyBoardApp
import com.input.board.appskey.keyboard.R
import com.input.board.appskey.keyboard.unityad.UnityAdManager
import com.input.board.appskey.keyboard.unityad.onShowCallBack
import java.util.Timer

class NovaActivity : AppCompatActivity() {

    private lateinit var progressBar: ProgressBar
    private lateinit var novaIm: ImageView
    private var countTime = 11000L
    private lateinit var timer: CountDownTimer
    private var needShow = true


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        UnityAdManager.loadAd(UnityAdManager.unitAd1)
        UnityAdManager.loadAd(UnityAdManager.unitAd2)
        UnityAdManager.loadAd(UnityAdManager.unitAd3)
        setContentView(R.layout.activity_nova)
        init()
        findView()
    }

    private fun init() {

        timer = object : CountDownTimer(countTime, 100) {
            override fun onTick(millisUntilFinished: Long) {
                Log.d(
                    UnityAdManager.TAG,
                    "--------millisUntilFinished=${millisUntilFinished}  $needShow"
                )
                if (needShow) {
                    startShowAd({
                        toHome()
                    }, false)
                }
            }

            override fun onFinish() {
                Log.d(UnityAdManager.TAG, "--------onFinish  needShow=${needShow}")
                if (needShow) {
                    startShowAd({
                        toHome()
                    }, true)
                }
            }

        }
        if (KeyBoardApp.SDKOK) {
            Log.d(UnityAdManager.TAG, "--------initSDK true")
            timer.start()
        } else {
            LocalBroadcastManager.getInstance(this).registerReceiver(object : BroadcastReceiver() {
                override fun onReceive(context: Context?, intent: Intent?) {
                    Log.d(UnityAdManager.TAG, "--------onReceive")
                    timer.start()
                }

            }, IntentFilter(KeyBoardApp.AD_INIT_ACTION))
        }


    }


    private fun findView() {
        novaIm = findViewById(R.id.nova_picture)
        progressBar = findViewById<ProgressBar>(R.id.nova_progress)
    }


    private fun startShowAd(action: () -> Unit, isFinish: Boolean) {
        val readyUnitAd = UnityAdManager.getReadyUnitAd()
        if (readyUnitAd != null) {
            needShow = false
            UnityAdManager.showUnityAd(this@NovaActivity, readyUnitAd, object :
                onShowCallBack {
                override fun onShowFail() {
                    action.invoke()
                }

                override fun onShowClose() {
                    action.invoke()
                }

                override fun onShowSuccess() {

                }

            })
        } else {
            if (isFinish) {
                action.invoke()
            }

        }
    }


    private fun toHome() {
        startActivity(Intent(this, HomeActivity::class.java))
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }


}