package com.cocking.fight.screen.report

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.gdtlib.lib.adapter.BaseListItem
import com.gdtlib.lib.viewholder.BaseListViewHolder
import com.sambath.klaklouk_main.R
import com.sambath.klaklouk_main.extension.setSafeOnClickListener
import com.sambath.klaklouk_main.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)

                try {
                    var datetime = item.date.split(" ")
                    var time = datetime[1]
                    var date = datetime[0]
                    tvDate.text = date + System.getProperty("line.separator") + time
                } catch (e: Exception) {
                    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)

                try {
                    var datetime = item.date.split(" ")
                    var time = datetime[1]
                    var date = datetime[0]
                    tvDate.text = date + System.getProperty("line.separator") + time
                } catch (e: Exception) {
                    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

                try {
                    var datetime = item.date.split(" ")
                    var time = datetime[1]
                    var date = datetime[0]
                    tvBettingDate.text = date + System.getProperty("line.separator") + time
                } catch (e: Exception) {
                    tvBettingDate.text = item.date
                }

                when (betType) {
                    /*K.Cancel -> {
                        tvWinResult.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.cancel
                            )
                        )
                    }
                    else -> {
                        tvWinResult.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.color_blue_800
                            )
                        )
                    }*/
                }
                tvResultName.text = item.resultName
                when (item.resultName) {
                    /*K.Cancel -> {
                        tvResultName.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.cancel
                            )
                        )
                    }
                    else -> {
                        tvWinResult.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.color_blue_800
                            )
                        )
                    }*/
                }
                val winLose = if (item.amountWin.isNullOrEmpty()) {
                    "0"
                } else {
                    getCurrency(item.amountWin)
                }
                tvResultAmount.text = "$betCast=$winLose"
                if (item.is_win == true) {
                    tvResultAmount.setTextColor(
                        ContextCompat.getColor(
                            itemView.context,
                            R.color.color_blue_800
                        )
                    )
                } else {
                    tvResultAmount.setTextColor(Color.RED)
                }
                tvFightNo.setSafeOnClickListener {
                    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()

                try {
                    var datetime = item.date.split(" ")
                    var time = datetime[1]
                    var date = datetime[0]
                    tvBettingDate.text = date + System.getProperty("line.separator") + time
                } catch (e: Exception) {
                    tvBettingDate.text = item.date
                }

                when (betType) {
                    K.Cancel -> {
                        tvResultName.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.cancel
                            )
                        )
                    }
                    else -> {
                        tvResultName.setTextColor(
                            ContextCompat.getColor(
                                itemView.context,
                                R.color.color_blue_800
                            )
                        )
                    }
                }
            }
            is ReportThree -> {//Statement
            }
        }
    }

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

    private fun getCurrency(currency: String): String {
        val cur = currency.toDouble()
        return if (cur > 999 || cur < -999) {
            val short = cur / 1000
            "${short}K"
        } else {
            cur.toString()
        }
    }
}