package com.pan_american.android.data.model.group_common_modle.commission_confirm.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.RecyclerView
import com.pan_american.android.OASystem
import com.pan_american.android.R
import com.pan_american.android.data.model.group_common_modle.commission_confirm.entity.CommissionConfirmListItem

class CommissionConfirmListAdapter(private val itemList: ArrayList<CommissionConfirmListItem>): RecyclerView.Adapter<CommissionConfirmListAdapter.ViewHolder>() {

    lateinit var onItemClick: OnItemClick

    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view) {

        val groupName: TextView = view.findViewById(R.id.group_name)
        val customerLevel: TextView = view.findViewById(R.id.customer_level)
        val endDate: TextView = view.findViewById(R.id.end_date)
        val commissionAmount: TextView = view.findViewById(R.id.commission_amount)
        val commissionDetail: TextView = view.findViewById(R.id.commission_detail)
        val confirmStatus: TextView = view.findViewById(R.id.confirm_status)
        val confirmIcon: ImageView = view.findViewById(R.id.confirm_icon)
        val deliverStatus: TextView = view.findViewById(R.id.deliver_status)
        val deliverIcon: ImageView = view.findViewById(R.id.deliver_icon)
        val confirmCheck: TextView = view.findViewById(R.id.confirm_check)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        val view = LayoutInflater.from(OASystem.context).inflate(R.layout.item_commission_confirm_list, parent, false)

        val viewHolder = ViewHolder(view)

        viewHolder.commissionDetail.setOnClickListener {
            onItemClick.itemDetailClick(viewHolder.absoluteAdapterPosition)
        }

        viewHolder.confirmCheck.setOnClickListener {
            onItemClick.itemConfirmClick(viewHolder.absoluteAdapterPosition)
        }

        return viewHolder
    }

    override fun getItemCount() = itemList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val item = itemList[position]

        item.apply {
            holder.groupName.text = teamName
            holder.customerLevel.text = teamLvStr
            holder.endDate.text = visitDate.substring(0, 10)
            holder.commissionAmount.text = price.toString()

            when(isConfirm) {
                0 -> {
                    holder.confirmStatus.apply {
                        text = resources.getString(R.string.unconfirmed)
                        setTextColor(Color.rgb(23, 171, 227))
                    }

                    holder.confirmIcon.setImageResource(R.mipmap.icon_wait)
                }

                1 -> {
                    holder.confirmStatus.apply {
                        text = resources.getString(R.string.confirmed)
                        setTextColor(Color.rgb(84, 213, 77))
                    }

                    holder.confirmIcon.setImageResource(R.mipmap.icon_passed)
                }
            }

            when(isSeed) {
                0 -> {
                    holder.deliverStatus.apply {
                        text = resources.getString(R.string.undelivered)
                        setTextColor(Color.rgb(23, 171, 227))
                    }

                    holder.deliverIcon.setImageResource(R.mipmap.icon_wait)
                }

                1 -> {
                    holder.deliverStatus.apply {
                        text = resources.getString(R.string.delivered)
                        setTextColor(Color.rgb(84, 213, 77))
                    }

                    holder.deliverIcon.setImageResource(R.mipmap.icon_passed)
                }
            }

            when(isConfirm) {
                0 -> {
                    holder.confirmCheck.apply {
                        background = ResourcesCompat.getDrawable(resources, R.drawable.shape_corner_solid_blue, null)
                        text = resources.getString(R.string.commission_confirm)
                    }
                }

                1 -> {
                    holder.confirmCheck.apply {
                        background = ResourcesCompat.getDrawable(resources, R.drawable.shape_corner_reset_button, null)
                        text = resources.getString(R.string.confirmed)
                    }
                }
            }
        }

    }

    interface OnItemClick {

        fun itemDetailClick(position: Int)

        fun itemConfirmClick(position: Int)
    }
}