package com.duben.microtribe.ui.widgets

import android.app.Activity
import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.TextView
import com.duben.microtribe.R
import com.duben.microtribe.ad.express.ExpressAdCallback
import com.duben.microtribe.ad.express.ExpressManager
import com.duben.microtribe.mvp.model.VedioBean
import com.duben.microtribe.ui.activitys.VipActivity
import com.duben.microtribe.ui.widgets.countdowntimer.CountDownTimerSupport
import com.duben.microtribe.ui.widgets.countdowntimer.OnCountDownTimerListener
import com.duben.microtribe.utils.UIUtils

class VipCountDialog(val activity: Activity, vedioBean: VedioBean, tipList: ArrayList<String>?) :
    Dialog(activity, R.style.dialog) {

    private val lp: WindowManager.LayoutParams
    var timer: CountDownTimerSupport? = null

    private val flAd: FrameLayout

    init {
        setContentView(R.layout.dialog_vip_count)
        // 设置window属性
        lp = window!!.attributes
        lp.gravity = Gravity.CENTER
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.windowAnimations = R.style.DialogAnimFade
        window!!.attributes = lp

        // 设置外部不可关闭
        setCancelable(false)
        setCanceledOnTouchOutside(false)
        setOnKeyListener { dialogInterface, i, keyEvent ->
            i == KeyEvent.KEYCODE_BACK
        }

        val fm = findViewById<View>(R.id.fm)
        val text_count = findViewById<TextView>(R.id.text_count)
        val tv_dialog_vip_count_left = findViewById<TextView>(R.id.tv_dialog_vip_count_left)
        val tv_dialog_vip_count_right = findViewById<TextView>(R.id.tv_dialog_vip_count_right)


        flAd = findViewById(R.id.fl_ad)

        timer = CountDownTimerSupport(3000L, 1000)
        timer?.setOnCountDownTimerListener(object : OnCountDownTimerListener {
            override fun onTick(millisUntilFinished: Long) {
                text_count.text = (millisUntilFinished / 1000L).toString()
            }

            override fun onFinish() {
                dismiss()
            }
        })
        timer?.start()

        if (tipList != null && tipList.size >= 2) {
            tv_dialog_vip_count_left.text = tipList.get(0)
            tv_dialog_vip_count_right.text = " ${tipList.get(1)}"
        }


        fm.setOnClickListener {
            val bundle = Bundle()
            bundle.putString(VipActivity.VEDIO_ID, vedioBean.vedioId.toString())
            bundle.putString(VipActivity.THIRD_ID, vedioBean.thirdId)
            readyGo(activity, VipActivity::class.java, bundle)
            dismiss()
        }

        showAd()
    }

    override fun dismiss() {
        if (activity.isFinishing) return
        super.dismiss()
        this.mOnDialogDismiss?.onDialogDismiss()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        timer?.stop()
        timer = null
    }

    private var mOnDialogDismiss: OnDialogDismiss? = null

    fun setOnDialogDismiss(onDialogDismiss: OnDialogDismiss): Dialog {
        this.mOnDialogDismiss = onDialogDismiss
        return this
    }

    interface OnDialogDismiss {
        fun onDialogDismiss()
    }

    private fun readyGo(activity: Activity, clazz: Class<*>?, bundle: Bundle?) {
        val intent = Intent(activity, clazz)
        if (null != bundle) {
            intent.putExtras(bundle)
        }
        activity.startActivity(intent)
    }

    private fun showAd() {
        ExpressManager.instance.getAdView(object : ExpressAdCallback {
            override fun loadSuccess(adView: FrameLayout?) {
                adView?.let {
                    UIUtils.removeFromParent(it)
                    flAd.removeAllViews()
                    flAd.addView(it)
                }

                ExpressManager.instance.preLoadAd()
            }

            override fun renderSuccess(adView: FrameLayout?): Boolean {
                adView?.let {
                    UIUtils.removeFromParent(it)
                    flAd.removeAllViews()
                    flAd.addView(it)
                }

                ExpressManager.instance.preLoadAd()
                return false
            }

            override fun loadFail() {
            }
        })
    }

}