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

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.wayne.www.waynelib.entity.ClickType
import com.wayne.www.waynelib.fdc.model.FuelSaleTransaction
import com.wayne.www.waynelib.fdc.model.FuelSaleTransactionState
import com.wayne.www.waynelib.fdc.model.Nozzle
import com.wayne.www.waynelib.listener.OnItemViewButtonClickListener
import com.wayne.www.waynelib.ui.R
import java.text.SimpleDateFormat
import java.util.ArrayList

/**
 * Created by Justin on 4/13/2018.
 */
public class WayneTransactionStatusItemView(context: Context?, transactionStatus: FuelSaleTransaction, nozzle: Nozzle?, nozzleName: String?, firstNozzle: Boolean = false) : LinearLayout(context) {

    private var bindTransactionStatus: FuelSaleTransaction? = null
    private var imageViewTrxStatus: ImageView? = null
    private var textViewTrxStatus: TextView? = null
    private var textViewNozzleId: TextView? = null
    private var textViewGradeName: TextView? = null
    private var textViewVolume: TextView? = null
    private var textViewPrice: TextView? = null
    private var textViewAmount: TextView? = null
    private var textViewTrxDate: TextView? = null
    private var nozzle: Nozzle? = null
    private val onItemViewButtonClickListener = ArrayList<OnItemViewButtonClickListener>()
    private var buttonMembershipCard: Button? = null
    private var buttonPayInside: Button? = null
    private var buttonPayByCash: Button? = null
    private var buttonAuthorizePump: Button? = null
    private var firstNozzle: Boolean = false

    init {
        this.bindTransactionStatus = transactionStatus
        nozzle?.NozzleName = nozzleName
        this.nozzle = nozzle
        this.firstNozzle = firstNozzle
        val inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        this.addView(inflater.inflate(R.layout.layout_wayne_transaction_status, null, false), LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        this.UpdateUI()
    }

    private fun UpdateUI() {
        imageViewTrxStatus = findViewById(R.id.image_view_trx_status) as ImageView
        textViewTrxStatus = findViewById(R.id.text_view_trx_status) as TextView
        textViewNozzleId = findViewById(R.id.text_view_nozzle_number) as TextView
        textViewGradeName = findViewById(R.id.text_view_grade_name) as TextView
        textViewVolume = findViewById(R.id.text_view_volume) as TextView
        textViewPrice = findViewById(R.id.text_view_price) as TextView
        textViewAmount = findViewById(R.id.text_view_current_amount) as TextView
        textViewTrxDate = findViewById(R.id.text_view_transaction_date) as TextView

        this.buttonPayInside = findViewById<View>(R.id.button_pay_inside) as Button
        this.buttonPayInside?.setOnClickListener(OnClickListener {
            if (bindTransactionStatus != null) {
                for (l in this@WayneTransactionStatusItemView.onItemViewButtonClickListener) {
                    l.onItemViewClicked(this@WayneTransactionStatusItemView, null, ClickType.PayByCash)
                }
            }
        })

        this.buttonPayByCash = findViewById<View>(R.id.button_one_key_authorize) as Button
        this.buttonPayByCash?.setOnClickListener(OnClickListener {
            if (bindTransactionStatus != null) {
                for (l in this@WayneTransactionStatusItemView.onItemViewButtonClickListener) {
                    l.onItemViewClicked(this@WayneTransactionStatusItemView, null, ClickType.OneKeyPayment)
                }
            }
        })

        if (bindTransactionStatus != null) {
            //{Idle, Authorized, Calling, Error, Starting, Fueling, Closed, Unpaid}
            //{Locked, Payable, Cleared, Init}
            textViewNozzleId!!.setText("枪号: ${nozzle?.NozzleName}")
//            textViewAmount!!.setText("金额:${(bindTransactionStatus?.Amount!!).format(2)}元")
            textViewGradeName!!.setText("油号: ${nozzle?.GradeName}")
            textViewVolume!!.setText("升数: ${(bindTransactionStatus?.Volume!!).format(2)}升")
            textViewPrice!!.setText("单价: ${bindTransactionStatus?.UnitPrice}元/升")
            val date = bindTransactionStatus?.TransactionTimeStamp?.time
            val df = SimpleDateFormat("yyyy.MM.dd HH:mm:ss")
            textViewTrxDate!!.setText("${df.format(date)}")

            when (bindTransactionStatus!!.State) {
                FuelSaleTransactionState.Cleared -> {
                    imageViewTrxStatus!!.setImageResource(R.drawable.paid)
                    textViewTrxStatus!!.setText("支付成功")
                    this.buttonPayInside!!.visibility = View.INVISIBLE
                    this.buttonPayByCash!!.visibility = View.INVISIBLE
                }
                FuelSaleTransactionState.Payable, FuelSaleTransactionState.Locked -> {
                    imageViewTrxStatus!!.setImageResource(R.drawable.waiting_for_fueling)
                    textViewTrxStatus!!.setText("待支付")
                }
            }
//            when (bindTransactionStatus!!.State) {
//                PumpOrNozzleState.Authorized, PumpOrNozzleState.Calling, PumpOrNozzleState.Starting -> {
//                    imageViewTrxStatus!!.setImageResource(R.drawable.waiting_for_fueling)
//                    textViewTrxStatus!!.setText("待加油")
//                }
//                PumpOrNozzleState.Fueling -> {
//                    imageViewTrxStatus!!.setImageResource(R.drawable.filling_ongoing)
//                    textViewTrxStatus!!.setText("加油中")
//                }
//                PumpOrNozzleState.Unpaid -> {
//                    imageViewTrxStatus!!.setImageResource(R.drawable.unpaid_failed_to_pay)
//                    textViewTrxStatus!!.setText("待支付")
//                }
//                PumpOrNozzleState.Error, PumpOrNozzleState.Closed -> {
//                    imageViewTrxStatus!!.setImageResource(R.drawable.unpaid_failed_to_pay)
//                    textViewTrxStatus!!.setText("连接中")
//                }
//                PumpOrNozzleState.Idle -> {
//                    if (bindTransactionStatus!!.State == FuelSaleTransactionState.Cleared) {
//                        imageViewTrxStatus!!.setImageResource(R.drawable.paid)
//                        textViewTrxStatus!!.setText("支付成功")
//                    } else {
//                        imageViewTrxStatus!!.setImageResource(R.drawable.unpaid_failed_to_pay)
//                        textViewTrxStatus!!.setText("待支付")
//                    }
//                }
//            }
        }

        imageViewTrxStatus!!.setOnClickListener {
            if (bindTransactionStatus != null) {
                for (l in this@WayneTransactionStatusItemView.onItemViewButtonClickListener) {
                    l.onItemViewClicked(this@WayneTransactionStatusItemView, null, ClickType.ItemSeleted)
                }
            }
        }

//        this.buttonMembershipCard = findViewById<View>(R.id.button_membership_card) as Button
//        this.buttonMembershipCard?.setOnClickListener(OnClickListener {
//                if (bindTransactionStatus != null) {
//                    for (l in this@WayneTransactionStatusItemView.onItemViewButtonClickListener) {
//                        l.onItemViewClicked(this@WayneTransactionStatusItemView, null, ClickType.MembershipCard)
//                    }
//                }
//            })


//        this.buttonAuthorizePump = findViewById<View>(R.id.button_authorize_pump) as Button
//        this.buttonAuthorizePump?.setOnClickListener(OnClickListener {
//            if (bindTransactionStatus != null) {
//                for (l in this@WayneTransactionStatusItemView.onItemViewButtonClickListener) {
//                    l.onItemViewClicked(this@WayneTransactionStatusItemView, null, ClickType.OneKeyAuth)
//                }
//            }
//        })
    }

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

    fun getFuelTransaction(): FuelSaleTransaction? {
        return bindTransactionStatus
    }

    fun addOnItemViewButtonClickListener(onItemViewButtonClickListener: OnItemViewButtonClickListener?) {
        this.onItemViewButtonClickListener.add(onItemViewButtonClickListener!!)
    }
}
