package com.ym.game.activity

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.app.Dialog
import android.content.DialogInterface
import android.view.View
import android.view.animation.AnimationUtils
import androidx.recyclerview.widget.LinearLayoutManager
import com.ym.xynt.R
import com.ym.game.adapter.WelfareDiamondAdapter
import com.ym.game.adapter.WelfareTaskAdpater
import com.ym.game.contract.WelfareContract
import com.ym.game.listener.IDialogViewCloseCallback
import com.ym.game.presenter.WelfarePresenter
import com.ym.game.view.MainDialog
import com.ym.library.listener.IAdVideoListener
import com.ym.library.listener.OnRecycleItemClickListener
import com.ym.library.module.*
import com.ym.library.net.MyBaseActivity
import com.ym.library.utils.*
import kotlinx.android.synthetic.main.activity_welfare.*

class WelfareActivity : MyBaseActivity(), WelfareContract.View,
    OnRecycleItemClickListener<NewsEntity> {

    private var mPresenter: WelfarePresenter? = null
    private var diamonAdapter: WelfareDiamondAdapter? = null
    private var taskAdapter: WelfareTaskAdpater? = null

    private var mDiamonList: MutableList<DiamondEntity> = ArrayList()
    private var mData: NewsEntity? = null
    private var mDiamondEntity: DiamondEntity? = null
    private var isClick = false

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

    override fun init() {
        EventUtils.onEvent("fuli_show")
        mPresenter = WelfarePresenter(this)
        iv_welfare_close?.setOnClickListener {
            finish()
        }

        iv_welfare_reward_box?.setOnClickListener {
            EventUtils.onEvent("welfare_task_reward_box_click")
            if (mDiamondEntity?.coins != null && mDiamondEntity?.max != null) {
                if (mDiamondEntity?.coins!! >= mDiamondEntity?.max!!) {
                    mPresenter?.getWelfareTaskBox()
                } else {
                    var lack = mDiamondEntity?.max!! - mDiamondEntity?.coins!!
                    MainDialog?.showWithDraw(
                        this,
                        3,
                        "",
                        "需要钻石${mDiamondEntity?.max}颗，还差${lack}颗，快去做任务领钻石吧",
                        "",
                        0,
                        null,
                        0,
                        null,
                        true,
                        Dialog(this)
                    )
                }

            }

        }

        iv_welfare_reward_red_pg?.setOnClickListener {
            EventUtils.onEvent("welfare_task_reward_red_package_click")
            mPresenter?.getWelfareTaskCash()
        }
        initAdapter()
        request()
    }

    private fun request() {
        mPresenter?.getDiamondList()
        mPresenter?.getTaskList()
    }

    private fun initAdapter() {
        //钻石列表
//        diamonAdapter = WelfareDiamondAdapter(this,object :OnRecycleItemClickListener<DiamondEntity> {
//            override fun onItemClick(view: View, position: Int, data: DiamondEntity) {
//                mPresenter?.getWelfareTaskBox()
//            }
//        })
//        recycler_welfare_diamond_reward?.layoutManager = GridLayoutManager(this, 7)
//        recycler_welfare_diamond_reward?.adapter = diamonAdapter

        taskAdapter = WelfareTaskAdpater(this, this)
        recycler_welfare_task.layoutManager = LinearLayoutManager(this)
        recycler_welfare_task?.adapter = taskAdapter
    }

    private var mSid: Int? = 0
    override fun onItemClick(view: View, position: Int, data: NewsEntity) {
        mData = data
        if (!isClick) {
            isClick = true
            //0未完成 1已领取 2可领取
            when (data.status) {
                0 -> {
                    EventUtils.onEvent("fuli_go_click_${data.sid}")
                    if (data.opentype == NewsEntity.AD_TYPE_SDK) {
                        if (data.sid != null) {

                            mSid = data.sid
//                            showAd(data.sid)
                            mPresenter?.getADShowTimer()
                            EventUtils.onEvent("welfare_task_undone_click_total")
                        }
                    } else {
                        JumpUtils.adJump(data, this)
                    }
                }
                1 -> {
                    Utils.showToast(this, "已领取")
                }
                2 -> {
                    if (data.opentype == NewsEntity.AD_TYPE_SDK) {
                        EventUtils.onEvent("welfare_task_done_click_total")
                    }
                    EventUtils.onEvent("fuli_done_click_${data.sid}")
                    mPresenter?.getTaskReceiveList(data?.sid)
                }
            }
            view?.postDelayed({
                isClick = false
            }, 1000)
        }

    }

    //钻石列表结果
    override fun diamondListResult(result: DiamondEntity?) {
        if (result != null) {
            mDiamondEntity = result
            mDiamonList.clear()
            tv_welfare_diamond_progress?.text = "${result?.coins}/${result?.max}"
            progess_watering_bar?.max = result?.max ?: 10
            progess_watering_bar?.progress = result?.coins ?: 0

            if (result?.display != null && result?.display == 1) {
                tv_welfare_diamond_conut?.text = "集齐${result?.max}颗钻石可开宝箱"
                iv_welfare_reward_red_pg?.visibility = View.GONE
                iv_welfare_reward_box?.visibility = View.VISIBLE

                clearAnimation()
                if (result?.coins != null && result?.max != null && result?.coins!! >= result?.max!!) {
                    iv_welfare_reward_box?.setImageResource(R.drawable.icon_welfare_box_open)
                    val oa: ObjectAnimator = ObjectAnimator.ofFloat(
                        iv_welfare_reward_box,
                        View.ROTATION,
                        0F, 30F, -25F, 0F, 0F, 0F, 0F, 0F, 0F
                    )
                    oa.repeatCount = ValueAnimator.INFINITE
                    oa.setDuration(2000).start()
                } else {
                    iv_welfare_reward_box?.animation =
                        AnimationUtils.loadAnimation(this, R.anim.scale_anim)
                    iv_welfare_reward_box?.setImageResource(R.drawable.icon_welfare_box_close)
                }
            } else {
                iv_welfare_reward_red_pg?.visibility = View.VISIBLE
                iv_welfare_reward_box?.visibility = View.GONE
                tv_welfare_diamond_conut?.text = "集齐${result?.max}颗钻石可提现"
                iv_welfare_reward_red_pg?.animation =
                    AnimationUtils.loadAnimation(this, R.anim.scale_anim)

            }

            /*var i = 0
            while (i < 7) {
                var data = DiamondEntity()
                data.coins = result.coins
                data.max = result.max
                if (i < result?.coins!!) {
                    data.show = 1
                } else {
                    data.show = 0
                }
                mDiamonList.add(data)
                i++
            }
            diamonAdapter?.clear()
            diamonAdapter?.appendToList(mDiamonList)
            diamonAdapter?.notifyDataSetChanged()*/

        }
    }

    //任务列表结果
    override fun taskListResult(result: TaskListEntity?) {
        if (result != null) {
            taskAdapter?.clear()
            taskAdapter?.appendToList(result?.userTask!!)
            taskAdapter?.notifyDataSetChanged()
        }
    }

    //任务领取结果
    override fun taskReceiveListResult(result: TaskCompleteEntity?) {
        request()
        if (result != null) {
            EventUtils.onEvent("welfare_task_receive_dialog_show")

            CenterDialog.showgetWaterDrop(
                this,
                3,
                mData?.coins?.toInt()!!,
                "welfare_task_receive",
                DialogInterface.OnCancelListener {

                },
                null
            )
        }
    }

    //领取宝箱结果
    override fun taskCashResult(result: List<FlyBoxEntity>?) {
        if (result != null && result.isNotEmpty()) {
            mPresenter?.getDiamondList()
            MainDialog.showWelfareOpenBox(this, result, object : IDialogViewCloseCallback {
                override fun onCloseViewSuccess(isSuccess: Boolean) {

                }
            })
        }
    }

    //领红包结果
    override fun welfareTaskCashResult(result: ExchangeEntity?) {
        if (result != null) {
            mPresenter?.getDiamondList()
            MainDialog?.showWithDraw(
                this,
                result?.status ?: 0,
                result?.title ?: "",
                result?.reason ?: "",
                result?.buttonText ?: "",
                result?.buttonStatus ?: 0,
                null,
                7,
                null,
                true,
                Dialog(this)
            )
        }
    }

    override fun taskTimeCd(time: Int?) {
        if (time == 0 && mSid != null && mSid != 0) {
            showAd(mSid!!)
        } else {
            Utils.showToast(this, "手速太快啦，${time}秒后再来吧")
        }
    }

    fun showAd(sId: Int) {
        AdUtils.playRewardAd(this, "welfare_task", object : IAdVideoListener {
            override fun onAdClose() {
                mPresenter?.getWelfareVideoReport(sId)
            }

            override fun onError(errorMsg: String?) {

            }
        })
    }

    private var isPause = false

    override fun onResume() {
        super.onResume()
        if (isPause) {
            request()
            isPause = false
        }

    }

    override fun onPause() {
        super.onPause()
        isPause = true
    }

    private fun clearAnimation() {
        iv_welfare_reward_box?.clearAnimation()
        iv_welfare_reward_red_pg?.clearAnimation()
    }

    override fun onDestroy() {
        super.onDestroy()
        clearAnimation()
        taskAdapter?.clearAnimation()
    }
}