package com.ym.game.adapter

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.app.Activity
import android.graphics.Color
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.ym.library.listener.OnRecycleItemClickListener
import com.ym.library.base.BaseRecyclerAdapter
import com.ym.xynt.R
import com.ym.library.module.ClockInWithdrawEntity
import com.ym.library.utils.Utils

/**
 *Craeted by ${junqi.li}
 *Craeted by 2020/9/18
 */
class ClockInWithdrawAdpater :
    BaseRecyclerAdapter<ClockInWithdrawEntity.ClockInItemEntity, RecyclerView.ViewHolder> {

    private var mContext: Activity? = null
    private val listener: OnRecycleItemClickListener<ClockInWithdrawEntity.ClockInItemEntity>

    constructor(
        content: Activity?,
        listener: OnRecycleItemClickListener<ClockInWithdrawEntity.ClockInItemEntity>
    ) : super() {
        this.listener = listener
        mContext = content
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ClockInWithdrawHolder(
            LayoutInflater.from(mContext).inflate(R.layout.item_clock_in_withdraw2, parent, false)
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        if (mList.size > 0) {
            val data = mList[position]
            if (holder is ClockInWithdrawHolder) {

                holder.item_title.text = Html.fromHtml(data.title)
                holder.item_desc.text = Html.fromHtml(data.endDesc)
                holder.item_cash_tv.text = data.cashStr + "元";
                holder.item_progress_tv.text = "${data.currentNum}/${data.targetNum}"
                holder?.item_progress_bar.max = data?.targetNum ?: 10
                holder?.item_progress_bar.progress = data?.currentNum ?: 0
                if (data?.status == 2) {
                    holder.item_redpack_img.setImageResource(R.mipmap.clock_in_red_pack)
                } else {
                    holder.item_redpack_img.setImageResource(R.mipmap.clock_in_gray_red_pack)
                }

                holder?.item.setOnClickListener {
                    if (data?.status == 3 || data?.status == 4) {
                        if (data?.special == 1) {
                            Utils.showToast(mContext, "完成打卡可领取奖励")
                        } else if (data?.special == 2) {
                            Utils.showToast(mContext, "完成打卡可提现")
                        }
                    } else if (data.status == 6 || data.status == 2) {
                        Utils.showToast(mContext, "已领取")
                    }
                    if (data.status == 1 || data?.status == 8) {
                        listener?.onItemClick(it, position, mList[position])
                    }
                }

//                holder?.layoutRedPg?.setOnClickListener {
//                    if (data.status == 1 || data?.status == 8) {
//                        listener?.onItemClick(it, position, mList[position])
//                    }
//                }
//
//                holder?.ivRightStatus?.setOnClickListener {
//                    if (data.status == 1 || data?.status == 8) {
//                        listener?.onItemClick(it, position, mList[position])
//                    }
//                }
            }
        }


/*        if (mList.size > 0) {
            val data = mList[position]

            if (holder is ClockInWithdrawHolder) {
                holder?.tvDayNum.text = "第${data?.needClockNum}天"
                holder?.tvMoney.text =
                    Html.fromHtml("可提现${data?.cashStr}元")
                holder?.tvMoneyRedPg.text =
                    Html.fromHtml("${data?.cashStr}<small><small>元</small></small>")
                holder?.tvComplete.text = "${data.desc}"

                if (data.type == 1) {
                    holder?.layoutTip.visibility = View.VISIBLE
                } else {
                    holder?.layoutTip.visibility = View.GONE
                }

                //1 普通打卡 2 提现打卡 3 手机
                when (data?.special) {
                    1 -> {
                        //普通任务
                        holder?.tvDayNum?.setTextColor(Color.parseColor("#ED7624"))
                        holder?.tvAdNum.text =
                            Html.fromHtml("<font color=\"#8C856F\"><small>${data?.firstDesc}  </small><</font>${data?.middleDesc}<font color=\"#8C856F\"><small>  ${data?.endDesc}</small></font>")

                        holder?.tvMoney?.visibility = View.GONE

//                        if (data?.currentNum != null && data?.targetNum != null) {
//                            if (data?.currentNum!! >= data?.targetNum!!) {
//                                startAnimator(holder?.ivRightStatus)
//                            }
//                        }

                        if (data.status == 2) {
                            //当天任务，已完成
                            holder?.layoutCenter.visibility = View.GONE
                            holder?.ivRightStatus.visibility = View.GONE
                            holder?.layoutProgress.visibility = View.GONE
                            holder?.ivRedPgStatus.visibility = View.VISIBLE
                            holder?.tvComplete.visibility = View.VISIBLE
                            holder?.layoutRedPg.visibility = View.VISIBLE
                            holder?.tvMoneyRedPg.visibility = View.GONE


                            holder?.ivBg?.setImageResource(R.drawable.icon_clock_in_item_gray_bg)
                            holder?.ivRedPgStatus?.setImageResource(R.drawable.icon_clock_in_item_gray_pg_complete)
                            holder?.ivRedPgStatus.clearAnimation()
                            holder?.ivRightStatus.clearAnimation()

                        } else if (data?.status == 4) {
                            //当天任务，未完成打卡
                            holder?.layoutCenter.visibility = View.VISIBLE
                            holder?.ivRightStatus.visibility = View.VISIBLE
                            holder?.layoutProgress.visibility = View.VISIBLE
                            holder?.ivRedPgStatus.visibility = View.GONE
                            holder?.tvComplete.visibility = View.GONE
                            holder?.layoutRedPg.visibility = View.GONE
                            holder?.tvMoneyRedPg.visibility = View.GONE


                            holder?.ivBg?.setImageResource(R.drawable.icon_clock_in_item_bg)
                            holder?.ivRightStatus?.setImageResource(R.drawable.icon_clock_in_item_red_pg)

                            holder?.tvProgress.text = "${data?.currentNum}/${data?.targetNum}"
                            holder?.progressBar.max = data?.targetNum ?: 10
                            holder?.progressBar.progress = data?.currentNum ?: 0
                            holder.ivRedPgStatus.clearAnimation()
                            holder?.ivRightStatus.clearAnimation()

                        } else if (data?.status == 5) {
                            //未来任务（未解锁）
                            holder?.ivRightStatus.visibility = View.GONE
                            holder?.layoutProgress?.visibility = View.GONE
                            holder?.ivRedPgStatus.visibility = View.VISIBLE
                            holder?.layoutCenter.visibility = View.VISIBLE
                            holder?.layoutRedPg.visibility = View.VISIBLE
                            holder?.tvMoneyRedPg.visibility = View.GONE

                            holder?.ivBg?.setImageResource(R.drawable.icon_clock_in_item_gray_bg)
                            holder?.ivRedPgStatus?.setImageResource(R.drawable.icon_clock_in_item_red_pg_gray)
                            holder.ivRedPgStatus.clearAnimation()
                            holder?.ivRightStatus.clearAnimation()
                        } else if (data?.status == 8) {
                            holder?.layoutCenter.visibility = View.GONE
                            holder?.ivRightStatus.visibility = View.VISIBLE
                            holder?.layoutProgress.visibility = View.GONE
                            holder?.ivRedPgStatus.visibility = View.GONE
                            holder?.tvComplete.visibility = View.VISIBLE
                            holder?.layoutRedPg.visibility = View.GONE
                            holder?.tvMoneyRedPg.visibility = View.GONE


                            holder?.ivBg?.setImageResource(R.drawable.icon_clock_in_item_bg)
                            holder?.ivRightStatus?.setImageResource(R.drawable.icon_clock_in_item_red_pg)

//                            holder?.tvProgress.text = "${data?.currentNum}/${data?.targetNum}"
//                            holder?.progressBar.max = data?.targetNum ?: 10
//                            holder?.progressBar.progress = data?.currentNum ?: 0
                            holder.ivRedPgStatus.clearAnimation()

                            startAnimator(holder?.ivRightStatus)
                        }
                    }
                    2 -> {
                        //提现任务
                        holder?.ivBg?.setImageResource(R.drawable.icon_clock_in_item_yellow_bg)
                        holder?.tvAdNum.text =
                            Html.fromHtml("<font color=\"#8F2C0F\"><small>${data.firstDesc}  </small></font>${data.middleDesc}<font color=\"#8F2C0F\"><small>  ${data.endDesc}</small></font>")

                        if (data.status == 3 || data.status == 1) {
                            // 当天任务，未完成打卡
                            holder?.tvMoneyRedPg.visibility = View.VISIBLE
                            holder?.ivRedPgStatus.visibility = View.VISIBLE
                            holder?.tvMoney?.visibility = View.GONE


                            holder?.ivRedPgStatus?.setImageResource(R.drawable.icon_clock_in_item_red_pg_coin)

                            //完成任务 未领取
                            if (data.status == 1) {
                                holder?.layoutProgress.visibility = View.GONE
                                holder?.tvComplete?.visibility = View.VISIBLE
                                holder?.layoutCenter.visibility = View.GONE
                                startAnimator(holder.layoutRedPg)
                            } else {
                                holder?.layoutProgress.visibility = View.VISIBLE
                                holder?.tvComplete?.visibility = View.GONE
                                holder?.layoutCenter.visibility = View.VISIBLE
                                holder.ivRedPgStatus.clearAnimation()
                            }
                            holder?.ivRightStatus.clearAnimation()

                            holder?.tvProgress.text = "${data?.currentNum}/${data?.targetNum}"
                            holder?.progressBar.max = data?.targetNum ?: 10
                            holder?.progressBar.progress = data?.currentNum ?: 0

                        } else if (data.status == 6) {
                            //已完成打卡并已提现
                            holder?.layoutCenter.visibility = View.GONE
                            holder?.tvMoney?.visibility = View.GONE
                            holder?.tvMoneyRedPg?.visibility = View.VISIBLE
                            holder?.tvComplete.visibility = View.VISIBLE
                            holder?.ivRightStatus.clearAnimation()
                            holder.ivRedPgStatus.clearAnimation()
                            holder?.tvComplete.setTextColor(Color.parseColor("#8F2C0F"))
                            holder?.ivRedPgStatus?.setImageResource(R.drawable.icon_clock_in_item_red_pg_coin_complete)

                        } else if (data.status == 7) {
                            //未来任务，未解锁
                            holder?.layoutCenter.visibility = View.VISIBLE
                            holder?.tvMoney?.visibility = View.VISIBLE
                            holder?.tvComplete.visibility = View.GONE
                            holder?.layoutProgress.visibility = View.GONE
                            holder?.tvMoneyRedPg?.visibility = View.VISIBLE
                            holder?.ivRightStatus.clearAnimation()
                            holder.ivRedPgStatus.clearAnimation()
                            holder?.ivRedPgStatus?.setImageResource(R.drawable.icon_clock_in_item_red_pg_coin_uncomplete)

                        }

                        holder?.tvDayNum?.setTextColor(Color.parseColor("#ffffff"))
                        holder?.layoutRedPg.visibility = View.VISIBLE
                        holder?.ivRightStatus.visibility = View.GONE
                    }
                    3 -> {
                        holder?.tvAdNum.text =
                            Html.fromHtml("<font color=\"#8F2C0F\"><small>${data.firstDesc}  </small></font>${data.middleDesc}<font color=\"#8F2C0F\"><small>  ${data.endDesc}</small></font>")
                        holder?.tvDayNum?.setTextColor(Color.parseColor("#ffffff"))
                        holder?.ivRedPgStatus?.setImageResource(R.drawable.icon_clock_in_item_phone)
                        holder?.ivBg?.setImageResource(R.drawable.icon_clock_in_item_yellow_bg)
                        holder?.tvMoney.text = data.cashStr

                        holder?.layoutRedPg.visibility = View.VISIBLE
                        holder?.layoutCenter.visibility = View.VISIBLE
                        holder?.tvMoney?.visibility = View.VISIBLE

                        holder?.layoutProgress.visibility = View.GONE
                        holder?.ivRightStatus.visibility = View.GONE
                        holder?.tvMoneyRedPg.visibility = View.GONE

                        holder.ivRedPgStatus.clearAnimation()
                        holder?.ivRightStatus.clearAnimation()

                    }
                }

                holder?.itemView.setOnClickListener {
                    if (data?.status == 3 || data?.status == 4) {
                        if (data?.special == 1) {
                            Utils.showToast(mContext, "完成打卡可领取奖励")
                        } else if (data?.special == 2) {
                            Utils.showToast(mContext, "完成打卡可提现")
                        }
                    } else if (data.status == 6 || data.status == 2) {
                        Utils.showToast(mContext, "已领取")
                    }
                }

                holder?.layoutRedPg?.setOnClickListener {
                    if (data.status == 1 || data?.status == 8) {
                        listener?.onItemClick(it, position, mList[position])
                    }
                }

                holder?.ivRightStatus?.setOnClickListener {
                    if (data.status == 1 || data?.status == 8) {
                        listener?.onItemClick(it, position, mList[position])
                    }
                }
            }
        }*/
    }

    inner class ClockInWithdrawHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var item = itemView.findViewById<RelativeLayout>(R.id.clock_in_item_rl);
        var item_title = itemView.findViewById<TextView>(R.id.clock_in_item_title);
        var item_desc = itemView.findViewById<TextView>(R.id.clock_in_item_desc);
        var item_redpack_img = itemView.findViewById<ImageView>(R.id.clock_in_item_red_pack_img);
        var item_cash_tv = itemView.findViewById<TextView>(R.id.clock_in_item_red_pack_tv);
        var item_progress_tv = itemView.findViewById<TextView>(R.id.progress_clock_in_bar_tv);
        var item_progress_bar = itemView.findViewById<ProgressBar>(R.id.progress_clock_in_bar);
    }

    private fun startAnimator(imageView: View) {
        val oa: ObjectAnimator = ObjectAnimator.ofFloat(
            imageView,
            View.ROTATION,
            0F, 30F, -25F, 0F, 0F, 0F, 0F, 0F, 0F
        )
        oa.repeatCount = ValueAnimator.INFINITE
        oa.setDuration(2000).start()
    }

}