package de.jg_cody.Teraplex.ui.home;

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.EditText;
import android.widget.ImageView;
import android.widget.TextView;
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 HomeFragment extends Fragment {

    public static String user, password, ip;

    public static EditText ipInput;
    public static EditText userInput;
    public static EditText passwordInput;
    Button loginButton;
    Button logoutButton;
    Button wlan_aus_Button;
    Button wlan_an_Button;
    Button kodian;
    Button kodiaus;


    private HomeViewModel homeViewModel;

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

        homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, 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);
        }
        final TextView textView = root.findViewById(R.id.text_home);
        final TextView login_data = root.findViewById(R.id.login_data);
        if (user == null || ip == null) {
            login_data.setText(R.string.please_login);
        } else {
            login_data.setText(getString(R.string.sie_sind_als_angemeldet).replace("{IP}", ip).replace("{USERNAME}", user));
        }
        userInput = (EditText) root.findViewById(R.id.loginuserInput);
        passwordInput = (EditText) root.findViewById(R.id.loginpasswordInput);
        ipInput = (EditText) root.findViewById(R.id.ipInput);
        loginButton = (Button) root.findViewById(R.id.loginButton);
        SharedPreferences t = requireContext().getSharedPreferences("appsettings", Context.MODE_PRIVATE);
        if (user == null || ip == null) {
            loginButton.setVisibility(View.VISIBLE);
        } else {
            loginButton.setVisibility(View.INVISIBLE);
            ipInput.setVisibility(View.GONE);
            userInput.setVisibility(View.GONE);
            passwordInput.setVisibility(View.GONE);
        }
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userInput.getText().toString().trim().length() == 0 || ipInput.getText().toString().trim().length() == 0 || passwordInput.getText().toString().trim().length() == 0) {
                    Toast.makeText(getContext(), getString(R.string.felder_dürfen_nicht_leer_sein), Toast.LENGTH_SHORT).show();
                } else {
                    user = userInput.getText().toString();
                    password = passwordInput.getText().toString();
                    ip = ipInput.getText().toString();
                    SharedPreferences p = requireContext().getSharedPreferences("appsettings", Context.MODE_PRIVATE);
                    p.edit().putString("user", user).putString("password", password).putString("ip", ip).apply();
                    Toast.makeText(getContext(), getString(R.string.login_saved), Toast.LENGTH_SHORT).show();
                    if (user == null || ip == null) {
                        login_data.setText("");
                    } else {
                        login_data.setText(getString(R.string.sie_sind_als_angemeldet).replace("{IP}", ip).replace("{USERNAME}", user));
                        logoutButton.setVisibility(View.VISIBLE);
                        loginButton.setVisibility(View.INVISIBLE);
                        ipInput.setVisibility(View.GONE);
                        userInput.setVisibility(View.GONE);
                        passwordInput.setVisibility(View.GONE);
                    }
                }
            }
        });
        logoutButton = (Button) root.findViewById(R.id.logoutButton);
        if (user == null || ip == null) {
            logoutButton.setVisibility(View.INVISIBLE);
            ipInput.setVisibility(View.VISIBLE);
            userInput.setVisibility(View.VISIBLE);
            passwordInput.setVisibility(View.VISIBLE);
        } else {
            logoutButton.setVisibility(View.VISIBLE);
        }
        logoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requireContext().getSharedPreferences("appsettings", Context.MODE_PRIVATE).edit().remove("ip").remove("user").remove("password").apply();
                startActivity(requireActivity().getIntent());
                requireActivity().finish();
            }
        });
        wlan_aus_Button = (Button) root.findViewById(R.id.wlan_aus);
        wlan_aus_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Toast.makeText(getContext(), "WLAN wird ausgeschaltet...", Toast.LENGTH_SHORT).show();
                    SSH_connection.executeRemoteCommand(ip, user, password, "service hostapd stop");
                } catch (Exception e) {
                }
            }
        });
        wlan_an_Button = (Button) root.findViewById(R.id.wlan_an);
        wlan_an_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Toast.makeText(getContext(), "WLAN wird eingeschaltet...", Toast.LENGTH_SHORT).show();
                    SSH_connection.executeRemoteCommand(ip, user, password, "service hostapd start");
                } catch (Exception e) {
                }
            }
        });
        kodiaus = (Button) root.findViewById(R.id.kodiaus);
        kodiaus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

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

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

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

        });
        return root;
    }
}
