package com.rhinemann.project;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class Calculator extends AppCompatActivity {

    RadioGroup UALan;
    RadioGroup UALit;
    RadioGroup FLit;
    RadioGroup FLan;
    RadioGroup UAHist;
    RadioGroup FHist;
    RadioGroup AnFLan;
    RadioGroup Math;
    RadioGroup Alg;
    RadioGroup Geom;
    RadioGroup PhysAstr;
    RadioGroup BioEco;
    RadioGroup Chem;
    RadioGroup Geo;
    RadioGroup PE;
    RadioGroup Milit;
    RadioGroup SocEd;
    RadioGroup Law;
    RadioGroup Econ;
    RadioGroup IT;
    RadioGroup Tech;
    RadioGroup Art;

    CheckBox checkBox_IT;
    CheckBox checkBox_Tech;
    CheckBox checkBox_Art;

    TextView HoursTaken;

    Float UALan_Hours = 2f;
    Float UALit_Hours = 2f;
    Float FLit_Hours = 1f;
    Float FLan_Hours = 2f;
    Float UAHist_Hours = 1.5f;
    Float FHist_Hours = 1f;
    Float AnFLan_Hours = 0f;
    Float Math_Hours = 3f;
    Float Alg_Hours = 0f;
    Float Geom_Hours = 0f;
    Float PhysAstr_Hours = 3f;
    Float BioEco_Hours = 2f;
    Float Chem_Hours = 1.5f;
    Float Geo_Hours = 1.5f;
    Float PE_Hours = 2f;
    Float Milit_Hours = 1.5f;
    Float SocEd_Hours = 2f;
    Float Law_Hours = 0f;
    Float Econ_Hours = 0f;
    Float IT_Hours = 0f;
    Float Tech_Hours = 0f;
    Float Art_Hours = 0f;

    Button Home;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        UALan = findViewById(R.id.UALan);
        UALit = findViewById(R.id.UALit);
        FLit = findViewById(R.id.FLit);
        FLan = findViewById(R.id.FLan);
        UAHist = findViewById(R.id.UAHist);
        FHist = findViewById(R.id.FHist);
        AnFLan = findViewById(R.id.AnFLan);
        Math = findViewById(R.id.Math);
        Alg = findViewById(R.id.Alg);
        Geom = findViewById(R.id.Geom);
        PhysAstr = findViewById(R.id.PhysAstr);
        BioEco = findViewById(R.id.BioEco);
        Chem = findViewById(R.id.Chem);
        Geo = findViewById(R.id.Geo);
        PE = findViewById(R.id.PE);
        Milit = findViewById(R.id.Milit);
        SocEd = findViewById(R.id.SocEd);
        Law = findViewById(R.id.Law);
        Econ = findViewById(R.id.Econ);
        IT = findViewById(R.id.IT);
        Tech = findViewById(R.id.Tech);
        Art = findViewById(R.id.Art);

        HoursTaken = findViewById(R.id.Hours);

        Home = findViewById(R.id.home);

        Home.setOnClickListener(v -> {
            Intent intent = new Intent(Calculator.this, HomeScreen.class);
            startActivity(intent);
        });

        checkBox_IT = findViewById(R.id.checkBox_IT);
        checkBox_Tech = findViewById(R.id.checkBox_Tech);
        checkBox_Art = findViewById(R.id.checkBox_Art);

        for (int i = 0; i < IT.getChildCount(); i++) {
            IT.getChildAt(i).setEnabled(false);
        }

        for (int i = 0; i < IT.getChildCount(); i++) {
            Tech.getChildAt(i).setEnabled(false);
        }

        for (int i = 0; i < IT.getChildCount(); i++) {
            Art.getChildAt(i).setEnabled(false);
        }

        UALan.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.UALanSt) UALan_Hours = 2f;
            else if (checkedId == R.id.UALanPro) UALan_Hours = 4f;

            Final();
        });

        UALit.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.UALitSt) UALit_Hours = 2f;
            else if (checkedId == R.id.UALitPro) UALit_Hours = 4f;

            Final();
        });

        FLit.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.FLitSt) FLit_Hours = 1f;
            else if (checkedId == R.id.FLitPro) FLit_Hours = 3f;

            Final();
        });

        FLan.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.FLanSt) FLan_Hours = 2f;
            else if (checkedId == R.id.FLanPro) FLan_Hours = 5f;

            Final();
        });

        UAHist.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.UAHistSt) UAHist_Hours = 1.5f;
            else if (checkedId == R.id.UAHistPro) UAHist_Hours = 3f;

            Final();
        });

        FHist.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.FHistSt) FHist_Hours = 1f;
            else if (checkedId == R.id.FHistPro) FHist_Hours = 3f;

            Final();
        });

        AnFLan.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.AnFLanSt) AnFLan_Hours = 0f;
            else if (checkedId == R.id.AnFLanPro) AnFLan_Hours = 3f;

            Final();
        });

        Math.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.MathSt) Math_Hours = 3f;
            else if (checkedId == R.id.MathPro) Math_Hours = 0f;

            Final();
        });

        Alg.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.AlgSt) Alg_Hours = 0f;
            else if (checkedId == R.id.AlgPro) Alg_Hours = 6f;

            Final();
        });

        Geom.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.GeomSt) Geom_Hours = 0f;
            else if (checkedId == R.id.GeomPro) Geom_Hours = 3f;

            Final();
        });

        PhysAstr.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.PhysAstrSt) PhysAstr_Hours = 3f;
            else if (checkedId == R.id.PhysAstrPro) PhysAstr_Hours = 6f;

            Final();
        });

        BioEco.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.BioEcoSt) BioEco_Hours = 2f;
            else if (checkedId == R.id.BioEcoPro) BioEco_Hours = 5f;

            Final();
        });

        Chem.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.ChemSt) Chem_Hours = 1.5f;
            else if (checkedId == R.id.ChemPro) Chem_Hours = 4f;

            Final();
        });

        Geo.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.GeoSt) Geo_Hours = 1.5f;
            else if (checkedId == R.id.GeoPro) Geo_Hours = 5f;

            Final();
        });

        PE.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.PESt) {
                PE_Hours = 2f;
            } else if (checkedId == R.id.PEPro) {
                PE_Hours = 6f;
            }

            Final();
        });

        Milit.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.MilitSt) Milit_Hours = 1.5f;
            else if (checkedId == R.id.MilitPro) Milit_Hours = 5f;

            Final();
        });

        SocEd.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.SocEdSt) SocEd_Hours = 2f;
            else if (checkedId == R.id.SocEdPro) SocEd_Hours = 0f;

            Final();
        });

        Law.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.LawSt) Law_Hours = 0f;
            else if (checkedId == R.id.LawPro) Law_Hours = 3f;

            Final();
        });

        Econ.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            if (checkedId == R.id.EconSt) Econ_Hours = 0f;
            else if (checkedId == R.id.EconPro) Econ_Hours = 3f;

            Final();
        });

        checkBox_IT.setOnCheckedChangeListener((buttonView, isChecked) -> {
            for (int i = 0; i < IT.getChildCount(); i++) {
                IT.getChildAt(i).setEnabled(isChecked);
            }

            if (checkBox_IT.isChecked()) {
                IT_Hours = 3f;
                IT.check(R.id.ITSt);

                IT.setOnCheckedChangeListener((radioGroup, checkedId) -> {
                    if (checkBox_IT.isChecked()) {
                        if (checkedId == R.id.ITSt) IT_Hours = 3f;
                        else if (checkedId == R.id.ITPro) IT_Hours = 5f;
                    } else IT_Hours = 0f;
                });
            } else {
                IT.clearCheck();
                IT_Hours = 0f;
            }

            Final();
        });

        checkBox_Tech.setOnCheckedChangeListener((buttonView, isChecked) -> {
            for (int i = 0; i < Tech.getChildCount(); i++) {
                Tech.getChildAt(i).setEnabled(isChecked);
            }

            if (checkBox_Tech.isChecked()) {
                Tech_Hours = 3f;
                Tech.check(R.id.TechSt);

                Tech.setOnCheckedChangeListener((radioGroup, checkedId) -> {
                    if (checkBox_Tech.isChecked()) {
                        if (checkedId == R.id.TechSt) Tech_Hours = 3f;
                        else if (checkedId == R.id.TechPro) Tech_Hours = 5f;
                    } else Tech_Hours = 0f;
                });
            } else {
                Tech.clearCheck();
                Tech_Hours = 0f;
            }

            Final();
        });

        checkBox_Art.setOnCheckedChangeListener((buttonView, isChecked) -> {
            for (int i = 0; i < Art.getChildCount(); i++) {
                Art.getChildAt(i).setEnabled(isChecked);
            }

            if (checkBox_Art.isChecked()) {
                Art_Hours = 3f;
                Art.check(R.id.ArtSt);

                Art.setOnCheckedChangeListener((radioGroup, checkedId) -> {
                    if (checkBox_Art.isChecked()) {
                        if (checkedId == R.id.ArtSt) Art_Hours = 3f;
                        else if (checkedId == R.id.ArtPro) Art_Hours = 5f;
                        Final();
                    } else Art_Hours = 0f;
                });
            } else {
                Art.clearCheck();
                Art_Hours = 0f;
            }

            Final();
        });

        Final();

    }

    public void Final() {
        float Hours = UALan_Hours + UALit_Hours + FLit_Hours + FLan_Hours + UAHist_Hours + FHist_Hours + AnFLan_Hours + Math_Hours + Alg_Hours + Geom_Hours + PhysAstr_Hours + BioEco_Hours + Chem_Hours + Geo_Hours + Milit_Hours + SocEd_Hours + Law_Hours + Econ_Hours + IT_Hours + Tech_Hours + Art_Hours + PE_Hours;

        if (Hours >= 33f) {
            HoursTaken.setTextColor(Color.rgb(255, 0, 0));
        } else {
            HoursTaken.setTextColor(Color.rgb(0, 0, 0));
        }

        HoursTaken.setText(String.format(new Locale("UA"), "Годин вибрано: %.1f", Hours));
    }

}