package com.pan_american.android.data.model.financial_module.expense_approval.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
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
import com.pan_american.android.data.model.financial_module.expense_approval.entity.ExpenseApprovalPaymentItem

class ExpenseApprovalPaymentListAdapter(private val paymentList: ArrayList<ExpenseApprovalPaymentItem>): RecyclerView.Adapter<ExpenseApprovalPaymentListAdapter.ViewHolder>() {

    lateinit var onItemClick: OnItemClick

    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val paymentName: TextView = view.findViewById(R.id.payment_name)
        val check: CheckBox = view.findViewById(R.id.check)
        val paymentModel: TextView = view.findViewById(R.id.payment_model)
        val busView: LinearLayout = view.findViewById(R.id.bus_view)
        val busName: TextView = view.findViewById(R.id.bus_name)
        val requiredPayment: TextView = view.findViewById(R.id.required_payment)
        val currentPayment: TextView = view.findViewById(R.id.current_payment)
        val balance: TextView = view.findViewById(R.id.balance)
        val overBudget: TextView = view.findViewById(R.id.over_budget)
        val paymentCreator: TextView = view.findViewById(R.id.payment_creator)
        val verifyName: TextView = view.findViewById(R.id.verify_name)
        val paymentWay: TextView = view.findViewById(R.id.payment_way)
        val cardType: TextView = view.findViewById(R.id.card_type)
        val reject: TextView = view.findViewById(R.id.reject)
        val pass: TextView = view.findViewById(R.id.pass)
        val revoke: TextView = view.findViewById(R.id.revoke)
    }

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

        val viewHolder = ViewHolder(view)

        viewHolder.itemView.setOnClickListener {
            onItemClick.onItemClick(viewHolder.absoluteAdapterPosition)
        }

        viewHolder.reject.setOnClickListener {
            onItemClick.onItemReject(viewHolder.absoluteAdapterPosition)
        }

        viewHolder.pass.setOnClickListener {
            onItemClick.onItemPass(viewHolder.absoluteAdapterPosition)
        }

        viewHolder.revoke.setOnClickListener {
            onItemClick.onItemRevoke(viewHolder.absoluteAdapterPosition)
        }

        return viewHolder
    }

    override fun getItemCount() = paymentList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        paymentList[position].also {
            holder.apply {
                paymentName.text = it.priceNameContent
                paymentModel.text = it.priceName
                if (it.priceName == "车/导游地接") {
                    busView.visibility = View.VISIBLE
                    busName.text = it.busName
                }
                requiredPayment.text = it.waitPay
                currentPayment.text = it.currPay
                balance.text = it.balance
                paymentCreator.text = it.operatorName
                verifyName.text = it.auditOperatorName
                overBudget.text = it.overBudget
                paymentWay.text = it.payType
                cardType.text = it.cardType

                when(it.isAuditGM) {
                    0 -> {
                        reject.visibility = View.VISIBLE
                        pass.visibility = View.VISIBLE
                        revoke.visibility = View.GONE
                    }

                    1 -> {
                        reject.visibility = View.GONE
                        pass.visibility = View.GONE
                        revoke.visibility = View.VISIBLE
                    }

                    2 -> {
                        reject.visibility = View.GONE
                        pass.visibility = View.VISIBLE
                        revoke.visibility = View.GONE
                    }
                }

                check.setOnCheckedChangeListener { _, isChecked ->
                    it.isChecked = isChecked
                    onItemClick.onItemCheck(position)
                }
            }
        }
    }

    interface OnItemClick {
        fun onItemCheck(position: Int)

        fun onItemClick(position: Int)

        fun onItemReject(position: Int)

        fun onItemPass(position: Int)

        fun onItemRevoke(position: Int)
    }
}