package com.khmer9.lotto.screen.report

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.gdtlib.lib.adapter.BaseListItem
import com.gdtlib.lib.viewholder.BaseListViewHolder
import com.khmer9.lotto.R
import com.khmer9.lotto.extension.btnClick
import com.khmer9.lotto.extension.setSafeOnClickListener
import com.khmer9.lotto.remote.Statement
import com.khmer9.lotto.remote.Win
import com.khmer9.lotto.remote.WinLost
import java.text.DecimalFormat


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


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

        when(item){

            is Win ->{
                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)

                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 = item.no
                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, item.no,item.date,betType,betCast,winLose)

                    }
                }

            }
            is WinLost ->{
                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)

                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 = item.no
                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, item.no,item.date,betType,betCast,winLose)

                    }
                }

            }
            is Statement ->{//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.date
                tvC3.text = currencyFormat(item.amount!!.toInt())
                when(item.note){

                    "Loto"->{
                        if (item.amount_win!! < 0){//lost
                            imgC1.setImageResource(R.drawable.bet)
                            tvC4.text = currencyFormat(item.amount_win.toInt())
                            tvC4.setTextColor(Color.RED)


                        }else{//win
                            imgC1.setImageResource(R.drawable.bet)
                            tvC4.text = currencyFormat(item.amount_win.toInt())
                            tvC4.setTextColor(Color.BLUE)
                        }

                    }
                    "Widthraw"->{
                        imgC1.setImageResource(R.drawable.withdraw)


                    }
                    "Deposite"->{
                        imgC1.setImageResource(R.drawable.deposit)

                    }
                }


            }


        }

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


}