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

import android.content.Context
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.GridView
import android.widget.ImageView
import android.widget.TextView
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.listener.OnPropertyChangedListener
import com.wayne.www.waynelib.ui.R
import com.wayne.www.waynelib.ui.adapter.ShellTrxListViewAdapter
import com.wayne.www.waynelib.ui.widget.NestedListview

class ShellNozzleGridView : FrameLayout, OnPropertyChangedListener {
    internal var LOG_TAG = "ShellNozzleGridView"

    private val onItemViewButtonClickListener = java.util.ArrayList<OnItemViewButtonClickListener>()
    private var numOfNozzles = 0
    private var nozzleName = ""
    private var parentGridView: GridView? = null
    private var nozzleId = ""


    override fun onPropertyChanged(sender: Any?, propertyName: String?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    constructor(context: Context, numOfNozzles: Int, nozzleName: String, gridView: GridView, nozzleId: String) : super(context) {
        this.numOfNozzles = numOfNozzles
        this.nozzleName = nozzleName
        this.parentGridView = gridView
        this.nozzleId = nozzleId
        sharedConstructing(context)
    }

    private var listViewTrxs: NestedListview? = null
    private var adapterTrxs: ShellTrxListViewAdapter? = null
    private var textViewNozzleName: TextView? = null
    private var nozzleView: ImageView? = null

    private fun sharedConstructing(context: Context?) {
        val inflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val view = inflater.inflate(R.layout.layout_shell_nozzle_grid_view, null, false)
        this.addView(view,
                FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))

        //nozzleView = findViewById(R.id.nozzle_view_background) as ImageView
        view?.setOnClickListener{
            Handler().post {
                for (l in this@ShellNozzleGridView.onItemViewButtonClickListener) {
                    l.onItemViewClicked(this@ShellNozzleGridView, -1, ClickType.NozzleClick)
                }
            }
        }

        textViewNozzleName = findViewById(R.id.shell_nozzle_description) as TextView
        textViewNozzleName?.setText(nozzleName)

        listViewTrxs = findViewById(R.id.list_view_shell_trxs) as NestedListview
        listViewTrxs?.setPadding(10, 20, 10, 0)
        listViewTrxs?.scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY

        listViewTrxs?.setParent(parentGridView)
//        listViewTrxs?.setOnItemLongClickListener { parent, view, position, id ->
//            //fileLogger.debug("LongClick on PumpView: " + this@NozzleGridView.getBindNozzle()?.NozzleNo)
//            Handler().post {
//                for (l in this@ShellNozzleGridView.onItemViewButtonClickListener) {
//                    l.onItemViewClicked(this@ShellNozzleGridView, position, ClickType.LongClick)
//                }
//            }
//            true
//        }

//        listViewTrxs?.setOnItemClickListener { adapterView, view, position, l ->
//            //fileLogger.debug("LongClick on PumpView: " + this@NozzleGridView.getBindNozzle()?.NozzleNo)
//            Handler().post {
//                for (l in this@ShellNozzleGridView.onItemViewButtonClickListener) {
//                    l.onItemViewClicked(this@ShellNozzleGridView, position, ClickType.LongClick)
//                }
//            }
//            true
//        }

        adapterTrxs = ShellTrxListViewAdapter(context, object : OnItemViewButtonClickListener {
            override fun onItemViewClicked(sender: ViewGroup, clickedItemId: Any?, clickType: ClickType) {
                if (sender is ShellTrxStatusView) {
                    when (clickType) {
                        ClickType.LongClick -> {
                            for (l in this@ShellNozzleGridView.onItemViewButtonClickListener) {
                                l.onItemViewClicked(this@ShellNozzleGridView, sender, ClickType.LongClick)
                            }
                        }
                        ClickType.SingleClick -> {
                            for (l in this@ShellNozzleGridView.onItemViewButtonClickListener) {
                                l.onItemViewClicked(this@ShellNozzleGridView, sender, ClickType.SingleClick)
                                //listViewTrxs?.setItemChecked(clickedItemId as Int, true)
                            }
                        }
                    }
                }
            }
        })
        listViewTrxs!!.adapter = adapterTrxs
    }

    fun setData(data: List<ShellTrxStatus?>?) {
        adapterTrxs?.setData(data)
        adapterTrxs?.notifyDataSetChanged()
        listViewTrxs?.setSelection(0)
    }

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

    fun removeViewButtonClickListener() {
        this.onItemViewButtonClickListener.clear()
    }
    fun getNozzleName() : String  {
        return nozzleName
    }

    fun getNozzleId() : String  {
        return nozzleId
    }
}