package com.ym.modulecommon.news.adapter

import android.app.Activity
import android.graphics.Color
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.ym.admodule.R
import com.ym.modulecommon.AppliContext
import com.ym.modulecommon.base.BaseRecyclerAdapter
import com.ym.modulecommon.news.entity.AdNewsHomeEntity
import com.ym.modulecommon.utils.AdJumpUtils

class AdNewsHomeAdapter :
    BaseRecyclerAdapter<AdNewsHomeEntity.News, AdNewsHomeAdapter.ViewViewHolder> {

    private var activity:Activity

    private var awardNum = 1
    private var imgUrl = ""

    constructor(activity: Activity) : super() {
        this.activity = activity
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewViewHolder {
        return ViewViewHolder(
            LayoutInflater.from(parent?.context)
                .inflate(R.layout.item_ad_news_home, parent, false)
        )
    }

    override fun onBindViewHolder(holder: ViewViewHolder, position: Int) {
        if (mList.size > 0) {
            val data = mList[position]

            holder?.mTitle.text = data.title
            holder?.mAwardNum.text = "x${awardNum}"

            //0任务未开始 1未完成 2已完成
            when (data?.status) {
                0 -> {
                    holder?.mStatus.text = "去完成"
                    holder?.mStatus.setTextColor(Color.parseColor("#E9373A"))
                    holder?.mStatus.setBackgroundResource(R.drawable.shape_e9373a_r12_2)
                }
                1 -> {
                    holder?.mStatus.text = "进行中"
                    holder?.mStatus.setTextColor(Color.parseColor("#FFFFFF"))
                    holder?.mStatus.setBackgroundResource(R.drawable.shape_e9373a_r12)
                }
                2 -> {
                    holder?.mStatus.text = "已完成"
                    holder?.mStatus.setTextColor(Color.parseColor("#7A7C7D"))
                    holder?.mStatus.setBackgroundResource(R.drawable.shape_c5c6c7_r12)
                }
            }

            if (!TextUtils.isEmpty(imgUrl) && imgUrl?.contains("http")) {
                Glide.with(AppliContext.get()).load(imgUrl)
                    .error(R.drawable.icon_ad_news_home_fu_big).into(holder.mImg)
            } else {
                holder?.mImg.setImageResource(R.drawable.icon_ad_news_home_fu_big)
            }

            holder?.itemView?.setOnClickListener {
                if (data?.status != 2) {
                    AdJumpUtils.jumpAdNewsWebActivity(activity,data?.newsUrl,data?.id)
                }
            }
        }
    }

    fun setAwardNum(awardNum: Int) {
        this.awardNum = awardNum
    }

    fun setImgUrl(imgUrl: String) {
        this.imgUrl = imgUrl
    }

    class ViewViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mImg: ImageView
        val mAwardNum: TextView
        val mTitle: TextView
        val mStatus: TextView

        init {
            mImg = itemView.findViewById(R.id.id_item_news_home_fu_img)
            mAwardNum = itemView.findViewById(R.id.id_item_news_home_num)
            mTitle = itemView.findViewById(R.id.id_item_news_home_title)
            mStatus = itemView.findViewById(R.id.id_item_news_home_status)
        }
    }

}