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

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
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.group_op.ground_convey_payment_insert.entity.GroundConveyListItem

class GroundConveyListAdapter(private val groundConveyList: ArrayList<GroundConveyListItem>) : RecyclerView.Adapter<GroundConveyListAdapter.ViewHolder>() {

    lateinit var onRecyclerViewItemClick: OnRecyclerViewItemClick<GroundConveyListItem>

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

    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val groundConveyArea: TextView = view.findViewById(R.id.group_convey_area)
        val paymentName: TextView = view.findViewById(R.id.payment_name)
        val servicePrice: TextView = view.findViewById(R.id.service_price)
        val thisTimePayment: TextView = view.findViewById(R.id.this_time_payment)
        val balancePayment: TextView = view.findViewById(R.id.balance_payment)
        val serviceDuringTime: TextView = view.findViewById(R.id.service_during_time)
        val isVerify: TextView = view.findViewById(R.id.is_verify)

        val deleteButton : ImageView = view.findViewById(R.id.delete)
        val viewPaymentDetail: TextView = view.findViewById(R.id.payment_detail)

        val priceType: TextView = view.findViewById(R.id.price_type)
    }

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

        val viewHolder = ViewHolder(view)

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

        viewHolder.viewPaymentDetail.setOnClickListener {
            onRecyclerViewItemClick.viewPaymentDetail(viewHolder.bindingAdapterPosition)
        }

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

        return viewHolder
    }

    override fun getItemCount() = groundConveyList.size

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

            holder.groundConveyArea.text = area
            holder.paymentName.text = priceName.ifBlank {
                "暂无"
            }
            holder.servicePrice.text = String.format("%.2f (%s)", payMoney, currencyStr)
            holder.thisTimePayment.text = String.format("%.2f (%s)", payThenMoney, currencyStr)
            holder.balancePayment.text = String.format("%.2f (%s)", remainingBalance, currencyStr)
            holder.serviceDuringTime.text = String.format("%s - %s", serviceStartTime, serviceEndTime)
            holder.priceType.text = priceTypeStr.ifBlank {
                "暂无"
            }

            when(isAuditGM) {
                0 -> {
                    holder.isVerify.apply {
                        text = resources.getString(R.string.verify_wait)
                        setTextColor(Color.rgb(23, 171, 227))
                    }
                }

                1 -> {
                    holder.isVerify.apply {
                        text = resources.getString(R.string.verify_passed)
                        setTextColor(Color.rgb(84, 213, 77))
                    }
                }

                2 -> {
                    holder.isVerify.apply {
                        text = resources.getString(R.string.verify_reject)
                        setTextColor(Color.rgb(210, 45, 34))
                    }
                }

                3 -> {
                    holder.isVerify.apply {
                        text = resources.getString(R.string.auto_verify)
                    }
                }
            }
        }

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

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