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

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

class DailyPaymentListAdapter(private var itemList: ArrayList<DailyPaymentCardListItem>): RecyclerView.Adapter<DailyPaymentListAdapter.ViewHolder>() {

    lateinit var onItemClick: OnItemClick

    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val paymentName: TextView = view.findViewById(R.id.payment_name)
        val deleteButton: ImageView = view.findViewById(R.id.delete)
        val paymentRevert: TextView = view.findViewById(R.id.payment_revert)
        val paymentCreator: TextView = view.findViewById(R.id.payment_creator)
        val totalPayment: TextView = view.findViewById(R.id.total_payment)
        val createTime: TextView = view.findViewById(R.id.create_time)
        val financeStatus: TextView = view.findViewById(R.id.finance_status)
        val financeIcon: ImageView = view.findViewById(R.id.finance_icon)
        val financeReviewer: TextView = view.findViewById(R.id.finance_reviewer)
        val generalStatus: TextView = view.findViewById(R.id.general_status)
        val generalIcon: ImageView = view.findViewById(R.id.general_icon)
        val generalReviewer: TextView = view.findViewById(R.id.general_reviewer)
        val isPay: TextView = view.findViewById(R.id.is_pay)
        val isPayIcon: ImageView = view.findViewById(R.id.is_pay_icon)
        val dailyPaymentApproval: LinearLayout = view.findViewById(R.id.daily_payment_approval)
        val disapproval: TextView = view.findViewById(R.id.disapproval)
        val approval: TextView = view.findViewById(R.id.approval)
        val passed: TextView = view.findViewById(R.id.verified)
    }

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

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

            deleteButton.setOnClickListener {
                onItemClick.onItemDelete(absoluteAdapterPosition)
            }

            disapproval.setOnClickListener {
                onItemClick.onDisapprovalClick(absoluteAdapterPosition)
            }

            approval.setOnClickListener {
                onItemClick.onApprovalClick(absoluteAdapterPosition)
            }
        }

        return viewHolder
    }

    override fun getItemCount() = itemList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.apply {
            itemList[position].also {
                paymentName.text = it.instructions
                paymentRevert.text = it.companyName
                paymentCreator.text = it.createUser
                totalPayment.text = String.format(OASystem.context.resources.getString(R.string.rmb_price), it.sumPrice)
                createTime.text = it.createTime
                financeStatus.text = it.fAuditDesc
                generalStatus.text = it.mAuditDesc
                financeReviewer.text = it.fAuditName
                generalReviewer.text = it.mAuditName

                when (it.fAudit) {
                    0 -> {
                        financeIcon.setImageResource(R.mipmap.icon_wait)
                        financeStatus.setTextColor(Color.rgb(23, 171, 227))
                    }

                    1 -> {
                        financeIcon.setImageResource(R.mipmap.icon_passed)
                        financeStatus.setTextColor(Color.rgb(84, 213, 77))
                    }

                    2 -> {
                        financeIcon.setImageResource(R.mipmap.icon_reject)
                        financeStatus.setTextColor(Color.rgb(210, 45, 34))
                    }
                }

                when (it.mAudit) {
                    0 -> {
                        generalIcon.setImageResource(R.mipmap.icon_wait)
                        generalStatus.setTextColor(Color.rgb(23, 171, 227))
                    }

                    1 -> {
                        generalIcon.setImageResource(R.mipmap.icon_passed)
                        generalStatus.setTextColor(Color.rgb(84, 213, 77))
                    }

                    2 -> {
                        generalIcon.setImageResource(R.mipmap.icon_reject)
                        generalStatus.setTextColor(Color.rgb(210, 45, 34))
                    }
                }

                when (it.isPay) {
                    0 -> {
                        isPay.apply {
                            text = resources.getString(R.string.not_pay)
                            setTextColor(Color.rgb(23, 171, 227))
                        }
                        isPayIcon.setImageResource(R.mipmap.icon_wait)
                    }

                    1 -> {
                        isPay.apply {
                            text = resources.getString(R.string.paid)
                            setTextColor(Color.rgb(84, 213, 77))
                        }
                        isPayIcon.setImageResource(R.mipmap.icon_passed)
                    }
                }

                when (OASystem.userInfo.userId) {

                    21 -> {
                        when (it.mAudit) {
                            0 -> {
                                dailyPaymentApproval.visibility = View.VISIBLE
                                passed.visibility = View.GONE
                            }

                            1 -> {
                                dailyPaymentApproval.visibility = View.GONE
                                passed.visibility = View.VISIBLE
                                passed.text = OASystem.context.resources.getString(R.string.verify_passed)
                            }

                            2 -> {
                                dailyPaymentApproval.visibility = View.GONE
                                passed.visibility = View.VISIBLE
                                passed.text = OASystem.context.resources.getString(R.string.verify_rejected)
                            }
                        }
                    }

                    22 -> {
                        when (it.fAudit) {
                            0 -> {
                                dailyPaymentApproval.visibility = View.VISIBLE
                                passed.visibility = View.GONE
                            }

                            1 -> {
                                dailyPaymentApproval.visibility = View.GONE
                                passed.visibility = View.VISIBLE
                                passed.text = OASystem.context.resources.getString(R.string.verify_passed)
                            }

                            2 -> {
                                dailyPaymentApproval.visibility = View.GONE
                                passed.visibility = View.VISIBLE
                                passed.text = OASystem.context.resources.getString(R.string.verify_rejected)
                            }
                        }
                    }
                }

                if (OASystem.authorization(OASystem.DAILY_PAYMENT, OASystem.DELETE) && it.fAudit == 0 && it.mAudit == 0) {
                    deleteButton.visibility = View.VISIBLE
                } else {
                    deleteButton.visibility = View.GONE
                }
            }
        }
    }

    interface OnItemClick {
        fun onItemClick (position: Int)

        fun onItemDelete(position: Int)

        fun onDisapprovalClick(position: Int)

        fun onApprovalClick(position: Int)
    }
}