package com.khmer9.yuki.screen.report.bettingStatement

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Typeface
import android.view.View
import android.widget.TextView
import com.gdtlib.lib.adapter.BaseListItem
import com.gdtlib.lib.viewholder.BaseListViewHolder
import com.khmer9.yuki.R
import com.khmer9.yuki.extension.btnClick
import com.khmer9.yuki.extension.setSafeOnClickListener
import com.khmer9.yuki.remote.LoginData
import com.khmer9.yuki.remote.StatementBetting
import com.khmer9.yuki.remote.User
import com.khmer9.yuki.util.Const
import com.khmer9.yuki.util.ModelPreferencesManager
import java.text.DecimalFormat

class ReportBettingViewHolder(
    itemView: View,
    private val printButtonListener: ((String, String, String, String, String, String) -> Unit)? = null,
    private val detailButtonListener: ((String, String) -> Unit)? = null
) : BaseListViewHolder(itemView) {

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

    @SuppressLint("SetTextI18n")
    override fun bindView(item: BaseListItem) {
        val tvId = itemView.findViewById<TextView>(R.id.tv_id)
        val tvBetType = itemView.findViewById<TextView>(R.id.tv_bet_type)
        val tvBetAmount = itemView.findViewById<TextView>(R.id.tv_bet_amount)
        val tvPayout = itemView.findViewById<TextView>(R.id.tv_payout)
        val tvWinLost = itemView.findViewById<TextView>(R.id.tv_win_lose)
        val tvCast = itemView.findViewById<TextView>(R.id.tv_cast)
        lateinit var user: User

        ModelPreferencesManager.get<LoginData>(Const.USER_KEY)?.let {
            user = it.user!!
        }

        if (item is StatementBetting) {
            tvId.text = "#" + item.no

            var datetime = item.date.split(" ")
            var time = datetime[1]
            var date = datetime[0]
            tvPayout.text = date + System.getProperty("line.separator") + time

            if (item.winLose > 0) {
                tvBetAmount.setTextColor(Color.parseColor("#303F9F"))
                tvBetAmount.setTypeface(null, Typeface.BOLD)

                tvCast.setTextColor(Color.parseColor("#303F9F"))
                tvCast.setTypeface(null, Typeface.BOLD)
            } else {
                tvBetAmount.setTextColor(Color.parseColor("#C62828"))
                tvBetAmount.setTypeface(null, Typeface.BOLD)

                tvCast.setTextColor(Color.parseColor("#C62828"))
                tvCast.setTypeface(null, Typeface.BOLD)
            }

            tvWinLost.text = "លម្អិត"
            tvWinLost.setSafeOnClickListener {
                it.btnClick().subscribe {
                    detailButtonListener?.invoke(item.id, item.no.toString())
                }
            }

            tvCast.text = "${item.cast}"

            when (user.currencyType) {
                1 -> {//dollar
                    tvBetType.text = "${currencyFormat(item.amount!!)} $"
                    tvBetAmount.text = "${currencyFormat(item.winLose)} $"
                }
                2 -> {//riel
                    tvBetType.text = "${currencyFormat(item.amount!!)} ៛"
                    tvBetAmount.text = "${currencyFormat(item.winLose)} ៛"
                }
                3 -> {//baht
                    tvBetType.text = "${currencyFormat(item.amount!!)} ฿"
                    tvBetAmount.text = "${currencyFormat(item.winLose)} ฿"
                }
            }
        }
    }
}