package com.haussteuerung;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

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;

public class activityDorisLicht extends AppCompatActivity implements View.OnClickListener {

    // Position 0 = unten
    // Position 100 = oben
    public static boolean pausedDorisLicht = true;
    Gson gson = new Gson();
    String msg;

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

        pausedDorisLicht = false;

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

        Button buttonDeckenlampe = findViewById(R.id.buttonDeckenlampe);
        Button buttonDorisStehlampe = findViewById(R.id.buttonStehlampe);
        Button buttonBadDeckenlampe = findViewById(R.id.buttonDeckenlampeBad);
        Button buttonBadWandlampe = findViewById(R.id.buttonWandlampeBad);

        Button buttonRollo1 = findViewById(R.id.buttonRollo1);
        Button buttonRollo2 = findViewById(R.id.buttonRollo2);
        Button buttonRollo3 = findViewById(R.id.buttonRollo3);
        Button buttonRollo4 = findViewById(R.id.buttonRollo4);
        Button buttonRollo5 = findViewById(R.id.buttonRollo5);
        Button buttonRollo6 = findViewById(R.id.buttonRollo6);
        Button buttonRollo7 = findViewById(R.id.buttonRollo7);
        Button buttonRollo8 = findViewById(R.id.buttonRollo8);
        Button buttonRollo9 = findViewById(R.id.buttonRollo9);
        Button buttonRollo10 = findViewById(R.id.buttonRollo10);
        Button buttonRollo11 = findViewById(R.id.buttonRollo11);
        Button buttonRollo12 = findViewById(R.id.buttonRollo12);
        Button buttonRollo13 = findViewById(R.id.buttonRollo13);
        Button buttonRollo14 = findViewById(R.id.buttonRollo14);
        Button buttonRollo15 = findViewById(R.id.buttonRollo15);

        buttonDeckenlampe.setOnClickListener(this);
        buttonDorisStehlampe.setOnClickListener(this);
        buttonBadDeckenlampe.setOnClickListener(this);
        buttonBadWandlampe.setOnClickListener(this);
        buttonRollo1.setOnClickListener(this);
        buttonRollo2.setOnClickListener(this);
        buttonRollo3.setOnClickListener(this);
        buttonRollo4.setOnClickListener(this);
        buttonRollo5.setOnClickListener(this);
        buttonRollo6.setOnClickListener(this);
        buttonRollo7.setOnClickListener(this);
        buttonRollo8.setOnClickListener(this);
        buttonRollo9.setOnClickListener(this);
        buttonRollo10.setOnClickListener(this);
        buttonRollo11.setOnClickListener(this);
        buttonRollo12.setOnClickListener(this);
        buttonRollo13.setOnClickListener(this);
        buttonRollo14.setOnClickListener(this);
        buttonRollo15.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.buttonDeckenlampe:
                msg = gson.toJson(new Licht(MainActivity.ID, "Doris Deckenlampe", "Lamp21"));
                MqttClient.publish("cmnd/Haussteuerung/Licht", msg);
                // String msg1 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Doris Deckenlampe" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"power" + "\",\"Message\":\"2\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Licht", msg1);
                break;

            case R.id.buttonStehlampe:
                msg = gson.toJson(new Licht(MainActivity.ID, "Doris Stehlampe", "Lamp22"));
                MqttClient.publish("cmnd/Haussteuerung/Licht", msg);
                // String msg2 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Doris Stehlampe" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"power" + "\",\"Message\":\"2\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Licht", msg2);
                break;

            case R.id.buttonDeckenlampeBad:
                msg = gson.toJson(new Licht(MainActivity.ID, "Doris Bad Deckenlampe", "Lamp23"));
                MqttClient.publish("cmnd/Haussteuerung/Licht", msg);
                // String msg3 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Doris Bad Deckenlampe" + "\",\"Device\":\"" + "sonoff177" + "\",\"Power\":\"power1" + "\",\"Message\":\"2\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Licht", msg3);
                break;

            case R.id.buttonWandlampeBad:
                msg = gson.toJson(new Licht(MainActivity.ID, "Doris Bad Wandlampe", "Lamp24"));
                MqttClient.publish("cmnd/Haussteuerung/Licht", msg);
                // String msg4 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Doris Bad Wandlampe" + "\",\"Device\":\"" + "sonoff177" + "\",\"Power\":\"power2" + "\",\"Message\":\"2\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Licht", msg4);
                break;

            case R.id.buttonRollo1:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Alle Rollo", "Rollo1", 100));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg5 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Alle Rollo" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"100\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg5);
                break;

            case R.id.buttonRollo2:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Alle Rollo", "Rollo1", 50));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg6 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Alle Rollo" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"50\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg6);
                break;

            case R.id.buttonRollo3:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Alle Rollo", "Rollo1", 0));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg7 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Alle Rollo" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"0\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg7);
                break;

            case R.id.buttonRollo4:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Terrasse Rollo", "sonoff171", 100));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg8 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Terrasse Rollo" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"100\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg8);
                break;

            case R.id.buttonRollo5:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Terrasse Rollo", "sonoff171", 50));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg9 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Terrasse Rollo" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"50\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg9);
                break;

            case R.id.buttonRollo6:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Terrasse Rollo", "sonoff171", 0));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg10 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Terrasse Rollo" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"0\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg10);
                break;

            case R.id.buttonRollo7:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Rollo Fenster links", "sonoff172", 100));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg11 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Rollo Fenster links" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"100\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg11);
                break;

            case R.id.buttonRollo8:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Rollo Fenster links", "sonoff172", 50));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg12 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Rollo Fenster links" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"50\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg12);
                break;

            case R.id.buttonRollo9:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Rollo Fenster links", "sonoff172", 0));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg13 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Rollo Fenster links" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"0\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg13);
                break;

            case R.id.buttonRollo10:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Rollo Fenster rechts", "sonoff173", 100));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg14 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Rollo Fenster rechts" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"100\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg14);
                break;

            case R.id.buttonRollo11:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Rollo Fenster rechts", "sonoff173", 50));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg15 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Rollo Fenster rechts" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"50\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg15);
                break;

            case R.id.buttonRollo12:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Rollo Fenster rechts", "sonoff173", 0));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg16 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Rollo Fenster rechts" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"0\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg16);
                break;

            case R.id.buttonRollo13:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Bad Rollo", "sonoff174", 100));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg17 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Bad Rollo" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"100\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg17);
                break;

            case R.id.buttonRollo14:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Bad Rollo", "sonoff174", 50));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg18 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Bad Rollo" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"50\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg18);
                break;

            case R.id.buttonRollo15:
                msg = gson.toJson(new Rollo(MainActivity.ID, "Bad Rollo", "sonoff174", 0));
                MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
                // String msg19 = "{\"DeviceID\":\"" + MainActivity.ID + "\",\"Name\":\"Bad Rollo" + "\",\"Device\":\"" + v.getTag().toString() + "\",\"Power\":\"SHUTTERPOSITION" + "\",\"Message\":\"0\"}";
                // MqttClient.publish("cmnd/Haussteuerung/Rollo", msg19);
                break;
        }
    }

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

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

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

class Rollo {
    @SerializedName("DeviceID")
    private final String deviceId;
    @SerializedName("Name")
    private final String name;
    @SerializedName("Device")
    private final String device;
    @SerializedName("Shutterposition")
    private final int shutterposition;

    public Rollo(String deviceId, String name, String device, int shutterposition) {
        this.deviceId = deviceId;
        this.name = name;
        this.device = device;
        this.shutterposition = shutterposition;
    }
}