package com.haussteuerung;

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

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

import com.google.gson.Gson;
import com.haussteuerung.helper.Licht;
import com.haussteuerung.helper.Rollo;

import java.util.HashMap;
import java.util.Map;

public class activityDorisLicht extends AppCompatActivity implements View.OnClickListener {

    // Position 0 = unten
    // Position 100 = oben

    private final Map<Integer, Rollo> rolloMap = new HashMap<>();
    Gson gson = new Gson();

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

        initRolloMap(MainActivity.ID);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        String msg;
        if (id == R.id.buttonDeckenlampe) {
            msg = gson.toJson(new Licht(MainActivity.ID, "Doris Deckenlampe", "Lamp21"));
            MqttClient.publish("cmnd/Haussteuerung/Licht", msg);
        } else if (id == R.id.buttonStehlampe) {
            msg = gson.toJson(new Licht(MainActivity.ID, "Doris Stehlampe", "Lamp22"));
            MqttClient.publish("cmnd/Haussteuerung/Licht", msg);
        } else if (id == R.id.buttonDeckenlampeBad) {
            msg = gson.toJson(new Licht(MainActivity.ID, "Doris Bad Deckenlampe", "Lamp23"));
            MqttClient.publish("cmnd/Haussteuerung/Licht", msg);
        } else if (id == R.id.buttonWandlampeBad) {
            msg = gson.toJson(new Licht(MainActivity.ID, "Doris Bad Wandlampe", "Lamp24"));
            MqttClient.publish("cmnd/Haussteuerung/Licht", msg);
        }

        Rollo rollo = rolloMap.get(id);
        if (rollo != null) {
            msg = gson.toJson(rollo);
            MqttClient.publish("cmnd/Haussteuerung/Rollo", msg);
        } else {
            Log.w("RolloControl", "Unknown button: " + id);
        }
    }

    private void initRolloMap(String androidId) {
        rolloMap.put(R.id.buttonRollo1, new Rollo(androidId, "Alle Rollo", "Rollo1", 100));
        rolloMap.put(R.id.buttonRollo2, new Rollo(androidId, "Alle Rollo", "Rollo1", 50));
        rolloMap.put(R.id.buttonRollo3, new Rollo(androidId, "Alle Rollo", "Rollo1", 0));

        rolloMap.put(R.id.buttonRollo4, new Rollo(androidId, "Terrasse Rollo", "sonoff171", 100));
        rolloMap.put(R.id.buttonRollo5, new Rollo(androidId, "Terrasse Rollo", "sonoff171", 50));
        rolloMap.put(R.id.buttonRollo6, new Rollo(androidId, "Terrasse Rollo", "sonoff171", 0));

        rolloMap.put(R.id.buttonRollo7, new Rollo(androidId, "Rollo Fenster links", "sonoff172", 100));
        rolloMap.put(R.id.buttonRollo8, new Rollo(androidId, "Rollo Fenster links", "sonoff172", 50));
        rolloMap.put(R.id.buttonRollo9, new Rollo(androidId, "Rollo Fenster links", "sonoff172", 0));

        rolloMap.put(R.id.buttonRollo10, new Rollo(androidId, "Rollo Fenster rechts", "sonoff173", 100));
        rolloMap.put(R.id.buttonRollo11, new Rollo(androidId, "Rollo Fenster rechts", "sonoff173", 50));
        rolloMap.put(R.id.buttonRollo12, new Rollo(androidId, "Rollo Fenster rechts", "sonoff173", 0));

        rolloMap.put(R.id.buttonRollo13, new Rollo(androidId, "Bad Rollo", "sonoff174", 100));
        rolloMap.put(R.id.buttonRollo14, new Rollo(androidId, "Bad Rollo", "sonoff174", 50));
        rolloMap.put(R.id.buttonRollo15, new Rollo(androidId, "Bad Rollo", "sonoff174", 0));
    }


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

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

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

