package com.sambath.kunkhmer.screen.news

import android.app.DatePickerDialog
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.sambath.kunkhmer.R
import com.sambath.kunkhmer.app.App
import kotlinx.android.synthetic.main.fragment_highlight_admin.view.et_title
import kotlinx.android.synthetic.main.fragment_highlight_admin.view.et_url
import kotlinx.android.synthetic.main.fragment_highlight_admin.view.submitButton
import kotlinx.android.synthetic.main.fragment_highlight_admin.view.til_date
import java.text.SimpleDateFormat
import java.util.Calendar

class HighlightAdminFragment : Fragment() {
    private var _root: View? = null
    private val binding get() = _root!!
    private lateinit var highlightViewModel: NewsViewModel

    private val calendar = Calendar.getInstance()
    private val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000")


    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_highlight_admin, container, false)

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

        binding.til_date.setOnClickListener {
            showDatePickerDialog()
        }

        binding.submitButton.setOnClickListener{
            highlightViewModel.createHighlight(
                binding.et_title.text.toString(),
                binding.til_date.text.toString(),
                binding.et_url.text.toString()
            )
        }

        setDefault()

        return binding
    }

    private fun setDefault() {
        binding.et_title.setText("Thiago (Kun Khmer) Vs Joe (Muay Thai)")
        binding.et_url.setText("https://youtu.be/K9sHyZE5BmI")
    }

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

            binding.til_date.text = ""
            binding.et_title.setText("")
            binding.et_url.setText("")
        }
        // Other state handling...
    }

    private fun showDatePickerDialog() {
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)

        val datePickerDialog = DatePickerDialog(
            requireContext(),
            DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
                // Update the calendar with the selected date
                calendar.set(year, monthOfYear, dayOfMonth)

                // Format the selected date and update the EditText text
                val formattedDate = dateFormat.format(calendar.time)
                binding.til_date.setText(formattedDate)
            },
            year,
            month,
            day
        )

        datePickerDialog.show()
    }
}