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

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.wayne.www.waynelib.ui.R
import com.wayne.www.waynelib.entity.TransactionStatus

/**
 * Created by Justin on 4/13/2018.
 */
public class TransactionStatusItemView(context: Context?, transactionStatus: TransactionStatus) : LinearLayout(context) {

    private var bindTransactionStatus: TransactionStatus? = null
    private var imageViewTrxStatus: ImageView? = null
    private var textViewTrxStatus: TextView? = null
    private var textViewCarLicensePlate: TextView? = null
    private var textViewMemberId: 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

    init {
        this.bindTransactionStatus = transactionStatus
        val inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        this.addView(inflater.inflate(R.layout.layout_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
        textViewCarLicensePlate = findViewById(R.id.text_view_car_license_plate) as TextView
        textViewMemberId = findViewById(R.id.text_view_member_id) as TextView
        textViewNozzleId = findViewById(R.id.text_view_nozzle_id) 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_amount) as TextView
        textViewTrxDate = findViewById(R.id.text_view_transaction_date) as TextView

        if (bindTransactionStatus != null) {
            when (bindTransactionStatus!!.transactionStatus) {
                "ReadyForFillingStart" -> { imageViewTrxStatus!!.setImageResource(R.drawable.waiting_for_fueling)
                    textViewTrxStatus!!.setText("待加油") }
                "FillingOngoing" -> { imageViewTrxStatus!!.setImageResource(R.drawable.filling_ongoing)
                    textViewTrxStatus!!.setText("加油中")}
                "PendingForPayment" -> { imageViewTrxStatus!!.setImageResource(R.drawable.unpaid_failed_to_pay)
                    textViewTrxStatus!!.setText("待支付")}
                "Failed" -> { imageViewTrxStatus!!.setImageResource(R.drawable.unpaid_failed_to_pay)
                    textViewTrxStatus!!.setText("支付失败")}
                "Success" -> { imageViewTrxStatus!!.setImageResource(R.drawable.paid)
                    textViewTrxStatus!!.setText("支付成功")}
                }

            textViewCarLicensePlate!!.setText(bindTransactionStatus!!.carPlate)
            textViewMemberId!!.setText("储值卡号： " + bindTransactionStatus!!.memberId)
            textViewNozzleId!!.setText("枪号:" + bindTransactionStatus!!.nozzleId.toString())
            textViewGradeName!!.setText("油号:" + bindTransactionStatus!!.gradeName)
            textViewVolume!!.setText("升数:" + bindTransactionStatus!!.volume + "升")
            textViewPrice!!.setText("单价:" + bindTransactionStatus!!.price + "元/升")
            textViewAmount!!.setText("金额:" + bindTransactionStatus!!.amount + "元")
            textViewTrxDate!!.setText(bindTransactionStatus!!.time)
        }
    }

    fun getTransactionStatus(): TransactionStatus? {
        return bindTransactionStatus
    }

}
