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 com.haussteuerung.helper.Sky;

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

public class activitySky extends AppCompatActivity {

    private Button buttonSkyZeit;
    private CheckBox checkSky;
    public static String zeitSkyMqtt = "";
    public static boolean isChecked = false;

    @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));

        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(v -> timePickerDialog(buttonSkyZeit));

        buttonSkySend.setOnClickListener(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 = (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();
        buttonSkyZeit.setText(zeitSkyMqtt);
        checkSky.setChecked(isChecked);
    }

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

