package com.module.backstage.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.module.backstage.R
import com.module.backstage.databinding.BackstageItemLocalAlarmClockBinding
import com.quyunshuo.androidbaseframemvvm.common.bean.LocalAlarmClockBean
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil

class LocalAlarmClockAdapter(var productList: ArrayList<LocalAlarmClockBean>?) :
    RecyclerView.Adapter<LocalAlarmClockAdapter.MyViewHolder>() {
    private val TAG = "HomeProductAdapter"
    private var itemListener: AdapterClickListener? = null
    var UPDATE = "1"
    var DELETE = "2"

    inner class MyViewHolder(binding: BackstageItemLocalAlarmClockBinding) :
        RecyclerView.ViewHolder(binding.root) {
        private val mBinding = binding

        fun bind(localAlarmClockBean: LocalAlarmClockBean, position: Int) {
            mBinding.run {
                tvFunction.text = UiUtil.getStringRes(
                    UiUtil.getResId(
                        localAlarmClockBean.type?.content,
                        R.string::class.java
                    )
                )
                tvTime.text = localAlarmClockBean.hour + ":" + localAlarmClockBean.minute
                var weekStr = ""
                localAlarmClockBean.week?.forEach {
                    weekStr += UiUtil.getStringRes(
                        UiUtil.getResId(
                            it.content,
                            R.string::class.java
                        )
                    ) + " "
                }
                tvWeek.text = weekStr
                tvDelete.setOnClickListener {
                    itemListener?.onClickListener(it, position, DELETE)
                }
                tvUpdate.setOnClickListener {
                    itemListener?.onClickListener(it, position, UPDATE)

                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val projectVH = MyViewHolder(
            BackstageItemLocalAlarmClockBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
        return projectVH
    }

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

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(productList!![position], position)
    }

    fun setItemListener(itemListener: AdapterClickListener?) {
        this.itemListener = itemListener
    }

    fun setDatas(newproductList: ArrayList<LocalAlarmClockBean>) {
        newproductList?.let {
            productList = it
            notifyDataSetChanged()
        }
    }
}