package com.example.smartlab

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.view.Gravity
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.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.example.smartlab.databinding.ActivityCreatePatientCardBinding
import java.time.LocalDate
import java.time.format.DateTimeFormatter


class CreatePatientCardActivity : AppCompatActivity() {
    lateinit var binding: ActivityCreatePatientCardBinding
    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)
        binding = ActivityCreatePatientCardBinding.inflate(layoutInflater)
        setContentView(binding.root)

        init()
    }

    private fun init(){
        val items= resources.getStringArray(R.array.gender_name)
        val spinnerAdapter= object : ArrayAdapter<String>(this,R.layout.text_view_gender, items) {

            override fun isEnabled(position: Int): Boolean {
                return position != 0
            }

            override fun getDropDownView(
                position: Int,
                convertView: View?,
                parent: ViewGroup
            ): View {
                val view: TextView = super.getDropDownView(position, convertView, parent) as TextView
                //set the color of first item in the drop down list to gray
                if(position == 0) {
                    view.setTextColor(Color.parseColor("#939396"))
                } else {
                    //here it is possible to define color for other items by
                    //view.setTextColor(Color.RED)
                }
                return view
            }

        }

        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.genderSpinner.adapter = spinnerAdapter

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

        binding.btnNextMainCard.setOnClickListener{
            val editor: SharedPreferences.Editor =
                getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
            editor.putBoolean("isCreateCard", false).commit()
            editor.apply()
            val intent: Intent = Intent(this@CreatePatientCardActivity, MainActivity::class.java)
            startActivity(intent)
        }
    }

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

        val dialog: Dialog = Dialog(this)
        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);
    }
    fun onClickCreateCard(){
        val editor: SharedPreferences.Editor =
            getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
        editor.putBoolean("isCreateCard", false).commit()
        editor.apply()
        val intent: Intent = Intent(this@CreatePatientCardActivity, MainActivity::class.java)
        startActivity(intent)
    }

}