package com.mints.goodmoney.ad.video

import android.app.Activity
import android.content.DialogInterface
import android.text.TextUtils
import android.view.KeyEvent
import com.mints.goodmoney.MintsApplication
import com.mints.goodmoney.mvp.views.BaseView
import com.mints.goodmoney.ui.widgets.LoadingDialog
import com.mints.goodmoney.utils.ToastUtil

open class BaseVideoAd(activity: Activity) : BaseView {
    var activity: Activity
    var progressDialog: LoadingDialog? = null

    init {
        this.activity = activity
    }

    override fun getBaseApplication(): MintsApplication {
        return activity.getApplication() as MintsApplication
    }

    /**
     * 显示加载进度条（自定义message）
     *
     * @param message
     */
    override fun showLoading(message: String?) {
        if (activity.getWindow() != null && !activity.isFinishing()) {
            if (progressDialog == null) {
                progressDialog = LoadingDialog(activity)
                progressDialog!!.setLoadText(message)
            }
            progressDialog?.show()
            setProgressOnTouchOutside(false)
            setProgressNoDismiss()
        }
    }

    /**
     * 设置Progress是否手触消失
     *
     * @param onTouchOutside
     */
    open fun setProgressOnTouchOutside(onTouchOutside: Boolean) {
        if (progressDialog != null) progressDialog!!.setCanceledOnTouchOutside(onTouchOutside)
    }

    /**
     * 设置点击返回是否消失加载进度条
     */
    fun setProgressNoDismiss() {
        if (progressDialog != null && progressDialog!!.isShowing()) {
            progressDialog!!.setOnKeyListener(DialogInterface.OnKeyListener { dialog, keyCode, event ->
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    true
                } else false
            })
        }
    }

    /**
     * 消失加载进度条
     */
    override fun hideLoading() {
        try {
            if (activity.getWindow() != null && !activity.isFinishing()) {
                if (progressDialog != null && progressDialog!!.isShowing()) {
                    progressDialog!!.dismiss()
                    progressDialog = null
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            progressDialog = null
        }
    }

    override fun showToast(msg: String?) {
        if (!TextUtils.isEmpty(msg)) ToastUtil.show(MintsApplication.getContext(), msg)
    }

    open fun loadAd(activity: Activity, curCoin: Int, carrierType: String, extraId: String){}

    open fun onDestory(){}
}