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.LinearLayout
import android.widget.TextView
import android.widget.Toast
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 overPayCurrencyCode = ""

    private var overPayExchangeRate = ""

    private var canBeEdit = true

    lateinit var onItemClick: OnItemClick

    lateinit var onItemTextChanged: OnItemTextChanged

    inner class HeaderViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val groupName: TextView = view.findViewById(R.id.group_name)
        val paymentCurrency: TextView = view.findViewById(R.id.payment_currency)
        val overPayCurrency: TextView = view.findViewById(R.id.over_pay_currency)
        val overPayExchangeRate: EditText = view.findViewById(R.id.over_pay_exchange_rate)
        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
        }

        fun bindOverPayCurrencyCode(code: String) {
            this.overPayCurrency.text = code
        }

        fun bindOverPayCurrencyRate(rate: String) {
            this.overPayExchangeRate.setText(rate)
        }
    }

    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
        } else {
            headerViewHolder.paymentCurrency.setOnClickListener {
                onItemClick.onCurrencyCodeClick()
            }

            headerViewHolder.overPayCurrency.setOnClickListener {
                onItemClick.onOverPayCurrencyClick()
            }

            headerViewHolder.overPayExchangeRate.setOnFocusChangeListener { _, flag ->

                if (!flag) {
                    if (headerViewHolder.overPayExchangeRate.text.isNullOrBlank() || headerViewHolder.overPayExchangeRate.text.toString().toDouble() == 0.0) {
                        Toast.makeText(OASystem.context, "超支汇率不能为空！", Toast.LENGTH_SHORT).show()
                        return@setOnFocusChangeListener
                    }

                    if (headerViewHolder.overPayExchangeRate.text.endsWith('.')) {
                        return@setOnFocusChangeListener
                    }

                    overPayExchangeRate = headerViewHolder.overPayExchangeRate.text.toString()
                    onItemTextChanged.onOverPayExchangeRateChanged()
                }
            }

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

        return headerViewHolder
    }

    override fun getItemCount() = 1

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

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

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

    fun getCurrencyCode(): String {
        return currencyCode
    }

    fun setOverPayCurrencyCode(str: String) {
        overPayCurrencyCode = str
        this.notifyItemChanged(0)
    }

    fun setOverPayExchangeRate(str: String) {
        overPayExchangeRate = str
        this.notifyItemChanged(0)
    }

    fun getOverPayExchangeRate(): String {
        return overPayExchangeRate
    }

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

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

    interface OnItemTextChanged {
        fun onOverPayExchangeRateChanged()
    }
}