package com.khmer9.yuki.screen.main

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Build
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.gdtlib.lib.adapter.BaseListItem
import com.gdtlib.lib.viewholder.BaseListViewHolder
import com.khmer9.yuki.R
import com.khmer9.yuki.app.App
import com.khmer9.yuki.app.ShareActivity
import com.khmer9.yuki.extension.btnClick
import com.khmer9.yuki.extension.setSafeOnClickListener
import com.khmer9.yuki.remote.CurrentBettingMobile
import com.khmer9.yuki.remote.LoginData
import com.khmer9.yuki.remote.User
import com.khmer9.yuki.util.Const
import com.khmer9.yuki.util.ModelPreferencesManager
import java.text.DecimalFormat

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

    private val tvId = itemView.findViewById<TextView>(R.id.tv_id)
    private val tvBet = itemView.findViewById<TextView>(R.id.tv_bet)
    private val tvAmount = itemView.findViewById<TextView>(R.id.tv_amount)
    private val tvCode = itemView.findViewById<TextView>(R.id.tv_code)
    private val tvWinLose = itemView.findViewById<TextView>(R.id.tv_win_lose)
    private val tvCast = itemView.findViewById<TextView>(R.id.tv_cast)
    private lateinit var user: User

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

    @RequiresApi(Build.VERSION_CODES.O)
    @SuppressLint("SetTextI18n")
    override fun bindView(item: BaseListItem) {
        Log.d("MainListViewHolder", item.toString())
        when (item) {
            is CurrentBettingMobile -> {
                try {
                    var datetime = item.time.split(" ")
                    var time = datetime[1]
                    var date = datetime[0]
                    tvId.text = date + System.getProperty("line.separator") + time
                } catch (e: Exception) {
                }

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

                //tvAmount.text = "${currencyFormat(item.totalAmount)}"
                tvCode.text = "លម្អិត"
                tvBet.text = "#" + item.no.toString()
                tvCast.text = "${item.totalCast}"

                try {
                    if (item.isReleaseResult) {
                        tvWinLose.visibility = View.VISIBLE
                        //tvWinLose.text = "${currencyFormat(item.totalAmountWin!!)}"
                        if (item.totalAmountWin!! > 0) {
                            tvWinLose.setTextColor(Color.parseColor("#303F9F"))
                            tvCast.setTextColor(Color.parseColor("#303F9F"))
                        } else {
                            tvWinLose.setTextColor(Color.parseColor("#C62828"))
                            tvCast.setTextColor(Color.parseColor("#C62828"))
                        }
                    } else {
                        tvWinLose.text = "0.0"
                    }
                } catch (e: Exception) {
                    tvWinLose.text = "0.0"
                }

                when (user.currencyType) {
                    1 -> {//dollar
                        tvWinLose.text = "${currencyFormat(item.totalAmountWin!!)} $"
                        tvAmount.text = "${currencyFormat(item.totalAmount)} $"
                    }
                    2 -> {//riel
                        tvWinLose.text = "${currencyFormat(item.totalAmountWin!!)} ៛"
                        tvAmount.text = "${currencyFormat(item.totalAmount)} ៛"
                    }
                    3 -> {//baht
                        tvWinLose.text = "${currencyFormat(item.totalAmountWin!!)} ฿"
                        tvAmount.text = "${currencyFormat(item.totalAmount)} ฿"
                    }
                }

                tvCode.setSafeOnClickListener {
                    it.btnClick().subscribe {
                        detailButtonListener?.invoke(item.id, item.no.toString())
                    }
                }

                try {
                    val strSplit = item.totalAmount.toString().split("x")
                    val strSplit2 = strSplit[1].split("=")
                    val bet = strSplit[0].trim()
                    val cast = strSplit2[0].trim()
                    val winLost = bet.toDouble() * cast.toDouble()

                    tvBet.setSafeOnClickListener {
                        it.btnClick().subscribe {
                            printButtonListener?.invoke(item.no.toString(), item.id, item.time, item.totalCast.toString(), item.totalAmount.toString(), winLost.toString())
                        }
                    }
                } catch (e: Exception) {
                    tvBet.setSafeOnClickListener {
                        it.btnClick().subscribe {
                            printButtonListener?.invoke(item.no.toString(), item.id, item.time, item.totalCast.toString(), item.totalAmount.toString(), "0")
                        }
                    }
                }
            }
        }
    }
}