package com.haussteuerung;

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

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.Timer;
import java.util.TimerTask;

public class activityLog extends AppCompatActivity {

    public static boolean pausedLog = true;
    private TextView textLog;
    final Handler myHandler = new Handler();
    final Timer myTimer = new Timer();

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

        pausedLog = false;

        setContentView(R.layout.activity_log);
        setTitle("Haussteuerung - Log");

        textLog = findViewById(R.id.textLog);
    }

    // Runnable method
    final Runnable myRunnable = new Runnable() {
        public void run() {
            textLog.setText(MqttClient.logContent);
        }
    };

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

    @Override
    protected void onResume() {
        super.onResume();
        pausedLog = false;
        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();
        pausedLog = true;
        myTimer.cancel();
    }
}