package com.haussteuerung;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

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

import java.util.Timer;
import java.util.TimerTask;

public class activityHaus extends AppCompatActivity {
    private TextView tv_Zustand1;
    private TextView tv_Zustand2;
    private TextView tv_Zustand3;
    private TextView tv_Zustand4;
    private TextView tv_Zustand5;
    private TextView tv_Zustand6;
    private TextView tv_Zustand7;
    // private TextView tv_Zustand8;
    private TextView tv_Zustand9;
    private TextView tv_Zustand10;
    private TextView tv_Zustand11;
    private TextView tv_Zustand12;
    private TextView tv_Zustand13;
    private TextView tv_Zustand14;
    private TextView tv_Zustand15;
    private TextView tv_Zustand16;
    private TextView tv_Zustand17;
    private TextView tv_Zustand18;

    final Handler myHandler = new Handler();
    final Timer myTimer = new Timer();

    @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_haus);
        setTitle("Haussteuerung - Hausansicht");

        tv_Zustand1 = findViewById(R.id.tv_Zustand1);
        tv_Zustand2 = findViewById(R.id.tv_Zustand2);
        tv_Zustand3 = findViewById(R.id.tv_Zustand3);
        tv_Zustand4 = findViewById(R.id.tv_Zustand4);
        tv_Zustand5 = findViewById(R.id.tv_Zustand5);
        tv_Zustand6 = findViewById(R.id.tv_Zustand6);
        tv_Zustand7 = findViewById(R.id.tv_Zustand7);
        tv_Zustand9 = findViewById(R.id.tv_Zustand9);
        tv_Zustand10 = findViewById(R.id.tv_Zustand10);
        tv_Zustand11 = findViewById(R.id.tv_Zustand11);
        tv_Zustand12 = findViewById(R.id.tv_Zustand12);
        tv_Zustand13 = findViewById(R.id.tv_Zustand13);
        tv_Zustand14 = findViewById(R.id.tv_Zustand14);
        tv_Zustand15 = findViewById(R.id.tv_Zustand15);
        tv_Zustand16 = findViewById(R.id.tv_Zustand16);
        tv_Zustand17 = findViewById(R.id.tv_Zustand17);
        tv_Zustand18 = findViewById(R.id.tv_Zustand18);
    }

    // Runnable method
    final Runnable myRunnable = new Runnable() {
        public void run() {
            if (MqttClient.tv_Zustand1.equals("zu")) {
                tv_Zustand1.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand1.setTextColor(Color.RED);
            } else {
                tv_Zustand1.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand1.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand2.equals("zu")) {
                tv_Zustand2.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand2.setTextColor(Color.RED);
            } else {
                tv_Zustand2.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand2.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand3.equals("zu")) {
                tv_Zustand3.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand3.setTextColor(Color.RED);
            } else {
                tv_Zustand3.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand3.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand4.equals("zu")) {
                tv_Zustand4.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand4.setTextColor(Color.RED);
            } else {
                tv_Zustand4.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand4.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand5.equals("zu")) {
                tv_Zustand5.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand5.setTextColor(Color.RED);
            } else {
                tv_Zustand5.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand5.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand6.equals("zu")) {
                tv_Zustand6.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand6.setTextColor(Color.RED);
            } else {
                tv_Zustand6.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand6.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand7.equals("zu")) {
                tv_Zustand7.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand7.setTextColor(Color.RED);
            } else {
                tv_Zustand7.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand7.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand9.equals("zu")) {
                tv_Zustand9.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand9.setTextColor(Color.RED);
            } else {
                tv_Zustand9.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand9.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand10.equals("zu")) {
                tv_Zustand10.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand10.setTextColor(Color.RED);
            } else {
                tv_Zustand10.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand10.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand11.equals("zu")) {
                tv_Zustand11.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand11.setTextColor(Color.RED);
            } else {
                tv_Zustand11.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand11.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand12.equals("zu")) {
                tv_Zustand12.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand12.setTextColor(Color.RED);
            } else {
                tv_Zustand12.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand12.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand13.equals("zu")) {
                tv_Zustand13.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand13.setTextColor(Color.RED);
            } else {
                tv_Zustand13.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand13.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand14.equals("zu")) {
                tv_Zustand14.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand14.setTextColor(Color.RED);
            } else {
                tv_Zustand14.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand14.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand15.equals("zu")) {
                tv_Zustand15.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand15.setTextColor(Color.RED);
            } else {
                tv_Zustand15.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand15.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand16.equals("zu")) {
                tv_Zustand16.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand16.setTextColor(Color.RED);
            } else {
                tv_Zustand16.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand16.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand17.equals("zu")) {
                tv_Zustand17.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand17.setTextColor(Color.RED);
            } else {
                tv_Zustand17.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand17.setTextColor(Color.GREEN);
            }

            if (MqttClient.tv_Zustand18.equals("zu")) {
                tv_Zustand18.setText(getResources().getString(R.string.geschlossen));
                tv_Zustand18.setTextColor(Color.RED);
            } else {
                tv_Zustand18.setText(getResources().getString(R.string.geoeffnet));
                tv_Zustand18.setTextColor(Color.GREEN);
            }        }
    };

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

    @Override
    protected void onResume() {
        super.onResume();
        TimerTask myTask = new TimerTask() {
            public void run() {
                myHandler.post(myRunnable); // updateUI method
            }
        };
        myTimer.schedule(myTask,0,1000); // TimerTask, delay, period
    }

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