package com.ym.game.adapter


import android.app.Activity
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.xynt.R
import com.ym.game.view.DailyCashWithdrawPopupWindow
import com.ym.game.view.MainDialog
import com.ym.library.base.BaseRecyclerAdapter
import com.ym.library.module.DialyCashDataEntity
import com.ym.library.net.NetConfig
import com.ym.library.utils.JumpUtils
import com.ym.library.utils.Utils

class DailyCashAdapter :
    BaseRecyclerAdapter<DialyCashDataEntity, DailyCashAdapter.WateringTaskHolder> {

    private var type: Int = -1
    private var mContext: Activity? = null
    private var dailyCashWithdrawPopupWindow: DailyCashWithdrawPopupWindow? = null

    constructor(
        content: Activity?,
        type: Int,
        dailyCashWithdrawPopupWindow: DailyCashWithdrawPopupWindow

    ) : super() {
        mContext = content
        this.type = type
        this.dailyCashWithdrawPopupWindow = dailyCashWithdrawPopupWindow

    }

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

    override fun onBindViewHolder(holder: WateringTaskHolder, position: Int) {
        if (mList.size > 0) {
            val data = mList[position]
            holder.tvTitle.text = data.wdText
            holder.tvCashStr.text = "${data.cashStr}元"
            holder.tvDesc.text = data.desc
            holder.btnProgess.max = data.targetNum
            holder.btnProgess.setProgress(data.currentNum)
            holder.tvProgess.text =
                Html.fromHtml("<font color=\"#FC6002\">${data?.currentNum}</font>/${data.targetNum}")
            val layoutParams = RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                Utils.dip2px(76)
            )
            if (position == mList.size - 1) {
                layoutParams.setMargins(0, 0, 0, Utils.dip2px(20f))
            } else {
                layoutParams.setMargins(0, 0, 0, Utils.dip2px(8f))
            }
            holder.rlbg.layoutParams = layoutParams
            holder.itemView.setOnClickListener {

                if (data.type == 10) {
                    if (data.currentNum >= data.targetNum) {
                        dailyCashWithdrawPopupWindow?.dissmissview()
                        JumpUtils.jumpUserInfoActivity()

                    } else {
                        MainDialog.showDailyTrip(
                            mContext as Activity,
                            data.type,
                            data.targetNum,
                            View.OnClickListener {
                                dailyCashWithdrawPopupWindow?.dissmissview()
                                JumpUtils.h5NormalJump(
                                    "免费领奖",
                                    NetConfig.H5.CONFIRMURL,
                                    false,
                                    mContext
                                )
                            })
                    }
                } else if (data.type == 11) {
                    if (data.currentNum >= data.targetNum) {
                        dailyCashWithdrawPopupWindow?.dissmissview()
                        JumpUtils.jumpUserInfoActivity()
                    } else {
                        MainDialog.showDailyTrip(
                            mContext as Activity,
                            data.type,
                            data.targetNum,
                            View.OnClickListener {
                                dailyCashWithdrawPopupWindow?.dissmissview()
                                JumpUtils.h5Jump(
                                    "幸运刮卡",
                                    NetConfig.H5.guaguakaurl,
                                    false,
                                    mContext
                                )
                            })
                    }
                } else {

                    JumpUtils.jumpUserInfoActivity()
                }

            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WateringTaskHolder {
        return WateringTaskHolder(
            LayoutInflater.from(mContext).inflate(R.layout.item_daily_cash, parent, false)
        )
    }

    inner class WateringTaskHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvTitle: TextView = itemView.findViewById(R.id.tv_item_title)
        val btnProgess: ProgressBar = itemView.findViewById(R.id.progess_watering_item)
        val tvProgess: TextView = itemView.findViewById(R.id.tv_item_progess)
        val tvDesc: TextView = itemView.findViewById(R.id.id_tv_desc)
        val tvCashStr: TextView = itemView.findViewById(R.id.tv_cashStr)
        val rlbg: RelativeLayout = itemView.findViewById(R.id.ll_one)

    }


}