package com.ym.game.activity

import android.app.Dialog
import android.text.Html
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.ym.xynt.R
import com.ym.game.adapter.ClockInWithdrawAdpater
import com.ym.game.contract.ClockInWithdrawContract
import com.ym.game.presenter.ClockInWithdrawPresenter
import com.ym.game.view.MainDialog
import com.ym.library.MyConstant
import com.ym.library.listener.OnRecycleItemClickListener
import com.ym.library.module.ClockInWithdrawEntity
import com.ym.library.module.ExchangeEntity
import com.ym.library.net.MyBaseActivity
import com.ym.library.utils.EventUtils
import kotlinx.android.synthetic.main.activity_clock_in_withdraw.*

//打卡提现  福利
class ClockInWithdrawActitivy : MyBaseActivity(), ClockInWithdrawContract.View,
    OnRecycleItemClickListener<ClockInWithdrawEntity.ClockInItemEntity> {

    private var mPresenter: ClockInWithdrawPresenter? = null
    private var mAdapter: ClockInWithdrawAdpater? = null
    private var mData: ClockInWithdrawEntity.ClockInItemEntity? = null
    private var isClick = false

    override fun layoutID(): Int {
        return R.layout.activity_clock_in_withdraw
    }

    override fun init() {
        EventUtils.onEvent("daka_show")
        mPresenter = ClockInWithdrawPresenter(this)
        iv_clock_in_close?.setOnClickListener {
            finish()
        }

        initAdapter()
        mPresenter?.getClockInWithdrawList()
    }

    private fun initAdapter() {
        recycler_clock_in?.layoutManager = LinearLayoutManager(this)
        mAdapter = ClockInWithdrawAdpater(this, this)
        recycler_clock_in?.adapter = mAdapter
    }

    override fun onItemClick(
        view: View,
        position: Int,
        data: ClockInWithdrawEntity.ClockInItemEntity
    ) {
        mData = data
        if (!isClick) {
            isClick = true
            if (data.special == 1) {
                EventUtils.onEvent("daka_hongbao_click")
                mPresenter?.getFlyBoxReceive(this)
            } else if (data.special == 2) {
                EventUtils.onEvent("daka_tixian_click")
                mPresenter?.getDoWd(data.id!!)
            }

            view?.postDelayed({
                isClick = false
            },1000)
        }

    }

    //任务列表结果
    override fun clockInWithdrawListResult(result: ClockInWithdrawEntity?) {
        if (result != null) {
            MyConstant.videoNum = result?.videoNum ?: 20

//            tv_clock_in_days?.text =
//                Html.fromHtml("已连续打卡 <font color=\"#EF680D\"><big>${result?.clockNum}</big></font> 天")
            tv_clock_in_days?.text =
                Html.fromHtml(result.clockTitle)
            tv_clock_in_info?.text = "${result?.todayTitle}"

            mAdapter?.clear()
            mAdapter?.appendToList(result?.clockData)
            mAdapter?.notifyDataSetChanged()
        }
    }

    //提现结果
    override fun dowdResult(result: ExchangeEntity?) {
        mPresenter?.getClockInWithdrawList()
        if (result != null) {
            MainDialog?.showWithDraw(
                this,
                result?.status?:0,
                result?.title?:"",
                result?.reason?:"",
                result?.buttonText?:"",
                result?.buttonStatus?:0,
                null,
                8,
                null,
                true,
                Dialog(this)
            )
        }
    }

}