package com.haussteuerung;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TimePicker;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

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

public class activitySky extends AppCompatActivity {

    private Button buttonSkyZeit;
    private CheckBox checkSky;
    public static String zeitSky = "", zeitSkyMqtt = "";
    public static boolean isChecked = false, pausedSky = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        pausedSky = false;

        setContentView(R.layout.activity_sky);
        setTitle("Haussteuerung - Sky");

        checkSky = findViewById(R.id.checkSky);
        buttonSkyZeit = findViewById(R.id.buttonSkyZeit);
        Button buttonSkySend = findViewById(R.id.buttonSkySend);

        buttonSkyZeit.setText(zeitSkyMqtt);
        checkSky.setChecked(isChecked);

        buttonSkyZeit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timePickerDialog(buttonSkyZeit);
            }
        });

        buttonSkySend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Gson gson = new Gson();
                String msg;
                if (checkSky.isChecked()) {
                    msg = gson.toJson(new Sky(MainActivity.ID, buttonSkyZeit.getText().toString(), true));
                    MqttClient.publish("cmnd/Haussteuerung/Sky", msg);
                } else if (!checkSky.isChecked()) {
                    msg = gson.toJson(new Sky(MainActivity.ID, buttonSkyZeit.getText().toString(), false));
                    MqttClient.publish("cmnd/Haussteuerung/Sky", msg);
                }
                finish();
            }
        });

        Log.d("ActivitySky","Created");
    } public void timePickerDialog(Button button) {
        LocalTime localTime = LocalTime.parse(button.getText());
        int hour = localTime.getHour();
        int minute = localTime.getMinute();
        TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int selectedHour, int 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();
        pausedSky = true;
        // Log.d("TimerSkyActivity", "Canceled");
        Log.d("ActivitySky","Paused");
    }

    @Override
    protected void onResume() {
        super.onResume();
        pausedSky = false;
        buttonSkyZeit.setText(zeitSkyMqtt);
        checkSky.setChecked(isChecked);
        Log.d("ActivitySky","Resumed");
    }

    @Override
    protected void onStop() {
        super.onStop();
        pausedSky = true;
        Log.d("ActivitySky","Stopped");
    }
}

class Sky {
    @SerializedName("DeviceID")
    private final String deviceId;
    @SerializedName("Datum")
    private final String datum;
    @SerializedName("CheckboxSky")
    private final boolean checkboxSky;

    public Sky(String deviceId, String datum, boolean checkboxSky) {
        this.deviceId = deviceId;
        this.datum = datum;
        this.checkboxSky = checkboxSky;
    }
}