package de.jg_cody.Teraplex.ui.home;

import android.content.Context;
import android.content.SharedPreferences;
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.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

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 wlan_aus_Button;
    Button wlan_an_Button;


    private HomeViewModel homeViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        SharedPreferences prefs = getContext().getSharedPreferences("logindata", 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);
        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("");
        } else {
            login_data.setText("Sie sind als " + user + " bei " + ip + " angemeldet!");
        }

        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        userInput = (EditText) root.findViewById(R.id.userInput);
        passwordInput = (EditText) root.findViewById(R.id.passwordInput);
        ipInput = (EditText) root.findViewById(R.id.ipInput);
        loginButton = (Button) root.findViewById(R.id.loginButton);
        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(), "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 = getContext().getSharedPreferences("logindata", Context.MODE_PRIVATE);
                    p.edit().putString("user", user).putString("password", password).putString("ip", ip).apply();
                    Toast.makeText(getContext(), "Login-Daten gespeichert...", Toast.LENGTH_SHORT).show();
                    if (user == null || ip == null) {
                        login_data.setText("");
                    } else {
                        login_data.setText("Sie sind als " + user + " bei " + ip + " angemeldet!");
                    }
                }
            }
        });
        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) {
                }
            }
        });
        return root;
    }
}
