package com.pan_american.android.data.model.common.adapter

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_management.group_info.entity.GroupInfoSimple

class GroupInfoSimpleAdapter(private val groupInfoList: ArrayList<GroupInfoSimple>): RecyclerView.Adapter<GroupInfoSimpleAdapter.ViewHolder>() {

    lateinit var onRecyclerViewItemClick: OnRecyclerViewItemClick<GroupInfoSimple>

    //是否有删除权限
    private var canDelete = OASystem.authorization(OASystem.GROUP_INFORMATION, OASystem.DELETE)

    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val groupName: TextView = view.findViewById(R.id.group_name)
        val deleteButton: ImageView = view.findViewById(R.id.delete)
        val groupType: TextView = view.findViewById(R.id.group_type)
        val customerUnit: TextView = view.findViewById(R.id.customer_unit)
        val customerName: TextView = view.findViewById(R.id.customer_name)
        val customerLevel: TextView = view.findViewById(R.id.customer_level)
        val visitTime: TextView = view.findViewById(R.id.visit_time)
        val visitDays: TextView = view.findViewById(R.id.visit_days)
        val visitMembers: TextView = view.findViewById(R.id.visit_members)
        val groupAffiliation: TextView = view.findViewById(R.id.group_affiliation)
        val groupStatus: TextView = view.findViewById(R.id.group_status)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_group_info_list, parent, false)

        val viewHolder = ViewHolder(view)

        viewHolder.deleteButton.setOnClickListener {
            onRecyclerViewItemClick.onItemDelete(viewHolder.bindingAdapterPosition)
        }

        viewHolder.groupStatus.setOnClickListener {
            onRecyclerViewItemClick.changeGroupStatus(viewHolder.bindingAdapterPosition)
        }

        viewHolder.itemView.setOnClickListener {
            onRecyclerViewItemClick.onItemClick(viewHolder.bindingAdapterPosition)
        }

        return viewHolder
    }

    override fun getItemCount() = groupInfoList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        groupInfoList[position].apply {

            holder.groupName.text = teamName
            holder.groupType.text = teamType
            holder.customerUnit.text = clientUnit
            holder.customerName.text = clientName
            holder.customerLevel.text = teamLev
            holder.visitTime.text = visitDate.substring(0, 10)
            holder.visitDays.text = visitDays.toString()
            holder.visitMembers.text = visitPNumber.toString()
            holder.groupAffiliation.text = department

            holder.groupStatus.apply {
                if (tourCode.isBlank()) {
                    text = resources.getString(R.string.confirm_group_depart)
                } else {
                    background = ResourcesCompat.getDrawable(resources, R.drawable.shape_corner_reset_button, null)
                    text = resources.getString(R.string.group_departed)
                }
            }
        }

        if (!canDelete) {
            holder.deleteButton.visibility = View.GONE
        }

    }

    interface OnRecyclerViewItemClick<T> {
        fun onItemClick(position: Int)
        fun onItemDelete(position: Int)
        fun changeGroupStatus(position: Int)
    }
}