package com.example.courseworkkotlin

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.children
import com.example.courseworkkotlin.databinding.ActivityCalculatorBinding
import java.util.Locale

class Calculator : AppCompatActivity() {
    private lateinit var binding: ActivityCalculatorBinding
    private var hours = DoubleArray(22)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCalculatorBinding.inflate(layoutInflater)
        setContentView(binding.root)

        disableGroups()

        setUpListeners()

        checkDefault()

        setTime()
    }

    private fun disableGroups() {
        binding.Art.children.forEach { it.isEnabled = false }
        binding.IT.children.forEach { it.isEnabled = false }
        binding.Tech.children.forEach { it.isEnabled = false }

        binding.Alg.children.forEach { it.isEnabled = false }
        binding.Geom.children.forEach { it.isEnabled = false }
        binding.Law.children.forEach { it.isEnabled = false }
        binding.Econ.children.forEach { it.isEnabled = false }
    }

    private fun checkDefault() {
        binding.UALanSt.isChecked = true
        binding.UALitSt.isChecked = true
        binding.FLitSt.isChecked = true
        binding.FLanSt.isChecked = true
        binding.UAHistSt.isChecked = true
        binding.FHistSt.isChecked = true
        binding.AnFLanSt.isChecked = true
        binding.MathSt.isChecked = true
        binding.PhysAstrSt.isChecked = true
        binding.BioEcoSt.isChecked = true
        binding.ChemSt.isChecked = true
        binding.GeoSt.isChecked = true
        binding.PESt.isChecked = true
        binding.MilitSt.isChecked = true
        binding.SocEdSt.isChecked = true
    }

    private fun setUpListeners() {
        binding.UALan.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.UALanSt -> hours[0] = 2.0
                R.id.UALanPro -> hours[0] = 4.0
            }
            setTime()
        }

        binding.UALit.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.UALitSt -> hours[1] = 2.0
                R.id.UALitPro -> hours[1] = 4.0
            }
            setTime()
        }

        binding.FLit.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.FLitSt -> hours[2] = 1.0
                R.id.FLitPro -> hours[2] = 3.0
            }
            setTime()
        }

        binding.FLan.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.FLanSt -> hours[3] = 2.0
                R.id.FLanPro -> hours[3] = 5.0
            }
            setTime()
        }

        binding.UAHist.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.UAHistSt -> hours[4] = 1.5
                R.id.UAHistPro -> hours[4] = 3.0
            }
            setTime()
        }

        binding.FHist.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.FHistSt -> hours[5] = 1.0
                R.id.FHistPro -> hours[5] = 3.0
            }
            setTime()
        }

        binding.AnFLan.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.AnFLanSt -> hours[6] = 0.0
                R.id.AnFLanPro -> hours[6] = 3.0
            }
            setTime()
        }

        binding.Math.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.MathSt -> {
                    hours[7] = 3.0
                    binding.AlgSt.isChecked = true
                    binding.GeomSt.isChecked = true
                }
                R.id.MathPro -> {
                    hours[7] = 0.0
                    binding.AlgPro.isChecked = true
                    binding.GeomPro.isChecked = true
                }
            }
            setTime()
        }

        binding.Alg.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.AlgSt -> hours[8] = 0.0
                R.id.AlgPro -> hours[8] = 6.0
            }
        }

        binding.Geom.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.GeomSt -> hours[9] = 0.0
                R.id.GeomPro -> hours[9] = 3.0
            }
        }

        binding.PhysAstr.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.PhysAstrSt -> hours[10] = 3.0
                R.id.PhysAstrPro -> hours[10] = 6.0
            }
            setTime()
        }

        binding.BioEco.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.BioEcoSt -> hours[11] = 2.0
                R.id.BioEcoPro -> hours[11] = 5.0
            }
            setTime()
        }

        binding.Chem.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.ChemSt -> hours[12] = 1.5
                R.id.ChemPro -> hours[12] = 4.0
            }
            setTime()
        }

        binding.Geo.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.GeoSt -> hours[13] = 1.5
                R.id.GeoPro -> hours[13] = 5.0
            }
            setTime()
        }

        binding.PE.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.PESt -> hours[14] = 2.0
                R.id.PEPro -> hours[14] = 6.0
            }
            setTime()
        }

        binding.Milit.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.MilitSt -> hours[15] = 1.5
                R.id.MilitPro -> hours[15] = 5.0
            }
            setTime()
        }

        binding.SocEd.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.SocEdSt -> {
                    hours[16] = 2.0
                    binding.LawSt.isChecked = true
                    binding.EconSt.isChecked = true
                }
                R.id.SocEdPro -> {
                    hours[16] = 0.0
                    binding.LawPro.isChecked = true
                    binding.EconPro.isChecked = true
                }
            }
            setTime()
        }

        binding.Law.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.LawSt -> hours[17] = 0.0
                R.id.LawPro -> hours[17] = 3.0
            }
        }

        binding.Econ.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.EconSt -> hours[18] = 0.0
                R.id.EconPro -> hours[18] = 3.0
            }
        }

        binding.IT.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.ITSt -> hours[19] = 3.0
                R.id.ITPro -> hours[19] = 5.0
                else -> hours[19] = 0.0
            }
            setTime()
        }

        binding.Tech.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.TechSt -> hours[20] = 3.0
                R.id.TechPro -> hours[20] = 5.0
                else -> hours[20] = 0.0
            }
            setTime()
        }

        binding.Art.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.ArtSt -> hours[21] = 3.0
                R.id.ArtPro -> hours[21] = 5.0
                else -> hours[21] = 0.0
            }
            setTime()
        }

        binding.checkBoxIT.setOnCheckedChangeListener { _, isChecked ->
            binding.IT.children.forEach { it.isEnabled = isChecked }
            if (isChecked) binding.ITSt.isChecked = true
            else binding.IT.clearCheck()
            setTime()
        }

        binding.checkBoxTech.setOnCheckedChangeListener { _, isChecked ->
            binding.Tech.children.forEach { it.isEnabled = isChecked }
            if (isChecked) binding.TechSt.isChecked = true
            else binding.Tech.clearCheck()
            setTime()
        }

        binding.checkBoxArt.setOnCheckedChangeListener { _, isChecked ->
            binding.Art.children.forEach { it.isEnabled = isChecked }
            if (isChecked) binding.ArtSt.isChecked = true
            else binding.Art.clearCheck()
            setTime()
        }

        binding.home.setOnClickListener { startActivity(Intent(this, HomeScreen::class.java)) }
    }
    
    private fun setTime() {
        val totalHours = hours.sum()

        if (totalHours >= 33f)  binding.Hours.setTextColor(Color.RED)
        else  binding.Hours.setTextColor(Color.BLACK)

        binding.Hours.text = String.format(Locale("UA"), "Годин вибрано: %.1f", totalHours)
    }
}