package com.example.smart119.views

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.PopupWindow
import android.widget.Spinner
import android.widget.TextView
import android.widget.Toast
import com.example.smart119.R
import com.example.smart119.datas.HospitalSearch
import com.google.android.material.textfield.TextInputEditText

object HospitalSearchPopup {

    fun showPopupHospitalSearch(view: View, context: Context): PopupWindow {
        // PopupView 레이아웃을 인플레이트
        val inflater = LayoutInflater.from(view.context)
        val popupView = inflater.inflate(R.layout.popup_search, null)

        val displayMetrics = view.resources.displayMetrics
        val screenWidth = displayMetrics.widthPixels
        val screenHeight = displayMetrics.heightPixels

        // PopupWindow 생성
        val popupWindow = PopupWindow(
            popupView,
//            LinearLayout.LayoutParams.WRAP_CONTENT,
//            LinearLayout.LayoutParams.MATCH_PARENT,
            (screenWidth * 0.4).toInt(), // 너비를 화면 너비의 80%로 설정
            (screenHeight * 0.4).toInt(), // 높이를 화면 높이의 60%로 설정
            true // 외부 클릭으로 닫기 가능
        )

        // PopupWindow 표시 (뷰 기준 위치)
//        popupWindow.showAsDropDown(view, 220, -220) // 기준 뷰 아래에 표시

        // 화면의 중앙에 표시
        popupWindow.showAtLocation(
            view, // 기준이 되는 뷰 (보통은 최상위 뷰)
            Gravity.END, // 중앙 정렬
            250, // xOffset
            -95  // yOffset
        )
        // PopupView 내의 컴포넌트 제어
        val popupText = popupView.findViewById<TextView>(R.id.popupText)
        val saveButton = popupView.findViewById<Button>(R.id.savePopup)
//        saveButton.position

        // 닫기 버튼 클릭 시 PopupWindow 닫기
        saveButton.setOnClickListener {
            val popupView = popupWindow.contentView
            val siSpin = popupView.findViewById<Spinner>(R.id.siSelectSpinner);
            val gunSpin = popupView.findViewById<Spinner>(R.id.guGunSelectSpinner);

            val editText = popupView.findViewById<TextInputEditText>(R.id.relateInput)
            HospitalSearch.setSearchFilter(
                siSpin.selectedItem.toString(),
                gunSpin.selectedItem.toString(),
                editText.text.toString()
            )
            popupWindow.dismiss()
        }

        val siSpinner: Spinner = popupView.findViewById(R.id.siSelectSpinner)
        val guGunSpinner: Spinner = popupView.findViewById(R.id.guGunSelectSpinner)

//        val sidefault = getString(R.string.si_spinner_prompt)
//        val siItems = mutableListOf("시도선택")
//        val arrAdapterSi = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, siItems)
//        siSpinner.adapter = arrAdapterSi;

        siSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
                val selectedItem = parent.getItemAtPosition(position).toString()
//                Toast.makeText(context, "선택된 항목: $selectedItem", Toast.LENGTH_SHORT).show()

                if (selectedItem != "" && selectedItem != "시도선택") {
                    Toast.makeText(context, "선택된 항목: $selectedItem", Toast.LENGTH_SHORT).show()
                    HospitalSearch.req_gun_list(selectedItem)
                }
                else {

                }
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                // 아무것도 선택되지 않았을 때의 동작 (필요 없으면 빈 상태로 둬도 됨)
                Toast.makeText(context, "선택되지 않음", Toast.LENGTH_SHORT).show()
            }
        }

        guGunSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
                val selectedItem = parent.getItemAtPosition(position).toString()
//                Toast.makeText(context, "선택된 항목: $selectedItem", Toast.LENGTH_SHORT).show()

                if (selectedItem != "" && selectedItem != "구군선택") {
                    Toast.makeText(context, "선택된 항목: $selectedItem", Toast.LENGTH_SHORT).show()
                }
                else {

                }
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                // 아무것도 선택되지 않았을 때의 동작 (필요 없으면 빈 상태로 둬도 됨)
                Toast.makeText(context, "선택되지 않음", Toast.LENGTH_SHORT).show()
            }
        }

        val guGunItems = mutableListOf("구군선택")
        val arrAdapterguGun = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, guGunItems)
        guGunSpinner.adapter = arrAdapterguGun;

        return popupWindow
    }

    fun set_POPUP_SIDO_COMBO(view: PopupWindow, context: Context, items:List<String>) {
        val popupView = view.contentView

        val siSpinner: Spinner = popupView.findViewById(R.id.siSelectSpinner)
        val arrAdapterSi = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, items)
        siSpinner.adapter = arrAdapterSi;

        arrAdapterSi.notifyDataSetChanged()
    }

    fun set_POPUP_GUGUN_COMBO(view: PopupWindow, context: Context, items:List<String>) {
        val popupView = view.contentView

        val guGunSpinner: Spinner = popupView.findViewById(R.id.guGunSelectSpinner)
        val arrAdapterGuGun = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, items)
        guGunSpinner.adapter = arrAdapterGuGun;

        arrAdapterGuGun.notifyDataSetChanged()
    }
}