package com.example.smartlab

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.ArrayAdapter
import android.widget.DatePicker
import android.widget.EditText
import android.widget.Spinner
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import com.example.smartlab.databinding.FragmentPatientCardBinding
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Calendar
import java.util.Date
import java.util.Locale

class PatientCardFragment : Fragment(R.layout.fragment_patient_card) {
    private var _binding: FragmentPatientCardBinding? = null
    private val binding get() = _binding!!
    lateinit var dp_text: EditText

    private var _year = 1995
    private var _month = 0
    private var _day = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentPatientCardBinding.inflate(inflater, container, false)

        val items = resources.getStringArray(R.array.gender_name)
        val AdapterForSpiner =
            object : ArrayAdapter<String>(activity as Context, R.layout.text_view_gender, items) {
                override fun isEnabled(position: kotlin.Int): kotlin.Boolean {
                    return position != 0
                }

                override fun getDropDownView(
                    position: kotlin.Int,
                    convertView: android.view.View?,
                    parent: android.view.ViewGroup
                ): android.view.View {
                    val view: android.widget.TextView = super.getDropDownView(
                        position,
                        convertView,
                        parent
                    ) as android.widget.TextView
                    //set the color of first item in the drop down list to gray
                    if (position == 0) {
                        view.setTextColor(android.graphics.Color.parseColor("#939396"))
                    } else {
                        //here it is possible to define color for other items by
                        //view.setTextColor(Color.RED)
                    }
                    return view
                }
            }
        AdapterForSpiner.setDropDownViewResource(R.layout.text_view_gender)
        val spin: Spinner = binding.genderSpinner
        spin.adapter = AdapterForSpiner;
        spin.setSelection(0);

        dp_text = binding.editDPText
        dp_text.setOnClickListener {
            onClickChangeDB()
        }

        return binding.root
    }

    fun onClickChangeCard() {

    }

    @SuppressLint("SimpleDateFormat")
    @RequiresApi(Build.VERSION_CODES.O)
    private fun onClickChangeDB() {

        val dialog: Dialog = Dialog(activity as Context)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(R.layout.datepicker_view_db)

        val _dp: DatePicker = dialog.findViewById(R.id.date_picker_date_of_birthday)

        val formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy")
        _dp.init(_year, _month, _day, null)
        _dp.setOnDateChangedListener { picker, year, month, day ->
            val _thisDate = LocalDate.of(year, month + 1, day)
            _year = _dp.year
            _day = _dp.dayOfMonth
            _month = _dp.month
            dp_text.setText("" + _thisDate.format(formatter))
        }

        dialog.show();
        dialog.getWindow()
            ?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow()?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.getWindow()?.getAttributes()?.windowAnimations = R.style.DialogAnimation;
        dialog.getWindow()?.setGravity(Gravity.BOTTOM);
    }
}

