package com.cocking.fight.screen.report

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.gdtlib.lib.adapter.BaseListItem
import com.gdtlib.lib.viewholder.BaseListViewHolder
import com.sambath.kunkhmer.R
import com.sambath.kunkhmer.extension.setSafeOnClickListener
import com.sambath.kunkhmer.remote.*
import java.text.DecimalFormat


class ReportListViewHolder(
    itemView: View,
    private val printButtonListener: ((ReportOne) -> Unit)? = null
) : BaseListViewHolder(itemView) {
    @SuppressLint("SetTextI18n")
    override fun bindView(item: BaseListItem) {
        when (item) {
            is ReportDeposit -> {
                val tvDate = itemView.findViewById<TextView>(R.id.tv_date)
                val tvOldBalance = itemView.findViewById<TextView>(R.id.tv_result_amount)
                val tvNewBalance = itemView.findViewById<TextView>(R.id.tv_old_balance)
                val tvAmount = itemView.findViewById<TextView>(R.id.tv_amount)
                tvDate.text = item.date
                tvAmount.text = item.amount
                tvOldBalance.text = item.oldBalance
                tvNewBalance.text = item.newBalance
            }
            is ReportWithdraw -> {
                val tvDate = itemView.findViewById<TextView>(R.id.tv_date)
                val tvOldBalance = itemView.findViewById<TextView>(R.id.tv_result_amount)
                val tvNewBalance = itemView.findViewById<TextView>(R.id.tv_old_balance)
                val tvAmount = itemView.findViewById<TextView>(R.id.tv_amount)
                tvDate.text = item.date
                tvAmount.text = item.amount
                tvOldBalance.text = item.oldBalance
                tvNewBalance.text = item.newBalance
            }
            is ReportOne -> {
                val tvFightNo = itemView.findViewById<TextView>(R.id.tv_fight_no)
                val tvResultAmount = itemView.findViewById<TextView>(R.id.tv_result_amount)
                val tvWinResult = itemView.findViewById<TextView>(R.id.tv_old_balance)
                val tvResultName = itemView.findViewById<TextView>(R.id.tv_result_name)
                val tvBettingDate = itemView.findViewById<TextView>(R.id.tv_amount)
                val betType = item.betName
                val betCast = "${getCurrency(item.amount)}x${item.payout}"
                tvFightNo.text = "#${item.fightNo.toString()}"
                tvWinResult.text = betType
//                val date = item.date.split(" ")
                tvBettingDate.text = item.date
                when (betType) {
                    K.Meron -> {
                        tvWinResult.setTextColor(Color.RED)
                    }
                    K.Wala -> {
                        tvWinResult.setTextColor(Color.BLUE)
                    }
                    K.Tie -> {
                        tvWinResult.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.draw
                            )
                        )
                    }
                    K.Cancel -> {
                        tvWinResult.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.cancel
                            )
                        )
                    }
                }
                tvResultName.text = item.resultName
                when (item.resultName) {
                    K.Meron -> {
                        tvResultName.setTextColor(Color.RED)
                    }
                    K.Wala -> {
                        tvResultName.setTextColor(Color.BLUE)
                    }
                    K.Tie -> {
                        tvResultName.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.draw
                            )
                        )
                    }
                    K.Cancel -> {
                        tvResultName.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.cancel
                            )
                        )
                    }
                }
                val winLose = getCurrency(item.amountWin)
                tvResultAmount.text = "$betCast=$winLose"
                if (item.is_win == true) {
                    tvResultAmount.setTextColor(Color.BLACK)
                } else {
                    tvResultAmount.setTextColor(Color.RED)
                }
                tvFightNo.setSafeOnClickListener {
//                    Toast.makeText(co)
                    printButtonListener?.invoke(item)
                }
            }
            is ReportTwo -> {
                val tvFightNo = itemView.findViewById<TextView>(R.id.tv_fight_no)
                val tvResultName = itemView.findViewById<TextView>(R.id.tv_result_name)
                val tvGroupNo = itemView.findViewById<TextView>(R.id.tv_group_no)
                val tvBettingDate = itemView.findViewById<TextView>(R.id.tv_amount)
                val betType = item.name
                tvFightNo.text = "#${item.fightNo}"
                tvResultName.text = item.name
                tvGroupNo.text = item.group.toString()
//                val date = item.date.split(" ")
                tvBettingDate.text = item.date
                when (betType) {
                    K.Meron -> {
                        tvResultName.setTextColor(Color.RED)
                    }
                    K.Wala -> {
                        tvResultName.setTextColor(Color.BLUE)
                    }
                    K.Tie -> {
                        tvResultName.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.draw
                            )
                        )
                    }
                    K.Cancel -> {
                        tvResultName.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.cancel
                            )
                        )
                    }
                }
            }
            is ReportThree -> {//Statement
                val imgC1 = itemView.findViewById<ImageView>(R.id.img_c1)
                val tvC2 = itemView.findViewById<TextView>(R.id.tv_c2)
                val tvC3 = itemView.findViewById<TextView>(R.id.tv_c3)
                val tvC4 = itemView.findViewById<TextView>(R.id.tv_c4)
                // val tvC5 = itemView.findViewById<TextView>(R.id.tv_c5)
                tvC2.text = item.type
                tvC3.text = item.date
                when (item.type) {
                    "BETTED" -> {
                        imgC1.setImageResource(R.drawable.bet)
                        if (item.amount < 0) {//lost
                            tvC4.text = currencyFormat(item.amount.toInt())
                            tvC4.setTextColor(Color.RED)
                        } else {//win
                            tvC4.text = currencyFormat(item.amount.toInt())
                            tvC4.setTextColor(Color.BLUE)
                        }
                    }
                    "WITHDREW" -> {
                        imgC1.setImageResource(R.drawable.withdraw)
                    }
                    "DEPOSITED" -> {
                        imgC1.setImageResource(R.drawable.deposit)
                    }
                }
            }
        }
    }

    fun currencyFormat(amount: Int): String? {
        val formatter = DecimalFormat("#,###")
        return formatter.format(amount)
    }


}