package com.khmer9.yuki.screen.report.withdrawStatement

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.remote.LoginData
import com.khmer9.yuki.remote.StatementWithdraw
import com.khmer9.yuki.remote.User
import com.khmer9.yuki.util.Const
import com.khmer9.yuki.util.ModelPreferencesManager
import java.text.DecimalFormat
import java.text.ParseException
import java.text.SimpleDateFormat

class ReportWithdrawViewHolder(itemView: View, private val printButtonListener: ((String, String, String, String, 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 tvDate = itemView.findViewById<TextView>(R.id.tv_date)
        val tvOldBalance = itemView.findViewById<TextView>(R.id.tv_old_balance)
        val tvNewBalance = itemView.findViewById<TextView>(R.id.tv_new_balance)
        val tvAmount = itemView.findViewById<TextView>(R.id.tv_amount)
        lateinit var user: User

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

            tvId.text = item.id

            val df = SimpleDateFormat("yyyy-MM-dd")
            val parser = SimpleDateFormat("yyyy-MM-dd hh:mm a")
            try {
                val date = df.format(parser.parse(item.date))
                tvDate.text = date.toString()
            } catch (e: ParseException) {
                tvDate.text = item.date
            }

            when (user.currencyType) {
                1 -> {//dollar
                    tvOldBalance.text = "${currencyFormat(item.oldBalance)} $"
                    tvNewBalance.text = "${currencyFormat(item.newBalance)} $"
                    tvAmount.text = "${currencyFormat(item.amount)} $"
                }
                2 -> {//riel
                    tvOldBalance.text = "${currencyFormat(item.oldBalance)} ៛"
                    tvNewBalance.text = "${currencyFormat(item.newBalance)} ฿"
                    tvAmount.text = "${currencyFormat(item.amount)} ฿"
                }
                3 -> {//baht
                    tvOldBalance.text = "${currencyFormat(item.oldBalance)} ฿"
                    tvNewBalance.text = "${currencyFormat(item.newBalance)} ฿"
                    tvAmount.text = "${currencyFormat(item.amount)} ฿"
                }
            }

            tvOldBalance.setTextColor(Color.parseColor("#2E7D32"))
            tvOldBalance.setTypeface(null, Typeface.BOLD)

            //tvNewBalance.text = item.newBalance.toString()
            tvNewBalance.setTextColor(Color.parseColor("#303F9F"))
            tvNewBalance.setTypeface(null, Typeface.BOLD)

            //tvAmount.text = item.amount.toString()
            tvAmount.setTextColor(Color.parseColor("#F9A825"))
            tvAmount.setTypeface(null, Typeface.BOLD)
        }
    }
}