package com.sambath.kunkhmer.screen.event

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import com.sambath.kunkhmer.R
import com.sambath.kunkhmer.app.App
import com.sambath.kunkhmer.remote.Matches
import com.sambath.kunkhmer.screen.fighter.FighterViewModel
import com.sambath.kunkhmer.screen.fighter.FighterViewState
import kotlinx.android.synthetic.main.fragment_event_admin.view.et_desc
import kotlinx.android.synthetic.main.fragment_event_admin.view.et_title
import kotlinx.android.synthetic.main.fragment_event_admin.view.submitButton
import kotlinx.android.synthetic.main.fragment_lives_admin.view.spinner_match

class EventAdminFragment : Fragment() {
    private var _root: View? = null
    private val binding get() = _root!!
    private lateinit var eventViewModel: EventViewModel
    private lateinit var matchViewModel: FighterViewModel
    private lateinit var selectedId: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _root = inflater.inflate(R.layout.fragment_event_admin, container, false)

        eventViewModel = EventViewModel(App.injectApiService(), App.injectPrefHelper())
        eventViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
            render(it)
        })

        matchViewModel = FighterViewModel(App.injectApiService(), App.injectPrefHelper())
        matchViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
            renderMatch(it)
        })
        matchViewModel.getMatches()

        setDefaultData()
        binding.submitButton.setOnClickListener{
            eventViewModel.createEvent(
                binding.et_title.text.toString(),
                binding.et_desc.text.toString(),
                selectedId
            )
        }

        // Inflate the layout for this fragment
        return binding
    }

    private fun renderMatch(state: FighterViewState) {
        if (state.isLoginSuccess) {
            val matchesResponse = state.matchesData
            val matches: List<Matches> = matchesResponse!!.obj
            // Ensure matches is not null before mapping
            val dropdownItems = matches?.map { it.title  }

            val adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, dropdownItems)
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            binding.spinner_match.adapter = adapter
            binding.spinner_match.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(parentView: AdapterView<*>, selectedItemView: View, position: Int, id: Long) {
                    //  Get the selected Matches object
                    val selectedMatch = matches[position]

                    // Access the corresponding ID (it.matchInfo.id)
                    selectedId = selectedMatch.matchInfo
                }

                override fun onNothingSelected(parentView: AdapterView<*>) {
                    // Do nothing here
                }
            }

        }
    }

    private fun setDefaultData() {
        binding.et_title.setText("Prum Samnang VS Thoeun Theara")
        binding.et_desc.setText("Prum Samnang VS Thoeun Theara")
    }

    private fun render(state: EventViewModel.EventViewState) {
        if (state.isLoginSuccess) {
            Toast.makeText(context, "Create Success...", Toast.LENGTH_LONG).show()

            binding.et_title.setText("")
            binding.et_desc.setText("")
        }
        // Other state handling...
    }
}