package com.pan_american.android.data.model.group_op.ground_convey_payment_insert.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.pan_american.android.OASystem
import com.pan_american.android.R

class GroundConveyHeaderAdapter: RecyclerView.Adapter<GroundConveyHeaderAdapter.HeaderViewHolder>() {

    private var groupNameStr = ""

    private var currencyCode = ""

    private var canBeEdit = true

    lateinit var onItemClick: OnItemClick

    inner class HeaderViewHolder(view: View): RecyclerView.ViewHolder(view) {
        private val groupName: TextView = view.findViewById(R.id.group_name)
        val paymentCurrency: TextView = view.findViewById(R.id.payment_currency)
        val paymentType: LinearLayout = view.findViewById(R.id.payment_type)

        fun bindGroupName(groupName: String) {
            this.groupName.text = groupName
        }

        fun bindPaymentCurrencyCode(code: String) {
            this.paymentCurrency.text = code
        }
    }

    override fun getItemViewType(position: Int): Int {
        return R.layout.header_ground_convey_payment_detail
    }

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

        val headerViewHolder = HeaderViewHolder(view)

        if (!canBeEdit) {
            headerViewHolder.paymentCurrency.isEnabled = false
            headerViewHolder.paymentType.isEnabled = false
        } else {
            headerViewHolder.paymentCurrency.setOnClickListener {
                onItemClick.onCurrencyCodeClick()
            }

            headerViewHolder.paymentType.setOnClickListener {
                onItemClick.onPaymentTypeClick()
            }
        }

        return headerViewHolder
    }

    override fun getItemCount() = 1

    override fun onBindViewHolder(holder: HeaderViewHolder, position: Int) {
        holder.bindGroupName(groupNameStr)
        holder.bindPaymentCurrencyCode(currencyCode)
    }

    fun setGroupName(str: String) {
        groupNameStr = str
        this.notifyItemChanged(0)
    }

    fun setCurrencyCode(str: String) {
        currencyCode = str
        this.notifyItemChanged(0)
    }

    fun getCurrencyCode(): String {
        return currencyCode
    }

    fun setCanBeEdit(flag: Boolean) {
        canBeEdit = flag
    }

    interface OnItemClick {
        fun onCurrencyCodeClick()
        fun onPaymentTypeClick()
    }
}