package com.haussteuerung;

import android.app.TimePickerDialog;
import android.os.Bundle;

import androidx.appcompat.widget.SwitchCompat;
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.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.TimePicker;

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

public class FragmentWW extends Fragment {

    Button btn_wwZeit1, btn_wwZeit2, btn_wwZeit3, btn_wwZeit4, btn_wwZeit5, btn_wwZeit6;
    CheckBox check_wwZeit1, check_wwZeit2, check_wwZeit3;
    SwitchCompat switch_silentModeWw1, switch_silentModeWw2, switch_silentModeWw3;
    public static String wwZeit1Mqtt = "00:00", wwZeit2Mqtt = "00:00", wwZeit3Mqtt = "00:00", wwZeit4Mqtt = "00:00", wwZeit5Mqtt = "00:00", wwZeit6Mqtt = "00:00";
    public static boolean isCheckedWW1 = false, isCheckedWW2 = false, isCheckedWW3 = false;
    public static boolean switchWW1 = false, switchWW2 = false, switchWW3 = false;
    public static int tempWw1 = 39, tempWw2 = 44;
    public static String tempBoilerOben = "40", tempBoilerUnten = "35";
    public SeekBar seekBar_WwMin, seekBar_WwMax;
    public TextView tv_WwMinVal, tv_WwMaxVal, tv_boilerObenVal, tv_boilerUntenVal;
    View view;
    final Handler myHandler = new Handler();
    final Timer myTimer = new Timer();
    TimerTask myTask;

    public FragmentWW() {
        // 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_ww, container, false);
        requireActivity().setTitle("Haussteuerung - Warmwasser");

        tv_boilerObenVal = view.findViewById(R.id.tv_boilerObenVal);
        tv_boilerUntenVal = view.findViewById(R.id.tv_boilerUntenVal);

        return view;
    }

    // Runnable method
    final Runnable myRunnable = new Runnable() {
        public void run() {
            Log.d("FragmentWW", "Timer 1s");
            String oben = tempBoilerOben + "°";
            tv_boilerObenVal.setText(oben);
            String unten = tempBoilerUnten + "°";
            tv_boilerUntenVal.setText(unten);
        }
    };

    public void sendValues() {
        Button btn_WwSend = view.findViewById(R.id.btn_HzSend);
        btn_WwSend.setOnClickListener(v -> {
            String msgWwZeit1 = "{" +
                    "\"DeviceID\":\"" + MainActivity.ID + "\"," +
                    "\"CheckWW1\":" + "\"" + check_wwZeit1.isChecked() + "\"," +
                    "\"SilentModeWW1\": " + "\"" + switch_silentModeWw1.isChecked() + "\"," +
                    "\"ZeitWW1\": " + "\"" + btn_wwZeit1.getText().toString() + "\"," +
                    "\"ZeitWW2\": " + "\"" + btn_wwZeit2.getText().toString() + "\"," +
                    "\"CheckWW2\":" + "\"" + check_wwZeit2.isChecked() + "\"," +
                    "\"SilentModeWW2\": " + "\"" + switch_silentModeWw2.isChecked() + "\"," +
                    "\"ZeitWW3\": " + "\"" + btn_wwZeit3.getText().toString() + "\"," +
                    "\"ZeitWW4\": " + "\"" + btn_wwZeit4.getText().toString() + "\"," +
                    "\"CheckWW3\":" + "\"" + check_wwZeit3.isChecked() + "\"," +
                    "\"SilentModeWW3\": " + "\"" + switch_silentModeWw3.isChecked() + "\"," +
                    "\"ZeitWW5\": " + "\"" + btn_wwZeit5.getText().toString() + "\"," +
                    "\"ZeitWW6\": " + "\"" + btn_wwZeit6.getText().toString() + "\"," +
                    "\"TempWW1\": " + tv_WwMinVal.getText() + "," +
                    "\"TempWW2\": " + tv_WwMaxVal.getText() +
                    "}";

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

    public void handleSwitch() {
        switch_silentModeWw1 = view.findViewById(R.id.switch_silentModeWw1);
        switch_silentModeWw2 = view.findViewById(R.id.switch_silentModeWw2);
        switch_silentModeWw3 = view.findViewById(R.id.switch_silentModeWw3);

        switch_silentModeWw1.setChecked(switchWW1);
        switch_silentModeWw2.setChecked(switchWW2);
        switch_silentModeWw3.setChecked(switchWW3);
    }

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

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

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

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

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

        btn_wwZeit6 = view.findViewById(R.id.btn_wwZeit6);
        btn_wwZeit6.setOnClickListener(v -> timePickerDialog(btn_wwZeit6));
        btn_wwZeit6.setText(wwZeit6Mqtt.substring(0, 5));
    }

    public void handleCheckBoxes() {
        check_wwZeit1 = view.findViewById(R.id.check_wwZeit1);
        check_wwZeit2 = view.findViewById(R.id.check_wwZeit2);
        check_wwZeit3 = view.findViewById(R.id.check_wwZeit3);
        check_wwZeit1.setChecked(isCheckedWW1);
        check_wwZeit2.setChecked(isCheckedWW2);
        check_wwZeit3.setChecked(isCheckedWW3);

        // Set visibilty of buttons
        if (check_wwZeit1.isChecked()) {
            switch_silentModeWw1.setEnabled(true);
            btn_wwZeit1.setEnabled(true);
            btn_wwZeit2.setEnabled(true);
        } else {
            switch_silentModeWw1.setEnabled(false);
            btn_wwZeit1.setEnabled(false);
            btn_wwZeit2.setEnabled(false);
        }
        check_wwZeit1.setOnClickListener(v -> {
            if (check_wwZeit1.isChecked()) {
                switch_silentModeWw1.setEnabled(true);
                btn_wwZeit1.setEnabled(true);
                btn_wwZeit2.setEnabled(true);
            } else {
                switch_silentModeWw1.setEnabled(false);
                btn_wwZeit1.setEnabled(false);
                btn_wwZeit2.setEnabled(false);
            }
        });

        if (check_wwZeit2.isChecked()) {
            switch_silentModeWw2.setEnabled(true);
            btn_wwZeit3.setEnabled(true);
            btn_wwZeit4.setEnabled(true);
        } else {
            switch_silentModeWw2.setEnabled(false);
            btn_wwZeit3.setEnabled(false);
            btn_wwZeit4.setEnabled(false);
        }
        check_wwZeit2.setOnClickListener(v -> {
            if (check_wwZeit2.isChecked()) {
                switch_silentModeWw2.setEnabled(true);
                btn_wwZeit3.setEnabled(true);
                btn_wwZeit4.setEnabled(true);
            } else {
                switch_silentModeWw2.setEnabled(false);
                btn_wwZeit3.setEnabled(false);
                btn_wwZeit4.setEnabled(false);
            }
        });

        if (check_wwZeit3.isChecked()) {
            switch_silentModeWw3.setEnabled(true);
            btn_wwZeit5.setEnabled(true);
            btn_wwZeit6.setEnabled(true);
        } else {
            switch_silentModeWw3.setEnabled(false);
            btn_wwZeit5.setEnabled(false);
            btn_wwZeit6.setEnabled(false);
        }
        check_wwZeit3.setOnClickListener(v -> {
            if (check_wwZeit3.isChecked()) {
                switch_silentModeWw3.setEnabled(true);
                btn_wwZeit5.setEnabled(true);
                btn_wwZeit6.setEnabled(true);
            } else {
                switch_silentModeWw3.setEnabled(false);
                btn_wwZeit5.setEnabled(false);
                btn_wwZeit6.setEnabled(false);
            }
        });
    }

    public void handleTextViews() {
        tv_WwMinVal = view.findViewById(R.id.tv_wwMinVal);
        tv_WwMaxVal = view.findViewById(R.id.tv_wwMaxVal);
        tv_WwMinVal.setText(String.valueOf(tempWw1));
        tv_WwMaxVal.setText(String.valueOf(tempWw2));
    }

    public void handleSeekBars() {
        seekBar_WwMin = view.findViewById(R.id.seekBar_wwMin);
        seekBar_WwMin.setProgress(tempWw1);
        seekBar_WwMin.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_WwMinVal.setText(String.valueOf(progress));
                seekBar_WwMax.setMin(progress + 2);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        seekBar_WwMax = view.findViewById(R.id.seekBar_wwMax);
        seekBar_WwMax.setProgress(tempWw2);
        seekBar_WwMax.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_WwMaxVal.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

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

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

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

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