package com.sambath.klaklouk_main.screen.main

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import android.widget.TextView
import com.gdtlib.lib.adapter.BaseListItem
import com.gdtlib.lib.viewholder.BaseListViewHolder
import com.sambath.klaklouk_main.R
import com.sambath.klaklouk_main.extension.btnClick
import com.sambath.klaklouk_main.extension.setSafeOnClickListener
import com.sambath.klaklouk_main.remote.ListItem

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

    val tvId = itemView.findViewById<TextView>(R.id.tv_id)
    val tvBet = itemView.findViewById<TextView>(R.id.tv_bet)
    val tvAmount =itemView.findViewById<TextView>(R.id.tv_amount)
    val tvCode = itemView.findViewById<TextView>(R.id.tv_code)

    @SuppressLint("SetTextI18n")
    override fun bindView(item: BaseListItem) {

        when(item){

            is ListItem ->{

                val ticketID= item.id.substring(item.id.length-7)
                val betType = getTypeOfBetting(item.typeOfBetting)
                val betCast = "${getCurrency(item.amount.toInt())}x${item.cast}"
                val winLose: String

                tvId.text = myBettingId
                tvBet.text = betType
                tvCode.text = ticketID
                if (item.win_lose != null){
                    winLose = "${item.amount_win}"
                    tvAmount.text = "$betCast=$winLose"
                    when (item.win_lose) {
                        "false" -> {//lost
                            tvAmount.setTextColor(Color.RED)

                        }
                        "true" -> {//win
                            tvAmount.setTextColor(Color.BLUE)

                        }
                    }
                }else{
                    //waiting result
                    winLose = "???"

                    tvAmount.text = "$betCast=$winLose"


                }

                tvCode.setSafeOnClickListener{
                    it.btnClick().subscribe {
                        printButtonListener?.invoke(ticketID, myBettingId,item.date,betType,betCast,winLose)

                    }
                }


            }


        }

    }



}