package com.casic.detector.widgets

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.*
import com.casic.detector.R
import com.pengxh.kt.lite.extensions.initDialogLayoutParams
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.widget.DeleteEditText

class QueryMarkerDialog private constructor(builder: Builder) : Dialog(
    builder.context, R.style.UserDefinedDialogStyle
) {
    private val ctx = builder.context
    private val title = builder.title
    private val spinnerArray = builder.spinnerArray
    private val hint = builder.hint
    private val positiveBtn = builder.positiveBtn
    private val negativeBtn = builder.negativeBtn
    private val listener = builder.listener

    class Builder {
        lateinit var context: Context
        lateinit var title: String
        lateinit var spinnerArray: Array<String>
        lateinit var hint: String
        lateinit var positiveBtn: String
        lateinit var negativeBtn: String
        lateinit var listener: OnDialogButtonClickListener

        fun setContext(context: Context): Builder {
            this.context = context
            return this
        }

        fun setTitle(title: String): Builder {
            this.title = title
            return this
        }

        fun setSpinnerArray(spinnerArray: Array<String>): Builder {
            this.spinnerArray = spinnerArray
            return this
        }

        fun setHintMessage(hint: String): Builder {
            this.hint = hint
            return this
        }

        fun setPositiveButton(name: String): Builder {
            positiveBtn = name
            return this
        }

        fun setNegativeButton(name: String): Builder {
            negativeBtn = name
            return this
        }

        fun setOnDialogButtonClickListener(listener: OnDialogButtonClickListener): Builder {
            this.listener = listener
            return this
        }

        fun build(): QueryMarkerDialog {
            return QueryMarkerDialog(this)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.initDialogLayoutParams(0.8f)
        setContentView(R.layout.dialog_query_marker)
        setCanceledOnTouchOutside(false)
        initView()
    }

    private var selectedItem = ""

    private fun initView() {
        val dialogTitleView: TextView = findViewById(R.id.dialogTitleView)
        val dialogSpinnerView: Spinner = findViewById(R.id.dialogSpinnerView)
        val dialogInputView: DeleteEditText = findViewById(R.id.dialogInputView)
        val dialogCancelButton = findViewById<Button>(R.id.dialogCancelButton)
        val dialogConfirmButton = findViewById<Button>(R.id.dialogConfirmButton)
        if (title.isNotBlank()) {
            dialogTitleView.text = title
        }

        if (spinnerArray.isNotEmpty()) {
            val arrayAdapter = ArrayAdapter(
                ctx, android.R.layout.simple_spinner_dropdown_item, spinnerArray
            )
            dialogSpinnerView.setSelection(0)
            selectedItem = spinnerArray[0]
            dialogSpinnerView.adapter = arrayAdapter
            dialogSpinnerView.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(
                    adapterView: AdapterView<*>?, view: View?, position: Int, id: Long
                ) {
                    selectedItem = spinnerArray[position]
                }

                override fun onNothingSelected(p0: AdapterView<*>?) {

                }
            }
        }

        if (hint.isNotBlank()) {
            dialogInputView.hint = hint
        }
        if (negativeBtn.isNotBlank()) {
            dialogCancelButton.text = negativeBtn
        }
        dialogCancelButton.setOnClickListener {
            listener.onCancelClick()
            dismiss()
        }
        if (positiveBtn.isNotBlank()) {
            dialogConfirmButton.text = positiveBtn
        }
        dialogConfirmButton.setOnClickListener(View.OnClickListener {
            val inputValue = dialogInputView.text.toString().trim()
            if (inputValue.isBlank()) {
                "输入错误，请检查！".show(ctx)
                return@OnClickListener
            }
            listener.onConfirmClick(selectedItem, inputValue)
            dismiss()
        })
    }

    interface OnDialogButtonClickListener {
        fun onConfirmClick(selectedItem: String, value: String)

        fun onCancelClick()
    }
}