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

import android.content.Context
import android.graphics.Color
import android.os.Handler
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.wayne.www.waynelib.entity.ClickType
import com.wayne.www.waynelib.entity.ShellTrxStatus
import com.wayne.www.waynelib.listener.OnItemViewButtonClickListener
import com.wayne.www.waynelib.ui.R
import java.util.*

class ShellTrxStatusView(context: Context?, trxStatus: ShellTrxStatus, position: Int) : ConstraintLayout(context) {
    private var bindTrxStatus: ShellTrxStatus
    private val onItemViewButtonClickListener = ArrayList<OnItemViewButtonClickListener>()
    private var position: Int = 0

    init {
        this.bindTrxStatus = trxStatus
        this.position = position
        if (bindTrxStatus != null) {
            val inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            this.addView(inflater.inflate(R.layout.layout_shell_license_plate_view, null, false),
                    ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80))
            this.UpdateUI()
        }
    }

    private fun UpdateUI() {
        var textViewLicensePlate = findViewById(R.id.text_view_shell_license_plate) as TextView
        var textViewTrxStatus = findViewById(R.id.text_view_shell_trx_status) as TextView
        var imageViewTrxStatus = findViewById(R.id.image_view_shell_trx_status) as ImageView
        var layout = findViewById(R.id.shell_license_plate_layout) as ConstraintLayout

//        layout.setOnClickListener {
//            Handler().post {
//                for (l in this@ShellTrxStatusView.onItemViewButtonClickListener) {
//                    l.onItemViewClicked(this@ShellTrxStatusView, position, ClickType.SingleClick)
//                }
//            }
//            true
//        }
//
//        layout.setOnLongClickListener{
//            Handler().post {
//                for (l in this@ShellTrxStatusView.onItemViewButtonClickListener) {
//                    l.onItemViewClicked(this@ShellTrxStatusView, position, ClickType.LongClick)
//                }
//            }
//            true
//        }

        textViewLicensePlate.setOnClickListener {
            Handler().post {
                for (l in this@ShellTrxStatusView.onItemViewButtonClickListener) {
                    l.onItemViewClicked(this@ShellTrxStatusView, position, ClickType.SingleClick)
                }
            }
            true
        }

        textViewLicensePlate.setOnLongClickListener {
            Handler().post {
                for (l in this@ShellTrxStatusView.onItemViewButtonClickListener) {
                    l.onItemViewClicked(this@ShellTrxStatusView, position, ClickType.LongClick)
                }
            }
            true
        }

        textViewLicensePlate.setText(bindTrxStatus.licensePlate)
        textViewTrxStatus.setText(bindTrxStatus.trxStatusText)

//        NORMAL_USER,
//        LICENSE_PLATE_USER,
//        TRX_IN_PROGRESS,
//        TRX_UNPAID,
//        TRX_PAID,
//        TRX_ERROR
//
//        when (bindTrxStatus.trxStatus) {
//            ShellTrxStatusEnum.NORMAL_USER ->
//                backgroundColor = resources.getColor(R.color.light_grey)
//            ShellTrxStatusEnum.LICENSE_PLATE_USER ->
//                backgroundColor = resources.getColor(R.color.gold)
//            ShellTrxStatusEnum.TRX_IN_PROGRESS ->
//                backgroundColor = resources.getColor(R.color.yellow)
//            ShellTrxStatusEnum.TRX_UNPAID ->
//                backgroundColor = resources.getColor(R.color.dodgerblue)
//            ShellTrxStatusEnum.TRX_PAID ->
//                backgroundColor = resources.getColor(R.color.limegreen)
//            ShellTrxStatusEnum.TRX_ERROR -> {
//                backgroundColor = resources.getColor(R.color.red)
//                textViewTrxStatus.setTextColor(Color.WHITE)
//            }
//        }

        var backgroundColor = Color.DKGRAY
        if (bindTrxStatus.backgroundColor != null && bindTrxStatus.backgroundColor.length > 1) {
            var colorString = bindTrxStatus.backgroundColor.substring(1).toUpperCase()
            if (colorString.length == 6) {
                colorString = "FF" + colorString // make it non-transparent
            }
            backgroundColor = (java.lang.Long.parseLong(colorString, 16)).toInt()
        }
        imageViewTrxStatus.setBackgroundColor(backgroundColor)

        if (bindTrxStatus.textColor != null && bindTrxStatus.textColor.length > 1) {
            var colorString = bindTrxStatus.textColor.substring(1).toUpperCase()
            if (colorString.length == 6) {
                colorString = "FF" + colorString // make it non-transparent
            }
            var textColor = (java.lang.Long.parseLong(colorString, 16)).toInt()
            textViewTrxStatus.setTextColor(textColor)
        }
    }


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

    fun getBindTrxStatus(): ShellTrxStatus {
        return bindTrxStatus
    }
}