package com.wayne.www.waynelib.ui.view

import android.content.Context
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.wayne.www.waynelib.fdc.model.FuelSaleTransaction
import com.wayne.www.waynelib.ui.R
import com.wayne.www.waynelib.util.Util

class TransactionDetailView(context: Context?, fuelSaleTransaction: FuelSaleTransaction, numOfNozzles: Int) : LinearLayout(context) {
    private var bindFuelSaleTransaction: FuelSaleTransaction? = null
    private var textViewFuelPrice: TextView? = null
    private var textViewAmount: TextView? = null
    private var textViewVolume: TextView? = null
    private var numOfNozzles: Int = 0

    init {
        this.bindFuelSaleTransaction = fuelSaleTransaction
        val inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        this.numOfNozzles = numOfNozzles
        when (numOfNozzles) {
            1,2 -> this.addView(inflater.inflate(R.layout.latest_trxs_view_2, null, false), LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
            3 -> this.addView(inflater.inflate(R.layout.latest_trxs_view_3, null, false), LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
            4 -> this.addView(inflater.inflate(R.layout.latest_trxs_view_4, null, false), LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
            else -> this.addView(inflater.inflate(R.layout.latest_trxs_view_2, null, false), LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        }

        this.UpdateUI()
    }

    private fun UpdateUI() {
        textViewFuelPrice = findViewById(R.id.text_view_fuel_price) as TextView
        textViewAmount = findViewById(R.id.text_view_amount) as TextView
        textViewVolume = findViewById(R.id.text_view_volume) as TextView

        if (bindFuelSaleTransaction != null) {
            if (Util.getScreenWidth(context) > 720 && Util.getScreenHeight(context) >= 1920) {
//                if (bindFuelSaleTransaction!!.Amount >= 1000) {
//                    when (numOfNozzles) {
//                        1,2 -> textViewAmount!!.setTextSize(TypedValue.COMPLEX_UNIT_DIP,27f)
//                        3 -> textViewAmount!!.setTextSize(TypedValue.COMPLEX_UNIT_DIP,27f)
//                        4 -> textViewAmount!!.setTextSize(TypedValue.COMPLEX_UNIT_DIP,23f)
//                        else -> textViewAmount!!.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20f)
//                    }
//                } else {
//                    when (numOfNozzles) {
//                        1,2 -> textViewAmount!!.setTextSize(TypedValue.COMPLEX_UNIT_DIP,30f)
//                        3 -> textViewAmount!!.setTextSize(TypedValue.COMPLEX_UNIT_DIP,30f)
//                        4 -> textViewAmount!!.setTextSize(TypedValue.COMPLEX_UNIT_DIP,26f)
//                        else -> textViewAmount!!.setTextSize(TypedValue.COMPLEX_UNIT_DIP,30f)
//                    }
//                }
            }

            textViewFuelPrice!!.setText(bindFuelSaleTransaction!!.UnitPrice.format(2))
//            textViewAmount!!.setText(bindFuelSaleTransaction!!.Amount.format(2))
            textViewVolume!!.setText(bindFuelSaleTransaction!!.Volume.format(2))
        }
    }

    fun Double.format(digits: Int) = java.lang.String.format("%.${digits}f", this)

    fun getTransactionStatus(): FuelSaleTransaction? {
        return bindFuelSaleTransaction
    }
}