package com.haussteuerung;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

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.ortiz.touchview.TouchImageView;

import java.io.File;

public class activityGlocke extends AppCompatActivity {

    public static boolean pausedGlocke = 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));

        pausedGlocke = false;

        setContentView(R.layout.activity_glocke);
        setTitle("Haussteuerung - Glocke");

        TouchImageView fotoGlocke = findViewById(R.id.fotoGlocke);

        Intent i = getIntent();
        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/Haussteuerung/cam.jpg");
        if (i.getExtras() != null) {
            if (i.getExtras() != null && i.hasExtra("TimeFB")) {
                file = new File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/Haussteuerung/" + i.getStringExtra("TimeFB") + ".jpg");
                Log.d("ZeitGlocke", i.getStringExtra("TimeFB"));
            } else {
                file = new File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/Haussteuerung/cam.jpg");
            }
        }

        if(file.exists()){
            Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            Log.d("ImageView", file.getAbsolutePath());
            fotoGlocke.setImageBitmap(myBitmap);
            Log.d("Foto", "ImageView erfolgreich");
        } else {
            Log.d("Foto", "ImageView fehlgeschlagen");
            Toast.makeText(this, "Laden des Fotos fehlgeschlagen", Toast.LENGTH_LONG).show();
        }

        Button buttonGlockeDoors = findViewById(R.id.buttonGlockeDoors);
        buttonGlockeDoors.setOnClickListener(view -> {
            msg = gson.toJson(new Licht(MainActivity.ID, "Gartentor", "Lamp10"));
            MqttClient.publish("cmnd/Haussteuerung/Licht", msg);
            msg = gson.toJson(new Licht(MainActivity.ID, "Eingangstuer", "Lamp11"));
            MqttClient.publish("cmnd/Haussteuerung/Licht", msg);
        });

        Button buttonKamera = findViewById(R.id.buttonKamera);
        buttonKamera.setOnClickListener(view -> {
            String url = "https://webcam.klebl.info/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=wuuPhkmUCeI9WG7C&user=admin&password=Florian1";
            Intent intentOne = new Intent(Intent.ACTION_VIEW);
            intentOne.setData(Uri.parse(url));
            startActivity(intentOne);
        });
    }

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

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

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