package de.jg_cody.Teraplex.ui.Schlafzimmer;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import java.util.Base64;

import de.jg_cody.Teraplex.MainActivity;
import de.jg_cody.Teraplex.R;
import de.jg_cody.Teraplex.SSH_connection;

public class SchlafzimmerFragment extends Fragment {

    public static String user, password, ip;

    Button schlafzimmeru1aus;
    Button schlafzimmeru1an;
    Button schlafzimmeru2aus;
    Button schlafzimmeru2an;
    Button schlafzimmeru3aus;
    Button schlafzimmeru3an;

    private SchlafzimmerViewModel schlafzimmerViewModel;

    @RequiresApi(api = Build.VERSION_CODES.O)
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        SharedPreferences prefs = requireContext().getSharedPreferences("appsettings", Context.MODE_PRIVATE);
        user = prefs.getString("user", null);
        password = prefs.getString("password", null);
        ip = prefs.getString("ip", null);

        schlafzimmerViewModel =
                new ViewModelProvider(this).get(SchlafzimmerViewModel.class);
        View root = inflater.inflate(R.layout.fragment_schlafzimmer, container, false);
        SharedPreferences p = requireContext().getSharedPreferences("appsettings", Activity.MODE_PRIVATE);
        String Background = p.getString("Background", null);
        if (Background != null) {
            ImageView I = root.findViewById(R.id.Background);
            byte[] BA = Base64.getDecoder().decode(Background);
            I.setImageDrawable(new BitmapDrawable(getResources(), MainActivity.scaleCenterCrop(BitmapFactory.decodeByteArray(BA, 0, BA.length), MainActivity.getScreenHeight(), MainActivity.getScreenWidth())));
            I.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }


        schlafzimmeru1aus = (Button) root.findViewById(R.id.schlafzimmeru1aus);
        schlafzimmeru1aus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Toast.makeText(getContext(), "Lampe wird ausgeschaltet...", Toast.LENGTH_SHORT).show();
                    SSH_connection.executeRemoteCommand(ip, user, password, "sudo /root/raspberry-remote/send 11111 4 0");
                } catch (Exception ignored) {
                }
            }

        });
        schlafzimmeru1an = (Button) root.findViewById(R.id.schlafzimmeru1an);
        schlafzimmeru1an.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Toast.makeText(getContext(), "Lampe wird angeschaltet...", Toast.LENGTH_SHORT).show();
                    SSH_connection.executeRemoteCommand(ip, user, password, "sudo /root/raspberry-remote/send 11111 4 1");
                } catch (Exception ignored) {
                }
            }

        });
        schlafzimmeru2aus = (Button) root.findViewById(R.id.schlafzimmeru2aus);
        schlafzimmeru2aus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Toast.makeText(getContext(), "Lampe wird ausgeschaltet...", Toast.LENGTH_SHORT).show();
                    SSH_connection.executeRemoteCommand(ip, user, password, "sudo /root/raspberry-remote/send 11111 2 0");
                } catch (Exception ignored) {
                }
            }

        });
        schlafzimmeru2an = (Button) root.findViewById(R.id.schlafzimmeru2an);
        schlafzimmeru2an.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Toast.makeText(getContext(), "Lampe wird angeschaltet...", Toast.LENGTH_SHORT).show();
                    SSH_connection.executeRemoteCommand(ip, user, password, "sudo /root/raspberry-remote/send 11111 2 1");
                } catch (Exception ignored) {
                }
            }

        });
        schlafzimmeru3aus = (Button) root.findViewById(R.id.schlafzimmeru3aus);
        schlafzimmeru3aus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Toast.makeText(getContext(), "Lampe wird ausgeschaltet...", Toast.LENGTH_SHORT).show();
                    SSH_connection.executeRemoteCommand(ip, user, password, "sudo /root/raspberry-remote/send 11111 3 0");
                } catch (Exception ignored) {
                }
            }

        });
        schlafzimmeru3an = (Button) root.findViewById(R.id.schlafzimmeru3an);
        schlafzimmeru3an.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Toast.makeText(getContext(), "Lampe wird angeschaltet...", Toast.LENGTH_SHORT).show();
                    SSH_connection.executeRemoteCommand(ip, user, password, "sudo /root/raspberry-remote/send 11111 3 1");
                } catch (Exception ignored) {
                }
            }

        });
        return root;
    }
}