package com.haussteuerung;

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

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

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.NumberPicker;
import android.widget.TimePicker;

import java.time.LocalTime;
import java.util.Locale;


public class FragmentHeizung extends Fragment {

    private Button btn_hzZeit1, btn_hzZeit2, btn_hzZeit3, btn_hzZeit4, btn_hzZeit5, btn_hzZeit6, btn_hzVL1, btn_hzVL2, btn_hzVL3;
    private CheckBox check_hzZeit1, check_hzZeit2, check_hzZeit3;
    SwitchCompat switch_silentModeHz1, switch_silentModeHz2, switch_silentModeHz3;
    public static String hzZeit1Mqtt = "00:00", hzZeit2Mqtt = "00:00", hzZeit3Mqtt = "00:00", hzZeit4Mqtt = "00:00", hzZeit5Mqtt = "00:00", hzZeit6Mqtt = "00:00";
    public static boolean isCheckedHZ1 = false, isCheckedHZ2 = false, isCheckedHZ3 = false;
    public static boolean switchHZ1 = false, switchHZ2 = false, switchHZ3 = false;
    public static int tempHz1 = 48, tempHz2 = 45, tempHz3 = 50;
    View view;

    public FragmentHeizung() {
        // 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_heizung, container, false);
        requireActivity().setTitle("Haussteuerung - Heizung");

        return view;
    }

    public void sendValues() {
        Button btn_HzSend = view.findViewById(R.id.btn_HzSend);
        btn_HzSend.setOnClickListener(v -> {
            String msgHzZeit1 = "{" +
                    "\"DeviceID\":\"" + MainActivity.ID + "\"," +
                    "\"CheckHZ1\":" + "\"" + check_hzZeit1.isChecked() + "\"," +
                    "\"SilentModeHZ1\":" + "\"" + switch_silentModeHz1.isChecked() + "\"," +
                    "\"ZeitHZ1\": " + "\"" + btn_hzZeit1.getText().toString() + "\"," +
                    "\"ZeitHZ2\": " + "\"" + btn_hzZeit2.getText().toString() + "\"," +
                    "\"TempHZ1\":" + btn_hzVL1.getText().toString() + "," +
                    "\"CheckHZ2\":" + "\"" + check_hzZeit2.isChecked() + "\"," +
                    "\"SilentModeHZ2\":" + "\"" + switch_silentModeHz2.isChecked() + "\"," +
                    "\"ZeitHZ3\": " + "\"" + btn_hzZeit3.getText().toString() + "\"," +
                    "\"ZeitHZ4\": " + "\"" + btn_hzZeit4.getText().toString() + "\"," +
                    "\"TempHZ2\":" + btn_hzVL2.getText().toString() + "," +
                    "\"CheckHZ3\":" + "\"" + check_hzZeit3.isChecked() + "\"," +
                    "\"SilentModeHZ3\":" + "\"" + switch_silentModeHz3.isChecked() + "\"," +
                    "\"ZeitHZ5\": " + "\"" + btn_hzZeit5.getText().toString() + "\"," +
                    "\"ZeitHZ6\": " + "\"" + btn_hzZeit6.getText().toString() + "\"," +
                    "\"TempHZ3\": " + btn_hzVL3.getText() +
                    "}";

            MqttClient.publish("stat/Haussteuerung/LWP/Heizung", msgHzZeit1, 2, true);
            MqttClient.publish("cmnd/Haussteuerung/LWP/Heizung", "{\"DeviceID\":\"" + MainActivity.ID + "\"}", 2, false);
            requireActivity().onBackPressed();
        });
    }

    public void handleButtons() {
        btn_hzZeit1 = view.findViewById(R.id.btn_hzZeit1);
        btn_hzZeit1.setOnClickListener(v -> timePickerDialog(btn_hzZeit1));
        btn_hzZeit1.setText(hzZeit1Mqtt.substring(0,5));

        btn_hzZeit2 = view.findViewById(R.id.btn_hzZeit2);
        btn_hzZeit2.setOnClickListener(v -> timePickerDialog(btn_hzZeit2));
        btn_hzZeit2.setText(hzZeit2Mqtt.substring(0,5));

        btn_hzZeit3 = view.findViewById(R.id.btn_hzZeit3);
        btn_hzZeit3.setOnClickListener(v -> timePickerDialog(btn_hzZeit3));
        btn_hzZeit3.setText(hzZeit3Mqtt.substring(0,5));

        btn_hzZeit4 = view.findViewById(R.id.btn_hzZeit4);
        btn_hzZeit4.setOnClickListener(v -> timePickerDialog(btn_hzZeit4));
        btn_hzZeit4.setText(hzZeit4Mqtt.substring(0,5));

        btn_hzZeit5 = view.findViewById(R.id.btn_hzZeit5);
        btn_hzZeit5.setOnClickListener(v -> timePickerDialog(btn_hzZeit5));
        btn_hzZeit5.setText(hzZeit5Mqtt.substring(0,5));

        btn_hzZeit6 = view.findViewById(R.id.btn_hzZeit6);
        btn_hzZeit6.setOnClickListener(v -> timePickerDialog(btn_hzZeit6));
        btn_hzZeit6.setText(hzZeit6Mqtt.substring(0,5));


        btn_hzVL1 = view.findViewById(R.id.btn_hzVL1);
        btn_hzVL1.setText(String.valueOf(tempHz1));
        btn_hzVL1.setOnClickListener(v -> showNumberPickerDialog(btn_hzVL1));

        btn_hzVL2 = view.findViewById(R.id.btn_hzVL2);
        btn_hzVL2.setText(String.valueOf(tempHz2));
        btn_hzVL2.setOnClickListener(v -> showNumberPickerDialog(btn_hzVL2));

        btn_hzVL3 = view.findViewById(R.id.btn_hzVL3);
        btn_hzVL3.setText(String.valueOf(tempHz3));
        btn_hzVL3.setOnClickListener(v -> showNumberPickerDialog(btn_hzVL3));
    }

    public void handleCheckBoxes() {
        check_hzZeit1 = view.findViewById(R.id.check_hzZeit1);
        check_hzZeit2 = view.findViewById(R.id.check_hzZeit2);
        check_hzZeit3 = view.findViewById(R.id.check_hzZeit3);
        check_hzZeit1.setChecked(isCheckedHZ1);
        check_hzZeit2.setChecked(isCheckedHZ2);
        check_hzZeit3.setChecked(isCheckedHZ3);

        // Set visibilty of buttons
        if (check_hzZeit1.isChecked()) {
            switch_silentModeHz1.setEnabled(true);
            btn_hzZeit1.setEnabled(true);
            btn_hzZeit2.setEnabled(true);
            btn_hzVL1.setEnabled(true);
        } else {
            switch_silentModeHz1.setEnabled(false);
            btn_hzZeit1.setEnabled(false);
            btn_hzZeit2.setEnabled(false);
            btn_hzVL1.setEnabled(false);
        }
        check_hzZeit1.setOnClickListener(v -> {
            if (check_hzZeit1.isChecked()) {
                switch_silentModeHz1.setEnabled(true);
                btn_hzZeit1.setEnabled(true);
                btn_hzZeit2.setEnabled(true);
                btn_hzVL1.setEnabled(true);
            } else {
                switch_silentModeHz1.setEnabled(false);
                btn_hzZeit1.setEnabled(false);
                btn_hzZeit2.setEnabled(false);
                btn_hzVL1.setEnabled(false);
            }
        });

        if (check_hzZeit2.isChecked()) {
            switch_silentModeHz2.setEnabled(true);
            btn_hzZeit3.setEnabled(true);
            btn_hzZeit4.setEnabled(true);
            btn_hzVL2.setEnabled(true);
        } else {
            switch_silentModeHz2.setEnabled(false);
            btn_hzZeit3.setEnabled(false);
            btn_hzZeit4.setEnabled(false);
            btn_hzVL2.setEnabled(false);
        }
        check_hzZeit2.setOnClickListener(v -> {
            if (check_hzZeit2.isChecked()) {
                switch_silentModeHz2.setEnabled(true);
                btn_hzZeit3.setEnabled(true);
                btn_hzZeit4.setEnabled(true);
                btn_hzVL2.setEnabled(true);
            } else {
                switch_silentModeHz2.setEnabled(false);
                btn_hzZeit3.setEnabled(false);
                btn_hzZeit4.setEnabled(false);
                btn_hzVL2.setEnabled(false);
            }
        });

        if (check_hzZeit3.isChecked()) {
            switch_silentModeHz3.setEnabled(true);
            btn_hzZeit5.setEnabled(true);
            btn_hzZeit6.setEnabled(true);
            btn_hzVL3.setEnabled(true);
        } else {
            switch_silentModeHz3.setEnabled(false);
            btn_hzZeit5.setEnabled(false);
            btn_hzZeit6.setEnabled(false);
            btn_hzVL3.setEnabled(false);
        }
        check_hzZeit3.setOnClickListener(v -> {
            if (check_hzZeit3.isChecked()) {
                switch_silentModeHz3.setEnabled(true);
                btn_hzZeit5.setEnabled(true);
                btn_hzZeit6.setEnabled(true);
                btn_hzVL3.setEnabled(true);
            } else {
                switch_silentModeHz3.setEnabled(false);
                btn_hzZeit5.setEnabled(false);
                btn_hzZeit6.setEnabled(false);
                btn_hzVL3.setEnabled(false);
            }
        });
    }

    public void handleSwitch() {
        switch_silentModeHz1 = view.findViewById(R.id.switch_silentModeHz1);
        switch_silentModeHz2 = view.findViewById(R.id.switch_silentModeHz2);
        switch_silentModeHz3 = view.findViewById(R.id.switch_silentModeHz3);

        switch_silentModeHz1.setChecked(switchHZ1);
        switch_silentModeHz2.setChecked(switchHZ2);
        switch_silentModeHz3.setChecked(switchHZ3);
    }

    private void showNumberPickerDialog(Button button) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        final View view = this.getLayoutInflater().inflate(R.layout.layout_numberpicker, null);
        builder.setView(view);
        //builder.setTitle();
        final NumberPicker picker = view.findViewById(R.id.picker);
        String[] values = {"30","31","32","33","34","35","36","37","38","39","40","41","42","43",
                "44","45","46","47","48","49","50"};
        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()]);
        })
                .setNegativeButton(android.R.string.cancel, (dialog, id) -> builder.create().cancel());
        builder.create().show();
    }

    public void timePickerDialog(Button button) {
        LocalTime localTime = LocalTime.parse(button.getText());
        int hour = localTime.getHour();
        int minute = localTime.getMinute();
        TimePickerDialog.OnTimeSetListener onTimeSetListener = (timePicker, selectedHour, selectedMinute) -> button.setText(String.format(Locale.getDefault(), "%02d:%02d", selectedHour, selectedMinute));
        TimePickerDialog timePickerDialog = new TimePickerDialog(getContext(), onTimeSetListener, hour, minute, true);
        timePickerDialog.show();
    }

    @Override
    public void onResume() {
        super.onResume();
        handleSwitch();
        handleButtons();
        handleCheckBoxes();
        sendValues();
    }

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