package com.haussteuerung;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ProcessLifecycleOwner;

import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class FragmentHeizkoerper extends Fragment {

    private Button btn_HK_Grossraum, btn_HK_Doris, btn_HK_Doris_Bad, btn_HK_Arbeitszimmer, btn_HK_Vorraum,
            btn_HK_WCEG, btn_HK_Bad, btn_HK_Florian, btn_HK_Stefan, btn_HK_Dachboden;
    private TextView tv_temp_Grossraum, tv_temp_Doris, tv_temp_Doris_Bad, tv_temp_Arbeitszimmer, tv_temp_Vorraum,
            tv_temp_WCEG, tv_temp_Bad, tv_temp_Florian, tv_temp_Stefan, tv_temp_Dachboden;
    private SwitchCompat switch_HK_Grossraum, switch_HK_Doris, switch_HK_Doris_Bad, switch_HK_Arbeitszimmer, switch_HK_Vorraum,
            switch_HK_WCEG, switch_HK_Bad, switch_HK_Florian, switch_HK_Stefan, switch_HK_Dachboden;
    public static String modeHKGrossraum = "", modeHKDoris = "", modeHKDorisBad = "", modeHKArbeitszimmer = "",
            modeHKVorraum = "", modeHKWCEG = "", modeHKBad = "", modeHKFlorian = "", modeHKStefan = "", modeHKDachboden = "";
    public static String istTempHKGrossraum = "", istTempHKDoris = "", istTempHKDorisBad = "", istTempHKArbeitszimmer = "",
            istTempHKVorraum = "", istTempHKWCEG = "", istTempHKBad = "", istTempHKFlorian = "", istTempHKStefan = "", istTempHKDachboden = "";
    public static String sollTempHKGrossraum = "", sollTempHKDoris = "", sollTempHKDorisBad = "", sollTempHKArbeitszimmer = "",
            sollTempHKVorraum = "", sollTempHKWCEG = "", sollTempHKBad = "", sollTempHKFlorian = "", sollTempHKStefan = "", sollTempHKDachboden = "";

    final Handler myHandler = new Handler();
    final Timer myTimer = new Timer();
    TimerTask myTask;
    Context context;
    View view;

    public FragmentHeizkoerper() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifecycleTracker());

        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_heizkoerper, container, false);
        requireActivity().setTitle("Haussteuerung - Heizkörper");

        handleButtons();
        handleTextViews();
        handleSwitches();

        context = getContext();

        return view;
    }

    // Runnable method
    final Runnable myRunnable = new Runnable() {
        public void run() {
            Log.d("Heizkoerper", "OK");

            tv_temp_Grossraum.setText(istTempHKGrossraum);
            setTextColor(tv_temp_Grossraum, istTempHKGrossraum);
            tv_temp_Doris.setText(istTempHKDoris);
            setTextColor(tv_temp_Doris, istTempHKDoris);
            tv_temp_Doris_Bad.setText(istTempHKDorisBad);
            setTextColor(tv_temp_Doris_Bad, istTempHKDorisBad);
            tv_temp_Arbeitszimmer.setText(istTempHKArbeitszimmer);
            setTextColor(tv_temp_Arbeitszimmer, istTempHKArbeitszimmer);
            tv_temp_Vorraum.setText(istTempHKVorraum);
            setTextColor(tv_temp_Vorraum, istTempHKVorraum);
            tv_temp_WCEG.setText(istTempHKWCEG);
            setTextColor(tv_temp_WCEG, istTempHKWCEG);
            tv_temp_Bad.setText(istTempHKBad);
            setTextColor(tv_temp_Bad, istTempHKBad);
            tv_temp_Florian.setText(istTempHKFlorian);
            setTextColor(tv_temp_Florian, istTempHKFlorian);
            tv_temp_Stefan.setText(istTempHKStefan);
            setTextColor(tv_temp_Stefan, istTempHKStefan);
            tv_temp_Dachboden.setText(istTempHKDachboden);
            setTextColor(tv_temp_Dachboden, istTempHKDachboden);
        }
    };

    private void showNumberPickerDialog(Button button, String roomSollTemp, String room, SwitchCompat switchCompat) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        final View view = this.getLayoutInflater().inflate(R.layout.layout_numberpicker, null);
        builder.setView(view);
        final NumberPicker picker = view.findViewById(R.id.picker);
        String[] values = {"off", "12.0", "12.5", "13.0", "13.5", "14.0", "14.5", "15.0", "15.5", "16.0", "16.5", "17.0", "17.5", "18.0",
                "18.5", "19.0", "19.5", "20.0", "20.5", "21.0", "21.5", "22.0", "22.5", "23.0", "23.5", "24.0", "24.5", "25.0",
                "25.5", "26.0", "26.5", "27.0", "27.5", "28.0", "28.5", "29.0", "29.5", "30.0", "on"};
        int ml = values.length;
        picker.setWrapSelectorWheel(false);
        picker.setMinValue(0);
        picker.setMaxValue(ml - 1);
        for (int y = 0; y < values.length; y++) {
            if (values[y].equals(button.getText().toString())) {
                picker.setValue(y);
            }
        }
        picker.setDisplayedValues(values);
        builder.setPositiveButton(android.R.string.ok, (dialog, id) -> {
                    button.setText(values[picker.getValue()]);

                    Log.d("SollTempGrossraum", "Solltemp" + roomSollTemp + ", Button " + button.getText());
                    if (!roomSollTemp.contentEquals(button.getText())) {
                        if (switchCompat.isChecked()) {
                            Log.d("SollTemp" + room, "Temp zu Auto+Temp geändert");
                            MqttClient.publish("fhem/Heizung/MAX_" + room + "/set/temp", "auto " + button.getText().toString(), 2, false);
                        } else {
                            Log.d("SollTemp" + room, "Temp geändert");
                            MqttClient.publish("fhem/Heizung/MAX_" + room + "/set/temp", button.getText().toString(), 2, false);
                        }
                    } else {
                        Log.d("SollTemp" + room, "Temp gleich");
                    }
                })
                .setNegativeButton(android.R.string.cancel, (dialog, id) -> builder.create().cancel());
        builder.create().show();
    }

    private void handleButtons() {
        btn_HK_Grossraum = view.findViewById(R.id.btn_HK_Grossraum);
        btn_HK_Doris = view.findViewById(R.id.btn_HK_Doris);
        btn_HK_Doris_Bad = view.findViewById(R.id.btn_HK_Doris_Bad);
        btn_HK_Arbeitszimmer = view.findViewById(R.id.btn_HK_Arbeitszimmer);
        btn_HK_Vorraum = view.findViewById(R.id.btn_HK_Vorraum);
        btn_HK_WCEG = view.findViewById(R.id.btn_HK_WCEG);
        btn_HK_Bad = view.findViewById(R.id.btn_HK_Bad);
        btn_HK_Florian = view.findViewById(R.id.btn_HK_Florian);
        btn_HK_Stefan = view.findViewById(R.id.btn_HK_Stefan);
//        btn_HK_Dachboden = view.findViewById(R.id.btn_HK_Dachboden);

        Log.d("SollTempGrossraum", sollTempHKGrossraum);
        Log.d("SollTempDoris", sollTempHKDoris);
        Log.d("SollTempDorisBad", sollTempHKDorisBad);
        Log.d("SollTempArbeitszimmer", sollTempHKArbeitszimmer);
        Log.d("SollTempVorraum", sollTempHKVorraum);
        Log.d("SollTempWCEG", sollTempHKWCEG);
        Log.d("SollTempBad", sollTempHKBad);
        Log.d("SollTempFlorian", sollTempHKFlorian);
        Log.d("SollTempStefan", sollTempHKStefan);
        Log.d("SollTempDachboden", sollTempHKDachboden);


        btn_HK_Grossraum.setText(sollTempHKGrossraum);
        btn_HK_Doris.setText(sollTempHKDoris);
        btn_HK_Doris_Bad.setText(sollTempHKDorisBad);
        btn_HK_Arbeitszimmer.setText(sollTempHKArbeitszimmer);
        btn_HK_Vorraum.setText(sollTempHKVorraum);
        btn_HK_WCEG.setText(sollTempHKWCEG);
        btn_HK_Bad.setText(sollTempHKBad);
        btn_HK_Florian.setText(sollTempHKFlorian);
        btn_HK_Stefan.setText(sollTempHKStefan);
//        btn_HK_Dachboden.setText(sollTempHKDachboden);


        btn_HK_Grossraum.setOnClickListener(v -> showNumberPickerDialog(btn_HK_Grossraum, sollTempHKGrossraum, "Grossraum", switch_HK_Grossraum));

        btn_HK_Doris.setOnClickListener(v -> showNumberPickerDialog(btn_HK_Doris, sollTempHKDoris, "Doris_klein", switch_HK_Doris));

        btn_HK_Doris_Bad.setOnClickListener(v -> showNumberPickerDialog(btn_HK_Doris_Bad, sollTempHKDorisBad, "Doris_Bad", switch_HK_Doris_Bad));

        btn_HK_Arbeitszimmer.setOnClickListener(v -> showNumberPickerDialog(btn_HK_Arbeitszimmer, sollTempHKArbeitszimmer, "Arbeitszimmer", switch_HK_Arbeitszimmer));

        btn_HK_Vorraum.setOnClickListener(v -> showNumberPickerDialog(btn_HK_Vorraum, sollTempHKVorraum, "Vorraum", switch_HK_Vorraum));

        btn_HK_WCEG.setOnClickListener(v -> showNumberPickerDialog(btn_HK_WCEG, sollTempHKWCEG, "WC", switch_HK_WCEG));

        btn_HK_Bad.setOnClickListener(v -> showNumberPickerDialog(btn_HK_Bad, sollTempHKBad, "Bad", switch_HK_Bad));

        btn_HK_Florian.setOnClickListener(v -> showNumberPickerDialog(btn_HK_Florian, sollTempHKFlorian, "Florian", switch_HK_Florian));

        btn_HK_Stefan.setOnClickListener(v -> showNumberPickerDialog(btn_HK_Stefan, sollTempHKStefan, "Stefan", switch_HK_Stefan));

//        btn_HK_Dachboden.setOnClickListener(v -> showNumberPickerDialog(btn_HK_Dachboden, sollTempHKDachboden, "Dachboden", switch_HK_Dachboden));
    }

    private void handleTextViews() {
        tv_temp_Grossraum = view.findViewById(R.id.tv_temp_Grossraum);
        tv_temp_Doris = view.findViewById(R.id.tv_temp_Doris);
        tv_temp_Doris_Bad = view.findViewById(R.id.tv_temp_Doris_Bad);
        tv_temp_Arbeitszimmer = view.findViewById(R.id.tv_temp_Arbeitszimmer);
        tv_temp_Vorraum = view.findViewById(R.id.tv_temp_Vorraum);
        tv_temp_WCEG = view.findViewById(R.id.tv_temp_WCEG);
        tv_temp_Bad = view.findViewById(R.id.tv_temp_Bad);
        tv_temp_Florian = view.findViewById(R.id.tv_temp_Florian);
        tv_temp_Stefan = view.findViewById(R.id.tv_temp_Stefan);
        tv_temp_Dachboden = view.findViewById(R.id.tv_temp_Dachboden);
    }

    private void handleSwitches() {
        switch_HK_Grossraum = view.findViewById(R.id.switch_HK_Grossraum);
        switch_HK_Doris = view.findViewById(R.id.switch_HK_Doris);
        switch_HK_Doris_Bad = view.findViewById(R.id.switch_HK_Doris_Bad);
        switch_HK_Arbeitszimmer = view.findViewById(R.id.switch_HK_Arbeitszimmer);
        switch_HK_Vorraum = view.findViewById(R.id.switch_HK_Vorraum);
        switch_HK_WCEG = view.findViewById(R.id.switch_HK_WCEG);
        switch_HK_Bad = view.findViewById(R.id.switch_HK_Bad);
        switch_HK_Florian = view.findViewById(R.id.switch_HK_Florian);
        switch_HK_Stefan = view.findViewById(R.id.switch_HK_Stefan);
//        switch_HK_Dachboden = view.findViewById(R.id.switch_HK_Dachboden);

        if (modeHKGrossraum.equals("manual")) {
            switch_HK_Grossraum.setChecked(false);
            switch_HK_Grossraum.setText(R.string.man);
        } else {
            switch_HK_Grossraum.setChecked(true);
            switch_HK_Grossraum.setText(R.string.auto);
        }

        if (modeHKDoris.equals("manual")) {
            switch_HK_Doris.setChecked(false);
            switch_HK_Doris.setText(R.string.man);
        } else {
            switch_HK_Doris.setChecked(true);
            switch_HK_Doris.setText(R.string.auto);
        }

        if (modeHKDorisBad.equals("manual")) {
            switch_HK_Doris_Bad.setChecked(false);
            switch_HK_Doris_Bad.setText(R.string.man);
        } else {
            switch_HK_Doris_Bad.setChecked(true);
            switch_HK_Doris_Bad.setText(R.string.auto);
        }

        if (modeHKArbeitszimmer.equals("manual")) {
            switch_HK_Arbeitszimmer.setChecked(false);
            switch_HK_Arbeitszimmer.setText(R.string.man);
        } else {
            switch_HK_Arbeitszimmer.setChecked(true);
            switch_HK_Arbeitszimmer.setText(R.string.auto);
        }

        if (modeHKVorraum.equals("manual")) {
            switch_HK_Vorraum.setChecked(false);
            switch_HK_Vorraum.setText(R.string.man);
        } else {
            switch_HK_Vorraum.setChecked(true);
            switch_HK_Vorraum.setText(R.string.auto);
        }

        if (modeHKWCEG.equals("manual")) {
            switch_HK_WCEG.setChecked(false);
            switch_HK_WCEG.setText(R.string.man);
        } else {
            switch_HK_WCEG.setChecked(true);
            switch_HK_WCEG.setText(R.string.auto);
        }

        if (modeHKBad.equals("manual")) {
            switch_HK_Bad.setChecked(false);
            switch_HK_Bad.setText(R.string.man);
        } else {
            switch_HK_Bad.setChecked(true);
            switch_HK_Bad.setText(R.string.auto);
        }

        if (modeHKFlorian.equals("manual")) {
            switch_HK_Florian.setChecked(false);
            switch_HK_Florian.setText(R.string.man);
        } else {
            switch_HK_Florian.setChecked(true);
            switch_HK_Florian.setText(R.string.auto);
        }

        if (modeHKStefan.equals("manual")) {
            switch_HK_Stefan.setChecked(false);
            switch_HK_Stefan.setText(R.string.man);
        } else {
            switch_HK_Stefan.setChecked(true);
            switch_HK_Stefan.setText(R.string.auto);
        }

//        if (modeHKDachboden.equals("manual")) {
//            switch_HK_Dachboden.setChecked(false);
//            switch_HK_Dachboden.setText(R.string.man);
//        } else {
//            switch_HK_Dachboden.setChecked(true);
//            switch_HK_Dachboden.setText(R.string.auto);
//        }

        switch_HK_Grossraum.setOnClickListener(v -> {
            if (switch_HK_Grossraum.isChecked()) {
                Log.d("Mode" + modeHKGrossraum, "Mode geändert");
                switch_HK_Grossraum.setText(R.string.auto);
                MqttClient.publish("fhem/Heizung/MAX_Grossraum/set/mode", "auto", 2, false);
            } else if (!switch_HK_Grossraum.isChecked()) {
                Log.d("Mode" + modeHKGrossraum, "Mode geändert");
                switch_HK_Grossraum.setText(R.string.man);
                MqttClient.publish("fhem/Heizung/MAX_Grossraum/set/mode", "manual", 2, false);
            }
        });

        switch_HK_Doris.setOnClickListener(v -> {
            if (switch_HK_Doris.isChecked()) {
                Log.d("Mode" + modeHKDoris, "Mode geändert");
                switch_HK_Doris.setText(R.string.auto);
                MqttClient.publish("fhem/Heizung/MAX_Doris_klein/set/mode", "auto", 2, false);
            } else if (!switch_HK_Doris.isChecked()) {
                Log.d("Mode" + modeHKDoris, "Mode geändert");
                switch_HK_Doris.setText(R.string.man);
                MqttClient.publish("fhem/Heizung/MAX_Doris_klein/set/mode", "manual", 2, false);
            }
        });

        switch_HK_Doris_Bad.setOnClickListener(v -> {
            if (switch_HK_Doris_Bad.isChecked()) {
                Log.d("Mode" + modeHKDorisBad, "Mode auto");
                switch_HK_Doris_Bad.setText(R.string.auto);
                MqttClient.publish("fhem/Heizung/MAX_Doris_Bad/set/mode", "auto", 2, false);
            } else if (!switch_HK_Doris_Bad.isChecked()) {
                Log.d("Mode" + modeHKDorisBad, "Mode manual");
                switch_HK_Doris_Bad.setText(R.string.man);
                MqttClient.publish("fhem/Heizung/MAX_Doris_Bad/set/mode", "manual", 2, false);
            }
        });

        switch_HK_Arbeitszimmer.setOnClickListener(v -> {
            if (switch_HK_Arbeitszimmer.isChecked()) {
                Log.d("Mode" + modeHKArbeitszimmer, "Mode auto");
                switch_HK_Arbeitszimmer.setText(R.string.auto);
                MqttClient.publish("fhem/Heizung/MAX_Arbeitszimmer/set/mode", "auto", 2, false);
            } else if (!switch_HK_Arbeitszimmer.isChecked()) {
                Log.d("Mode" + modeHKArbeitszimmer, "Mode manual");
                switch_HK_Arbeitszimmer.setText(R.string.man);
                MqttClient.publish("fhem/Heizung/MAX_Arbeitszimmer/set/mode", "manual", 2, false);
            }
        });

        switch_HK_Vorraum.setOnClickListener(v -> {
            if (switch_HK_Vorraum.isChecked()) {
                Log.d("Mode" + modeHKVorraum, "Mode auto");
                switch_HK_Vorraum.setText(R.string.auto);
                MqttClient.publish("fhem/Heizung/MAX_Vorraum/set/mode", "auto", 2, false);
            } else if (!switch_HK_Vorraum.isChecked()) {
                Log.d("Mode" + modeHKVorraum, "Mode manual");
                switch_HK_Vorraum.setText(R.string.man);
                MqttClient.publish("fhem/Heizung/MAX_Vorraum/set/mode", "manual", 2, false);
            }
        });

        switch_HK_WCEG.setOnClickListener(v -> {
            if (switch_HK_WCEG.isChecked()) {
                Log.d("Mode" + modeHKWCEG, "Mode auto");
                switch_HK_WCEG.setText(R.string.auto);
                MqttClient.publish("fhem/Heizung/MAX_WC/set/mode", "auto", 2, false);
            } else if (!switch_HK_WCEG.isChecked()) {
                Log.d("Mode" + modeHKWCEG, "Mode manual");
                switch_HK_WCEG.setText(R.string.man);
                MqttClient.publish("fhem/Heizung/MAX_WC/set/mode", "manual", 2, false);
            }
        });

        switch_HK_Bad.setOnClickListener(v -> {
            if (switch_HK_Bad.isChecked()) {
                Log.d("Mode" + modeHKBad, "Mode auto");
                switch_HK_Bad.setText(R.string.auto);
                MqttClient.publish("fhem/Heizung/MAX_Bad/set/mode", "auto", 2, false);
            } else if (!switch_HK_Bad.isChecked()) {
                Log.d("Mode" + modeHKBad, "Mode manual");
                switch_HK_Bad.setText(R.string.man);
                MqttClient.publish("fhem/Heizung/MAX_Bad/set/mode", "manual", 2, false);
            }
        });

        switch_HK_Florian.setOnClickListener(v -> {
            if (switch_HK_Florian.isChecked()) {
                Log.d("Mode" + modeHKFlorian, "Mode geändert");
                switch_HK_Florian.setText(R.string.auto);
                MqttClient.publish("fhem/Heizung/MAX_Florian/set/mode", "auto", 2, false);
            } else if (!switch_HK_Florian.isChecked()) {
                Log.d("Mode" + modeHKFlorian, "Mode geändert");
                switch_HK_Florian.setText(R.string.man);
                MqttClient.publish("fhem/Heizung/MAX_Florian/set/mode", "manual", 2, false);
            }
        });

        switch_HK_Stefan.setOnClickListener(v -> {
            if (switch_HK_Stefan.isChecked()) {
                Log.d("Mode" + modeHKStefan, "Mode auto");
                switch_HK_Stefan.setText(R.string.auto);
                MqttClient.publish("fhem/Heizung/MAX_Stefan/set/mode", "auto", 2, false);
            } else if (!switch_HK_Stefan.isChecked()) {
                Log.d("Mode" + modeHKStefan, "Mode manual");
                switch_HK_Stefan.setText(R.string.man);
                MqttClient.publish("fhem/Heizung/MAX_Stefan/set/mode", "manual", 2, false);
            }
        });

//        switch_HK_Dachboden.setOnClickListener(v -> {
//            if (switch_HK_Dachboden.isChecked()) {
//                Log.d("Mode" + modeHKDachboden, "Mode auto");
//                switch_HK_Dachboden.setText(R.string.auto);
//                MqttClient.publish("fhem/Heizung/MAX_Dachboden/set/mode", "auto", 2, false);
//            } else if (!switch_HK_Dachboden.isChecked()) {
//                Log.d("Mode" + modeHKDachboden, "Mode manual");
//                switch_HK_Dachboden.setText(R.string.man);
//                MqttClient.publish("fhem/Heizung/MAX_Dachboden/set/mode", "manual", 2, false);
//            }
//        });
    }

    private void setTextColor(TextView tv, String temp) {
        if (!temp.isEmpty()) {
            double tempDouble = Double.parseDouble(temp);
            if (tempDouble >= 25) {
                tv.setTextColor(ContextCompat.getColor(context, R.color.twoFive));
            }
            if (tempDouble >= 24 && tempDouble < 25) {
                tv.setTextColor(ContextCompat.getColor(context, R.color.twoFour));
            }
            if (tempDouble >= 23 && tempDouble < 24) {
                tv.setTextColor(ContextCompat.getColor(context, R.color.twoThree));
            }
            if (tempDouble >= 22 && tempDouble < 23) {
                tv.setTextColor(ContextCompat.getColor(context, R.color.twoTwo));
            }
            if (tempDouble >= 21 && tempDouble < 22) {
                tv.setTextColor(ContextCompat.getColor(context, R.color.twoOne));
            }
            if (tempDouble >= 20 && tempDouble < 21) {
                tv.setTextColor(ContextCompat.getColor(context, R.color.twoZero));
            }
            if (tempDouble >= 19 && tempDouble < 20) {
                tv.setTextColor(ContextCompat.getColor(context, R.color.oneNine));
            }
            if (tempDouble < 19) {
                tv.setTextColor(ContextCompat.getColor(context, R.color.oneEight));
            }
        } else {
            Log.d("Temp", "String Temp in FragmentHeizkörper is empty.");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        myTask = new TimerTask() {
            public void run() {
                myHandler.post(myRunnable); // updateUI method
            }
        };
        myTimer.schedule(myTask, 0, 1000);
    }

    @Override
    public void onStop() {
        super.onStop();
        myTask.cancel();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}