package com.haussteuerung;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.lifecycle.ProcessLifecycleOwner;

import com.google.gson.Gson;
import com.haussteuerung.helper.Blutdruck;
import com.haussteuerung.helper.Temperatur;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class activityDoris extends AppCompatActivity {
    Button buttonSend, zeitBlutdruck, zeitFiebertemp;
    int hourBlutdruck = 0, minuteBlutdruck = 0, hourFiebertemp = 0, minuteFiebertemp = 0;
    static String werteBlutdruck = "";
    TextView textSYS, textDIA, textFiebertemp;
    LocalDateTime datetime = LocalDateTime.now();
    DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    String formattedDate = datetime.format(dateTimeFormat);
    Gson gson = new Gson();
    String msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifecycleTracker());

        androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setBackgroundDrawable(AppCompatResources.getDrawable(getApplicationContext(), R.color.black));

        setContentView(R.layout.activity_doris);
        setTitle("Haussteuerung - Doris");

        buttonSend = findViewById(R.id.buttonSend);

        // Blutdruck ---------------------------------------
        zeitBlutdruck = findViewById(R.id.zeitBlutdruck);
        CheckBox checkBlutdruck = findViewById(R.id.checkBlutdruck);
        textSYS = findViewById(R.id.textSYS);
        textDIA = findViewById(R.id.textDIA);
        TextView tv_SYS = findViewById(R.id.tvSYS);
        TextView tv_DIA = findViewById(R.id.tvDIA);
        hourBlutdruck = datetime.getHour();
        minuteBlutdruck = datetime.getMinute();
        zeitBlutdruck.setText(String.format(Locale.getDefault(), "%02d:%02d", datetime.getHour(), datetime.getMinute()));
        zeitBlutdruck.setOnClickListener(v -> timePickerDialog(zeitBlutdruck));

        checkBlutdruck.setOnClickListener(view -> {
            if (checkBlutdruck.isChecked()) {
                zeitBlutdruck.setEnabled(true);
                tv_SYS.setEnabled(true);
                tv_DIA.setEnabled(true);
                textSYS.setEnabled(true);
                textDIA.setEnabled(true);
            } else {
                zeitBlutdruck.setEnabled(false);
                tv_SYS.setEnabled(false);
                tv_DIA.setEnabled(false);
                textSYS.setEnabled(false);
                textDIA.setEnabled(false);
            }
        });


        // Fiebertemperatur --------------------------------
        zeitFiebertemp = findViewById(R.id.zeitFiebertemp);
        CheckBox checkFiebertemp = findViewById(R.id.checkFiebertemp);
        TextView tvFiebertemp = findViewById(R.id.tvFiebertemp);
        textFiebertemp = findViewById(R.id.textFiebertemp);
        hourFiebertemp = datetime.getHour();
        minuteFiebertemp = datetime.getMinute();
        zeitFiebertemp.setText(String.format(Locale.getDefault(), "%02d:%02d", datetime.getHour(), datetime.getMinute()));
        zeitFiebertemp.setOnClickListener(v -> timePickerDialog(zeitFiebertemp));

        checkFiebertemp.setOnClickListener(view -> {
            if (checkFiebertemp.isChecked()) {
                zeitFiebertemp.setEnabled(true);
                tvFiebertemp.setEnabled(true);
                textFiebertemp.setEnabled(true);
            } else {
                zeitFiebertemp.setEnabled(false);
                tvFiebertemp.setEnabled(false);
                textFiebertemp.setEnabled(false);
            }
        });

        // Send-Button -------------------------------------

        buttonSend.setOnClickListener(view -> {
            boolean setBlutdruck = false;
            if (checkBlutdruck.isChecked() && (textSYS.getText().length() == 0 || textDIA.getText().length() == 0)) {
                Toast.makeText(activityDoris.this, "Bitte alle Blutdruck-Werte ausfüllen!", Toast.LENGTH_LONG).show();
            } else if (checkBlutdruck.isChecked() && (textSYS.getText().length() > 0 || textDIA.getText().length() > 0)) {
                String hour = Integer.toString(hourBlutdruck);
                String minute = Integer.toString(minuteBlutdruck);
                String sys = textSYS.getText().toString();
                String dia = textDIA.getText().toString();
                msg = gson.toJson(new Blutdruck(MainActivity.ID, formattedDate + " " + hour + ":" + minute + ":00", sys, dia));
                MqttClient.publish("cmnd/Haussteuerung/Doris/Blutdruck", msg);
                setBlutdruck = true;
            }

            boolean setFiebertemp = false;
            if (checkFiebertemp.isChecked() && (textFiebertemp.getText().length() == 0)) {
                Toast.makeText(activityDoris.this, "Bitte einen Temperaturwert eingeben!", Toast.LENGTH_LONG).show();
            } else if (checkFiebertemp.isChecked() && (textFiebertemp.getText().length() > 0)) {
                String formattedDate = datetime.format(dateTimeFormat);
                String hour = Integer.toString(hourFiebertemp);
                String minute = Integer.toString(minuteFiebertemp);
                String fiebertemp = textFiebertemp.getText().toString();
                msg = gson.toJson(new Temperatur(MainActivity.ID, formattedDate + " " + hour + ":" + minute + ":00", fiebertemp));
                MqttClient.publish("cmnd/Haussteuerung/Doris/Temperatur", msg);
                Log.d("Doris", msg);
                setFiebertemp = true;
            }
            if (setBlutdruck || setFiebertemp) {
                finish();
            }
        });
    }

    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(this, onTimeSetListener, hour, minute, true);
        timePickerDialog.show();
    }

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

    @Override
    protected void onResume() {
        super.onResume();
    }

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

