package com.mints.goodmoney.ui.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.mints.goodmoney.R
import com.mints.goodmoney.mvp.model.MyInfo
import com.mints.goodmoney.ui.adapter.listener.OnItemChildClickListener
import com.mints.goodmoney.ui.adapter.listener.OnItemClickListener

class MainMyAdapter(context: Context, taskData: MutableList<MyInfo.AutoListBean>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        // 跳转首页
        const val TO_HOME = "TO_HOME"

        // 邀请好友
        const val APP_SHAREFRIEND = "APP_SHAREFRIEND"

        // 看福利视频
        const val APP_VEDIO = "APP_VEDIO"

        // 试玩平台
        const val TO_YWHZ = "TO_YWHZ"

        // 玩游戏，赚金币
        const val TO_LIEBAO = "TO_LIEBAO"
    }

    private var mContext: Context = context
    private var taskData: List<MyInfo.AutoListBean>? = taskData

    private var mOnItemClickListener: OnItemClickListener? = null
    private var mOnItemChildClickListener: OnItemChildClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater: LayoutInflater = LayoutInflater.from(mContext)
        return TaskHolder(inflater.inflate(R.layout.item_fragment_main_my_task, parent, false))
    }

    override fun getItemCount(): Int {
        return if (taskData == null) {
            0
        } else {
            taskData!!.size
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onItemClick(it, position)
        }
        taskData?.let {
            val taskBean = it[position]
            if (holder is TaskHolder) {
                holder.itemTaskClick.setOnClickListener { it2 ->
                    mOnItemChildClickListener?.onItemChildClick(it2, position)
                }
                // 标题
                holder.itemTaskTitle.text = taskBean.baseConfig.title
                // 内容
                holder.itemTaskContent.text = taskBean.baseConfig.context
                // 按钮文字
                holder.itemTaskClick.text = taskBean.baseConfig.buttonText
                // 金币
                holder.itemTaskMoney.text = " " + taskBean.baseConfig.coinText
//                holder.itemTaskImg.setImageDrawable(mContext.resources.getDrawable(taskBean.imageSrc))
                if (taskBean.baseConfig.buttonText == null) {
                    holder.itemTaskClick.visibility = View.GONE
                } else {
                    holder.itemTaskClick.visibility = View.VISIBLE
                    // 按钮颜色
                    if (taskBean.otherConfig != null) {
                        when (taskBean.otherConfig.status) {
                            1 -> {
                                holder.itemTaskClick.setBackgroundResource(R.drawable.shape_btn_orange_complete)
                                holder.itemTaskClick.setTextColor(Color.WHITE)
                            }
                            2 -> {
                                if (taskBean.baseConfig.taskId == APP_SHAREFRIEND) {
                                    holder.itemTaskClick.setBackgroundResource(R.drawable.shape_btn_orange)
                                    holder.itemTaskClick.setTextColor(mContext.resources.getColor(R.color.my_color_orange))
                                } else {
                                    holder.itemTaskClick.setBackgroundResource(R.drawable.shape_btn_gury)
                                    holder.itemTaskClick.setTextColor(Color.WHITE)
                                }
                            }
                            0 -> {
                                holder.itemTaskClick.setBackgroundResource(R.drawable.shape_btn_orange)
                                holder.itemTaskClick.setTextColor(mContext.resources.getColor(R.color.my_color_orange))
                            }
                        }
                    } else {
                        holder.itemTaskClick.setBackgroundResource(R.drawable.shape_btn_orange)
                        holder.itemTaskClick.setTextColor(mContext.resources.getColor(R.color.my_color_orange))
                    }
                }

                // 进度条
                if (taskBean.otherConfig != null) {
                    if (taskBean.baseConfig != null) {
                        holder.itemTaskPb.visibility = View.VISIBLE
                        holder.itemTaskPb.progress = taskBean.otherConfig.complete
                        holder.itemTaskPb.max = taskBean.otherConfig.max
                    } else {
                        holder.itemTaskPb.visibility = View.GONE
                    }
                } else {
                    holder.itemTaskPb.visibility = View.GONE
                }
                if (taskBean.baseConfig.runingText != null) {
                    holder.itemTaskProgress.visibility = View.VISIBLE
                    holder.itemTaskProgress.text = taskBean.baseConfig.runingText
                } else {
                    holder.itemTaskProgress.visibility = View.GONE
                }

            }
        }

    }

    private inner class TaskHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        //        val itemTaskImg: ImageView = itemView.findViewById(R.id.item_title_img)
        val itemTaskTitle: TextView = itemView.findViewById(R.id.item_task_title)
        val itemTaskMoney: TextView = itemView.findViewById(R.id.item_task_money)
        val itemTaskProgress: TextView = itemView.findViewById(R.id.item_task_progress)
        val itemTaskClick: TextView = itemView.findViewById(R.id.item_task_click)
        val itemTaskContent: TextView = itemView.findViewById(R.id.item_task_content)
        val itemTaskPb: ProgressBar = itemView.findViewById(R.id.item_task_pb)
    }

    fun setOnItemChildClickListener(listener: OnItemChildClickListener) {
        mOnItemChildClickListener = listener
    }


}