package com.haussteuerung;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;

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 java.util.ArrayList;

public class activityChristmas extends AppCompatActivity {

    static String power = "OFF";
    private String hsbColorMqtt = "";
    static ArrayList<String> hsbColorArray;
    private Button buttonPower;
    private Button buttonColor1, buttonColor2,
            buttonColor3, buttonColor4,
            buttonColor5,buttonColor6,
            buttonColor7, buttonColor8;
    private NumberPicker picker;

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

        // Ändern der Statusbar-Farbe
        androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setBackgroundDrawable(AppCompatResources.getDrawable(getApplicationContext(), R.drawable.gradient_titlebar));

        setContentView(R.layout.activity_christmas);
        setTitle("Haussteuerung - Christmas");

        Log.d("HSBColor0", hsbColorArray.get(0));
        Log.d("HSBColor1", hsbColorArray.get(1));
        Log.d("HSBColor2", hsbColorArray.get(2));

        buttonPower = findViewById(R.id.buttonPower);
        buttonColor1 = findViewById(R.id.buttonColor1);
        buttonColor2 = findViewById(R.id.buttonColor2);
        buttonColor3 = findViewById(R.id.buttonColor3);
        buttonColor4 = findViewById(R.id.buttonColor4);
        buttonColor5 = findViewById(R.id.buttonColor5);
        buttonColor6 = findViewById(R.id.buttonColor6);
        buttonColor7 = findViewById(R.id.buttonColor7);
        buttonColor8 = findViewById(R.id.buttonColor8);
        picker = findViewById(R.id.picker);

        handleButton();
        handleNumberPicker();
    }

    private void handleButton() {
        if (power.equals("ON")) {
            buttonPower.setText(R.string.on);
        } else if (power.equals("OFF")) {
            buttonPower.setText(R.string.off);
        }

        buttonColor1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setHSBColor("10");
                Log.d("HSBColorRed", hsbColorMqtt);
                MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
            }
        });

        buttonColor2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setHSBColor("50");
                Log.d("HSBColorYellow", hsbColorMqtt);
                MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
            }
        });

        buttonColor3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setHSBColor("100");
                Log.d("HSBColorGreen", hsbColorMqtt);
                MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
            }
        });

        buttonColor4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setHSBColor("150");
                Log.d("HSBColorLightBlue", hsbColorMqtt);
                MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
            }
        });

        buttonColor5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setHSBColor("200");
                Log.d("HSBColorBlue", hsbColorMqtt);
                MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
            }
        });

        buttonColor6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setHSBColor("250");
                Log.d("HSBColorPurple", hsbColorMqtt);
                MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
            }
        });

        buttonColor7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setHSBColor("300");
                Log.d("HSBColorWhite", hsbColorMqtt);
                MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
            }
        });

        buttonColor8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setHSBColor("350");
                Log.d("HSBColorChanging", hsbColorMqtt);
                MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
            }
        });

        buttonPower.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String hsbColorMqtt = hsbColorArray.get(0) + "," + hsbColorArray.get(1) + "," + hsbColorArray.get(2);
                if (buttonPower.getText().equals("ON")) {
                    MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"OFF\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                    MqttClient.publish("cmnd/sonoff153/POWER", "0");
                    buttonPower.setText(R.string.off);
                } else if (buttonPower.getText().equals("OFF")) {
                    MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"ON\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                    MqttClient.publish("cmnd/sonoff153/POWER", "1");
                    buttonPower.setText(R.string.on);
                }
            }
        });
    }

    private void handleNumberPicker() {
        String[] values = {"Bright (Steady On)","Gradually (Slow Fade)","Star (Sequential)","Flower (In Waves)",
                "Marquee (Chasing / Flash)","Fireworks (Twinkle / Flash)","Meteor","Stream"};
        picker.setWrapSelectorWheel(false);
        picker.setMinValue(0);
        picker.setMaxValue(values.length - 1);

        if (Integer.parseInt(hsbColorArray.get(2)) >= 0 && Integer.parseInt(hsbColorArray.get(2)) <= 12) {
            picker.setValue(0);
        }
        if (Integer.parseInt(hsbColorArray.get(2)) >= 13 && Integer.parseInt(hsbColorArray.get(2)) <= 24) {
            picker.setValue(1);
        }
        if (Integer.parseInt(hsbColorArray.get(2)) >= 25 && Integer.parseInt(hsbColorArray.get(2)) <= 37) {
            picker.setValue(2);
        }
        if (Integer.parseInt(hsbColorArray.get(2)) >= 38 && Integer.parseInt(hsbColorArray.get(2)) <= 49) {
            picker.setValue(3);
        }
        if (Integer.parseInt(hsbColorArray.get(2)) >= 50 && Integer.parseInt(hsbColorArray.get(2)) <= 62) {
            picker.setValue(4);
        }
        if (Integer.parseInt(hsbColorArray.get(2)) >= 63 && Integer.parseInt(hsbColorArray.get(2)) <= 74) {
            picker.setValue(5);
        }
        if (Integer.parseInt(hsbColorArray.get(2)) >= 75 && Integer.parseInt(hsbColorArray.get(2)) <= 87) {
            picker.setValue(6);
        }
        if (Integer.parseInt(hsbColorArray.get(2)) >= 88 && Integer.parseInt(hsbColorArray.get(2)) <= 100) {
            picker.setValue(7);
        }

        picker.setDisplayedValues(values);

        picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                if (numberPicker.getValue() == 0) {
                    String hsbColorMqtt = hsbColorArray.get(0) + ",100,10";
                    MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                    MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
                }
                if (numberPicker.getValue() == 1) {
                    String hsbColorMqtt = hsbColorArray.get(0) + ",100,20";
                    MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                    MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
                }
                if (numberPicker.getValue() == 2) {
                    String hsbColorMqtt = hsbColorArray.get(0) + ",100,30";
                    MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                    MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
                }
                if (numberPicker.getValue() == 3) {
                    String hsbColorMqtt = hsbColorArray.get(0) + ",100,40";
                    MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                    MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
                }
                if (numberPicker.getValue() == 4) {
                    String hsbColorMqtt = hsbColorArray.get(0) + ",100,50";
                    MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                    MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
                }
                if (numberPicker.getValue() == 5) {
                    String hsbColorMqtt = hsbColorArray.get(0) + ",100,70";
                    MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                    MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
                }
                if (numberPicker.getValue() == 6) {
                    String hsbColorMqtt = hsbColorArray.get(0) + ",100,80";
                    MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                    MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
                }
                if (numberPicker.getValue() == 7) {
                    String hsbColorMqtt = hsbColorArray.get(0) + ",100,90";
                    MqttClient.publish("tele/sonoff153/STATE", "{\"POWER\":\"" + buttonPower.getText() +"\",\"HSBColor\":\""+ hsbColorMqtt + "\"}", 2, true);
                    MqttClient.publish("cmnd/sonoff153/HSBColor", hsbColorMqtt);
                }
                Log.d("NumberPicker", String.valueOf(numberPicker.getValue()));
            }
        });
    }

    private void setHSBColor(String color) {
        if (picker.getValue() == 0) {
            hsbColorMqtt = color + ",100,10";
        }
        if (picker.getValue() == 1) {
            hsbColorMqtt = color + ",100,20";
        }
        if (picker.getValue() == 2) {
            hsbColorMqtt = color + ",100,30";
        }
        if (picker.getValue() == 3) {
            hsbColorMqtt = color + ",100,40";
        }
        if (picker.getValue() == 4) {
            hsbColorMqtt = color + ",100,50";
        }
        if (picker.getValue() == 5) {
            hsbColorMqtt = color + ",100,70";
        }
        if (picker.getValue() == 6) {
            hsbColorMqtt = color + ",100,80";
        }
        if (picker.getValue() == 7) {
            hsbColorMqtt = color + ",100,90";
        }
    }

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

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