package com.sunny.app.soft.timberkeyboardnew.ui.activity

import android.content.Intent
import android.os.CountDownTimer
import android.view.View
import com.sunny.app.soft.timberkeyboardnew.databinding.ActivityLoadingBinding

class LoadingActivity : BaseActivity() {

    private lateinit var binding: ActivityLoadingBinding

    private lateinit var timer: CountDownTimer

    private var hasShow = false


    override fun setRootView(): View {
        binding = ActivityLoadingBinding.inflate(layoutInflater)
        return binding.root
    }

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

    override fun initView() {
        super.initView()

        timer = object : CountDownTimer(11000, 100) {
            override fun onTick(millisUntilFinished: Long) {

                if (hasShow) return
                show(false) {
                        intoMainActivity()

                }


            }

            override fun onFinish() {

                if (!hasShow) {
                    show(true) {
                        intoMainActivity()
                    }
                }

            }

        }

        timer.start()
    }


    private fun show(isFinish: Boolean, action: (showSuccess: Boolean) -> Unit) {
//        val readyUnitAd = UnityAdManager.getReadyUnitAd()
//        if (readyUnitAd != null) {
//            hasShow = true
//            UnityAdManager.showUnityAd(this@LoadingActivity, readyUnitAd, object :
//                onShowCallBack {
//                override fun onShowFail() {
//                    action.invoke(false)
//                }
//
//                override fun onShowClose() {
//                    intoMainActivity()
//                    action.invoke(true)
//                }
//
//                override fun onShowSuccess() {
//
//                }
//
//            })
//        } else {
//            if (isFinish) {
//                action.invoke(false)
//            }
//
//        }
    }

    private fun intoMainActivity() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

}