package com.mints.goodmoney.ui.activitys

import android.os.Bundle
import android.view.View
import com.mints.goodmoney.R
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.manager.TTNativeExpressManager
import com.mints.goodmoney.manager.TtCsjAdManager
import com.mints.goodmoney.mvp.model.WaterBean
import com.mints.goodmoney.mvp.presenters.WaterPresenter
import com.mints.goodmoney.ui.activitys.base.BaseActivity
import com.mints.goodmoney.ui.widgets.WaterView
import kotlinx.android.synthetic.main.activity_water.*

/**
 * 描述：喝水赚钱
 * 作者：孟崔广
 * 时间：2020/10/13 11:55
 */
class WaterActivity : BaseActivity(), View.OnClickListener, WaterView.WaterViewListener
        , com.mints.goodmoney.mvp.views.WaterView {

    private val waterPresenter by lazy { WaterPresenter() }
    private val ttNativeExpressManager by lazy { TTNativeExpressManager() }

    override fun getContentViewLayoutID() = R.layout.activity_water

    override fun isApplyKitKatTranslucency() = false

    override fun initViewsAndEvents() {
        waterPresenter.attachView(this)

        initListener()
    }

    override fun onResume() {
        super.onResume()

        initExpress()
        waterPresenter.getWaterMsg()
    }

    override fun onPause() {
        super.onPause()

        wvWater?.closeWaterViewTime()
    }

    override fun onDestroy() {
        super.onDestroy()

        // 取消水滴动画
        wvWater?.destoryAnim()
        wvWater?.closeWaterViewTime()

        ttNativeExpressManager.onTTAdDestroy()
        waterPresenter.detachView()
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.ivWaterBack -> {
                finish()
            }
        }
    }

    override fun clickWater(water: Int, waterCoin: String) {
        if (water == 0) {
            waterPresenter.clickForWater()
        }
    }

    override fun clickForWaterSuc(waterCoin: Int) {
        val bundle = Bundle()
        bundle.putInt(Constant.MAIN_CUR_COIN, waterCoin)
        bundle.putString(Constant.MAIN_CARRIER_TYPE, Constant.CARRIER_HOMEWATER)
        readyGo(AwardActivity::class.java, bundle)
    }

    override fun getWaterMsgHomeSuc(data: WaterBean) {
        wvWater.setWaterMaxProgress(data.canGetCoinMax)
        wvWater.setWaterProgress(data.complete, data.canGetCoinMax)
        // 喝水按钮状态
        if (data.isStatus) {
            wvWater.setTvDrinkBtnEnable()
        } else {
            wvWater.setTvDrinkBtnNone()
        }

        // 喝水时间
        if (data.nextClickTime > 0) {
            wvWater.setTvDrinkBtnText("喝水打卡")
            wvWater.closeWaterViewTime()
            wvWater.setWaterViewTime(this@WaterActivity, data.nextClickTime)
        } else {
            wvWater.setTvDrinkBtnText("喝水领金币")
            wvWater.setWaterViewTimeGone()
        }
    }

    private fun initListener() {
        ivWaterBack.setOnClickListener(this)
        // 喝水监听
        wvWater.setWaterViewListener(this)
    }

    /**
     * 初始化信息iyc
     */
    private fun initExpress() {
        try {
            ttNativeExpressManager.initTTAd( false)
            ttNativeExpressManager.getNativeExpressInit(TtCsjAdManager.TT_AD_NATIVEEXPRESS_WATER, flWaterAd, 320f, 0f)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}