package com.sambath.kunkhmer.screen.lives

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.RadioButton
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.sambath.kunkhmer.R
import com.sambath.kunkhmer.app.App
import com.sambath.kunkhmer.remote.CreateLivesRequest
import com.sambath.kunkhmer.remote.Matches
import com.sambath.kunkhmer.remote.MatchesResponse
import com.sambath.kunkhmer.screen.fighter.FighterViewModel
import com.sambath.kunkhmer.screen.fighter.FighterViewState
import kotlinx.android.synthetic.main.fragment_lives_admin.view.et_url
import kotlinx.android.synthetic.main.fragment_lives_admin.view.radio_group_status
import kotlinx.android.synthetic.main.fragment_lives_admin.view.spinner_match
import kotlinx.android.synthetic.main.fragment_lives_admin.view.submitButton


class LivesAdminFragment : Fragment() {
    private var _root: View? = null
    private val binding get() = _root!!
    private lateinit var livesViewModel: LivesViewModel
    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? {
        // Inflate the layout for this fragment
        _root = inflater.inflate(R.layout.fragment_lives_admin, container, false)
        livesViewModel = LivesViewModel(App.injectApiService(), App.injectPrefHelper())
        matchViewModel = FighterViewModel(App.injectApiService(), App.injectPrefHelper())

        livesViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
            render(it)
        })

        matchViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
            renderMatch(it)
        })
        matchViewModel.getMatches()

        setDefault()

        binding.submitButton.setOnClickListener {
            val selectedStatusId = binding.radio_group_status.checkedRadioButtonId
            var status = false
            if (selectedStatusId != -1) {
                val selectedRadioButton: RadioButton = binding.findViewById(selectedStatusId)
                val selectedValue: String = selectedRadioButton.text.toString()
                if (selectedValue == "True") {
                    status = true
                }
            }

            val createLivesRequest = CreateLivesRequest(
                selectedId, status, binding.et_url.text.toString()
            )

            //Toast.makeText(requireContext(), "$selectedId-$status",Toast.LENGTH_LONG).show()
            livesViewModel.createLives(createLivesRequest.matchFighterId, createLivesRequest.status, createLivesRequest.url)
        }

        return binding
    }

    private fun setDefault() {
        //binding.et_match_fighter_id.setText("650ea3b6b66ab4183c1657c1")
        binding.et_url.setText("https://youtu.be/xZtadif15WU")
    }

    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 render(state: LivesViewModel.LivesViewState) {
        if (state.isLoginSuccess) {
            Toast.makeText(context, "Create Success...", Toast.LENGTH_LONG).show()

            //binding.et_match_fighter_id.setText("")
            binding.et_url.setText("")
        }
        // Other state handling...
    }
}