package com.haussteuerung;

import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
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.ortiz.touchview.TouchImageView;

import java.io.IOException;

public class activityGlocke extends AppCompatActivity {
    Gson gson = new Gson();
    String msg;

    @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_glocke);
        setTitle("Haussteuerung - Glocke");

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

        Intent i = getIntent();
        Bitmap bitmap;
        if ((i.getExtras() != null) && (i.hasExtra("TimeFB"))) {
            bitmap = loadBitmapFromMediaStore(getApplicationContext(), i.getStringExtra("TimeFB") + ".jpg");
        } else {
            bitmap = loadBitmapFromMediaStore(getApplicationContext(), "cam.jpg");
        }

        if (bitmap != null) {
            fotoGlocke.setImageBitmap(bitmap);
        }

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

    public Bitmap loadBitmapFromMediaStore(Context context, String filename) {
        Uri collection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projection = new String[]{MediaStore.Images.Media._ID};
        String selection = MediaStore.Images.Media.DISPLAY_NAME + "=?";
        String[] selectionArgs = new String[]{filename};

        Cursor cursor = context.getContentResolver().query(collection, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
            long id = cursor.getLong(idColumn);
            cursor.close();

            Uri imageUri = ContentUris.withAppendedId(collection, id);
            try {
                return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageUri));
            } catch (IOException e) {
                Log.d("loadBitmapFromMediaStore - activityGlocke", "Loading bitmap cam.jpg from MediaStore failed");
            }
        }
        return null;
    }

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

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

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