package com.ym.modulecommon.news

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.text.TextUtils
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.bumptech.glide.Glide
import com.ym.admodule.R
import com.ym.modulecommon.AdConstant
import com.ym.modulecommon.AppliContext
import com.ym.modulecommon.base.BaseActivity
import com.ym.modulecommon.listener.OnRecycleItemClickListener
import com.ym.modulecommon.module.WithdrawResultEntity
import com.ym.modulecommon.news.adapter.AdNewsHomeAdapter
import com.ym.modulecommon.news.contract.AdNewsHomeContract
import com.ym.modulecommon.news.entity.AdNewsHomeEntity
import com.ym.modulecommon.news.presenter.AdNewsHomePresenter
import com.ym.modulecommon.utils.AdWebDialog
import com.ym.modulecommon.utils.Utils
import kotlinx.android.synthetic.main.activity_ad_news_home.*

class AdNewsHomeActivity : BaseActivity(), AdNewsHomeContract.View {

    private var mPresenter: AdNewsHomePresenter? = null
    private var adapter: AdNewsHomeAdapter? = null

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

    override fun init() {
        mPresenter = AdNewsHomePresenter(this)

        id_news_home_close?.setOnClickListener { finish() }

        id_ad_news_home_red_pg?.setOnClickListener {
            mPresenter?.getDoWd()
        }

        val lp = ad_news_home_bar_layout.getLayoutParams() as LinearLayout.LayoutParams
        lp.topMargin = Utils.getStatusBarHeight()
        ad_news_home_bar_layout.layoutParams = lp

        recycler_ad_news_home.layoutManager = GridLayoutManager(this, 2)
        adapter = AdNewsHomeAdapter(this)
        recycler_ad_news_home.adapter = adapter

    }

    override fun homeDataResult(result: AdNewsHomeEntity?) {
        if (result != null) {

            if (!TextUtils.isEmpty(result?.currencyIcon) && result?.currencyIcon?.contains("http")!!) {
                adapter?.setImgUrl(result?.currencyIcon?:"")
                Glide.with(AppliContext.get()).load(result?.currencyIcon)
                    .error(R.drawable.icon_ad_news_home_fu_big).into(id_icon_ad_news_home_fu)
            } else {
                id_icon_ad_news_home_fu?.setImageResource(R.drawable.icon_ad_news_home_fu_big)
            }

            AdConstant.WebTheme.textColor = result?.textColor
            AdConstant.WebTheme.themeColor = result?.themeColor

            id_news_home_title?.text = "集${result?.unit}得红包"
            id_news_home_title2?.text = "集${result?.unit} 得红包"
            id_ad_news_progress_title?.text = "集满${result.totalNumber}个${result?.unit}即可提现"
            id_ad_news_progress_bar?.max = result.totalNumber?:0
            id_ad_news_progress_bar?.progress = result.completeNumber?:0

            if (result?.isWithdrawal == 1) {
                startAnimator()
            } else {
                mObjectAnimator?.cancel()
                id_ad_news_home_red_pg?.clearAnimation()
            }

            if (result?.isGetCard == 1) {
                AdWebDialog.showHomeReward(this,result,object :View.OnClickListener {
                    override fun onClick(p0: View?) {
                        mPresenter?.getHomeData()
                    }
                })
            }

            adapter?.clear()
            adapter?.setAwardNum(result?.awardNum?:1)
            adapter?.appendToList(result?.newsList)
            adapter?.notifyDataSetChanged()
        }
    }

    override fun withdrawResult(result: WithdrawResultEntity?) {
        mPresenter?.getHomeData()
        if (result != null) {
            Utils.showToast(this,result?.reason)
        }
    }

    private var isPause = false

    override fun onResume() {
        super.onResume()
        if (isPause) {
            mPresenter?.getHomeData()
        }
    }

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

    var mObjectAnimator: ObjectAnimator? = null
    private fun startAnimator() {
        mObjectAnimator = ObjectAnimator.ofFloat(
            id_ad_news_home_red_pg,
            View.ROTATION,
            0F, 30F, -25F, 0F, 0F, 0F, 0F, 0F, 0F
        )
        mObjectAnimator?.repeatCount = ValueAnimator.INFINITE
        mObjectAnimator?.setDuration(2000)?.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        mObjectAnimator?.cancel()
        id_ad_news_home_red_pg?.clearAnimation()
    }


}