package com.casic.detector.widgets

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.casic.detector.R
import com.casic.detector.databinding.DialogQueryMarkerBinding
import com.casic.detector.utils.DataBaseManager
import com.pengxh.kt.lite.extensions.binding
import com.pengxh.kt.lite.extensions.initDialogLayoutParams
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


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 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 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 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)
        }
    }

    private val binding: DialogQueryMarkerBinding by binding()

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

    private fun initView() {
        if (title.isNotBlank()) {
            binding.dialogTitleView.text = title
        }

        if (spinnerArray.isNotEmpty()) {
            val arrayAdapter = ArrayAdapter(
                ctx, android.R.layout.simple_spinner_dropdown_item, spinnerArray
            )
            binding.dialogSpinnerView.setSelection(0)
            binding.dialogSpinnerView.adapter = arrayAdapter
            binding.dialogSpinnerView.onItemSelectedListener =
                object : AdapterView.OnItemSelectedListener {
                    override fun onItemSelected(
                        adapterView: AdapterView<*>?, view: View?, position: Int, id: Long
                    ) {
                        //查库
                        val autoCompleteData = HashSet<String>()
                        CoroutineScope(Dispatchers.Main).launch {
                            val labelBeans = withContext(Dispatchers.IO) {
                                DataBaseManager.get.loadLabelByCondition(
                                    spinnerArray[position],
                                    null
                                )
                            }
                            for (labelBean in labelBeans) {
                                when (position) {
                                    0 -> autoCompleteData.add(labelBean.identifierId)
                                    1 -> autoCompleteData.add(labelBean.area)
                                    2 -> autoCompleteData.add(labelBean.line)
                                    3 -> autoCompleteData.add(labelBean.road)
                                    4 -> autoCompleteData.add(labelBean.owner)
                                    5 -> autoCompleteData.add(labelBean.person)
                                    6 -> autoCompleteData.add(labelBean.installTime)
                                    else -> autoCompleteData.add(labelBean.remark)
                                }
                            }

                            //AppCompatAutoCompleteTextView逻辑
                            val autoCompleteAdapter = ArrayAdapter(
                                ctx,
                                android.R.layout.simple_dropdown_item_1line,
                                autoCompleteData.toArray()
                            )
                            binding.dialogInputView.setAdapter(autoCompleteAdapter)
                        }
                    }

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

                    }
                }
        }

        if (negativeBtn.isNotBlank()) {
            binding.dialogCancelButton.text = negativeBtn
        }
        binding.dialogCancelButton.setOnClickListener {
            listener.onCancelClick()
            dismiss()
        }
        if (positiveBtn.isNotBlank()) {
            binding.dialogConfirmButton.text = positiveBtn
        }
        binding.dialogConfirmButton.setOnClickListener {
            listener.onConfirmClick(
                binding.dialogSpinnerView.selectedItem.toString(),
                binding.dialogInputView.text.toString().trim()
            )
            dismiss()
        }
    }

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

        fun onCancelClick()
    }
}