package com.gh.gamecenter.forum.moderator

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.ItemModeratorTaskBinding
import com.gh.gamecenter.entity.ApplyModeratorTaskEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class ModeratorTaskAdapter(context: Context) :
    BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    val taskList = ArrayList<ApplyModeratorTaskEntity>()

    fun setListData(data: ArrayList<ApplyModeratorTaskEntity>) {
        taskList.clear()
        taskList.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ModeratorTaskItemViewHolder(
            ItemModeratorTaskBinding.bind(
                mLayoutInflater.inflate(R.layout.item_moderator_task, parent, false)
            )
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ModeratorTaskItemViewHolder) {
            val taskEntity = taskList[position]
            holder.binding.run {
                taskNameTv.text = taskEntity.taskName
                taskIconIv.setImageDrawable(taskEntity.taskIcon.toDrawable())
                finishTv.background =
                    if (taskEntity.finishedTask) R.drawable.bg_moderator_task_disabled.toDrawable(mContext) else R.drawable.download_button_normal_style.toDrawable(
                        mContext
                    )
                finishTv.text = if (taskEntity.finishedTask) "已完成" else "去完成"
                finishTv.setTextColor(
                    if (taskEntity.finishedTask) R.color.text_subtitleDesc.toColor(mContext) else R.color.white.toColor(
                        mContext
                    )
                )
                finishTv.setOnClickListener(taskEntity.taskAction)
                taskDesTv.setOnClickListener(taskEntity.taskDesAction)

                if (!TextUtils.isEmpty(taskEntity.taskDescription)) {
                    taskDesTv.visibility = View.VISIBLE
                    taskDesTv.text = taskEntity.taskDescription
                } else {
                    taskDesTv.visibility = View.GONE
                }
            }
        }
    }

    override fun getItemCount(): Int = taskList.size

    class ModeratorTaskItemViewHolder(val binding: ItemModeratorTaskBinding) :
        BaseRecyclerViewHolder<ApplyModeratorTaskEntity>(binding.root)
}