package com.mints.goodmoney.ui.activitys

import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.View.OnLongClickListener
import android.view.ViewGroup
import android.webkit.WebChromeClient
import com.github.lzyzsd.jsbridge.BridgeHandler
import com.github.lzyzsd.jsbridge.CallBackFunction
import com.github.lzyzsd.jsbridge.DefaultHandler
import com.mints.goodmoney.BuildConfig
import com.mints.goodmoney.R
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.manager.UserManager
import com.mints.goodmoney.mvp.model.TurntableBean
import com.mints.goodmoney.ui.activitys.base.BaseActivity
import com.mints.library.utils.json.JsonUtil
import kotlinx.android.synthetic.main.activity_erase.*
import kotlinx.android.synthetic.main.header_layout.*

/**
 * 描述：刮刮乐
 * 作者：孟崔广
 * 时间：2020/10/15 15:48
 * 邮箱：mengcga@163.com
 */
class EraseActivity : BaseActivity(), View.OnClickListener{

    private val userManager by lazy { UserManager.getInstance() }
    private val carrierType = Constant.CARRIER_CHALLENGE_CARD

    override fun getContentViewLayoutID()= R.layout.activity_erase

    override fun isApplyKitKatTranslucency()=false

    override fun initViewsAndEvents() {
        tv_title.text = "刮刮乐"
        iv_left_icon.visibility = View.VISIBLE
        iv_left_icon.setImageResource(R.mipmap.ic_arrow_back)

        initListener()
        loadTurnUrl()
    }

    private fun initListener() {
        iv_left_icon.setOnClickListener(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (blEarseWebview != null) {
            val parent = blEarseWebview.parent as ViewGroup
            parent.removeView(blEarseWebview)
            blEarseWebview.removeAllViews()
            blEarseWebview.destroy()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            androidCallJsCancel()
            finish()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.iv_left_icon -> {
                androidCallJsCancel()

                finish()
            }
        }
    }

    /**
     * 加载转盘url
     */
    private fun loadTurnUrl() {
        getTurnUrl()
        blEarseWebview.setDefaultHandler(DefaultHandler())
        blEarseWebview.setWebChromeClient(WebChromeClient())
        blEarseWebview.setOnLongClickListener(OnLongClickListener { true })
        blEarseWebview.registerHandler("getEraseResult", BridgeHandler { data, function ->
            if(!isFinishing){
                val turntableBean: TurntableBean = JsonUtil.parseJson(data, TurntableBean::class.java)
                if (turntableBean != null) {
                    val bundle = Bundle()
                    bundle.putInt(Constant.MAIN_CUR_COIN, turntableBean.getCoin())
                    bundle.putString(Constant.MAIN_CARRIER_TYPE, carrierType)
                    bundle.putString(Constant.MAIN_EXTRA_ID, turntableBean.getKey())
                    readyGo(AwardActivity::class.java, bundle)

                    androidCallJsReferse()
                }
            }
        })
        blEarseWebview.setDefaultHandler(object : DefaultHandler() {
            override fun handler(data: String, function: CallBackFunction) {
                showToast(data)
            }
        })
    }

    /**
     * 获取url
     */
    private fun getTurnUrl() {
        val tokenID: String = userManager.getTokenID()
        val url: String = BuildConfig.MainIp+ "hang/card.html?carrierType=" + carrierType + "&token=" + tokenID
        blEarseWebview.loadUrl(url)
    }

    /**
     * 奖励成功刷新js界面
     */
    private fun androidCallJsReferse() {
        blEarseWebview.callHandler("eraseRefresh", "") {
        }
    }

    /**
     * 用户主动取消js调用
     */
    private fun androidCallJsCancel() {
        blEarseWebview.callHandler("eraseCancel", "") {
        }
    }
}