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.EditText
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.RecyclerView
import com.pan_american.android.OASystem
import com.pan_american.android.R
import com.pan_american.android.util.MoneyInputFilter

class GroundConveyFooterAdapter: RecyclerView.Adapter<GroundConveyFooterAdapter.FooterViewHolder>() {

    private var paymentPercentageStr = ""

    private var payeeStr = ""

    private var paymentMarkStr = ""

    private var paymentWayStr = ""

    private var canBeEdit = true

    lateinit var onItemClick: OnItemClick

    inner class FooterViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val paymentPercentage: EditText = view.findViewById(R.id.payment_percentage)
        val payee: EditText = view.findViewById(R.id.payee)
        val paymentMark: TextView = view.findViewById(R.id.payment_mark)
        val paymentWay: TextView = view.findViewById(R.id.payment_way)

        fun bindPercentage(str: String) {
            paymentPercentage.setText(str)
        }

        fun bindPayee(str: String) {
            payee.setText(str)
        }

        fun bindPaymentMark(str: String) {
            paymentMark.text = str
        }

        fun bindPaymentWay(str: String) {
            paymentWay.text = str
        }
    }

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

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

        val footerViewHolder = FooterViewHolder(view)

        if (!canBeEdit) {
            footerViewHolder.paymentMark.isEnabled = false
            footerViewHolder.paymentWay.isEnabled = false
            footerViewHolder.payee.isEnabled =false
            footerViewHolder.paymentPercentage.isEnabled = false
        } else {
            footerViewHolder.paymentMark.setOnClickListener {
                onItemClick.paymentMarkClick()
            }

            footerViewHolder.paymentWay.setOnClickListener {
                onItemClick.paymentWayClick()
            }

            footerViewHolder.payee.addTextChangedListener({_,_,_,_ -> }, {_,_,_,_ -> }, {
                if (it != null) {
                    payeeStr = it.toString()
                }
            })

            footerViewHolder.paymentPercentage.addTextChangedListener({_,_,_,_ -> }, {_,_,_,_ -> }, {
                if (it != null) {
                    paymentPercentageStr = it.toString()
                }
            })
        }

        return footerViewHolder
    }

    override fun getItemCount() = 1

    override fun onBindViewHolder(holder: FooterViewHolder, position: Int) {
        holder.bindPercentage(paymentPercentageStr)
        holder.bindPayee(payeeStr)
        holder.bindPaymentMark(paymentMarkStr)
        holder.bindPaymentWay(paymentWayStr)

        holder.paymentPercentage.filters = arrayOf(MoneyInputFilter())
    }

    fun setPaymentPercentage(str: String) {
        paymentPercentageStr = str
        this.notifyItemChanged(0)
    }

    fun getPaymentPercentage(): String {
        return paymentPercentageStr
    }

    fun setPayee(str: String) {
        payeeStr = str
        this.notifyItemChanged(0)
    }

    fun getPayee(): String {
        return payeeStr
    }

    fun setPaymentMark(str: String) {
        paymentMarkStr = str
        this.notifyItemChanged(0)
    }

    fun setPaymentWay(str: String) {
        this.notifyItemChanged(0)
        paymentWayStr = str
    }

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

    interface OnItemClick {
        fun paymentMarkClick()
        fun paymentWayClick()
    }
}