package com.example.lfrmobileapp.ui.notifications;

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.Toast;

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

import com.example.lfrmobileapp.Communication;
import com.example.lfrmobileapp.DataTransferAsyncTask;
import com.example.lfrmobileapp.databinding.FragmentEinstellungenBinding;

import java.io.DataOutputStream;
import java.net.Socket;


/*class NetworkThread extends Thread {
    String ip;
    int port;
    boolean connected;

    NetworkThread(String ip,  int port) {
        this.ip = ip;
        this.port = port;
        this.connected = false;
    }

    public void run() {
        try {
            Socket socket = new Socket(ip, port);
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
            outputStream.writeBytes("Verbindung aufbauen");
            outputStream.flush();
            socket.close();
            connected = true;

        } catch (Exception e) {
            e.printStackTrace();
            connected = false;
        }
    }
}*/

public class NotificationsFragment extends Fragment {

    Boolean connected = false;

    String ipKey = "ipkey";
    String portKey = "portkey";
    String connectionFailed = "Verbindung fehlgeschlagen.";
    String connectionSuccessful = "Verbindung erfolgreich.";

    String ipAddress = "192.168.0.1";
    int port = 8000;

    int duration = Toast.LENGTH_LONG;
    DataTransferAsyncTask dataTransferAsyncTask;

    private FragmentEinstellungenBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        NotificationsViewModel notificationsViewModel =
                new ViewModelProvider(this).get(NotificationsViewModel.class);

        binding = FragmentEinstellungenBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        Communication com = new Communication();

        SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();

        ipAddress = sharedPref.getString(ipKey, "");
        binding.ipAdress.setText(ipAddress);
        port = sharedPref.getInt(portKey, -1);
        if(port != -1){
            binding.port.setText(Integer.toString(port));
        }else{
            binding.port.setText("");
        }

        dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port);



        final Button button = binding.connect;
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ipAddress = String.valueOf(binding.ipAdress.getText());
                port = Integer.parseInt(String.valueOf(binding.port.getText()));

                new Thread(new Runnable(){
                    public void run(){
                        try {
                            Socket socket = new Socket(ipAddress, port);
                            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
                            outputStream.writeBytes("Verbindung aufbauen");
                            outputStream.flush();
                            socket.close();
                            connected = true;

                        } catch (Exception e) {
                            e.printStackTrace();
                            connected = false;
                        }
                    }
                }).start();

                if(connected){
                    editor.putString(ipKey, ipAddress);
                    editor.putInt(portKey, port);
                    editor.apply();
                    Toast toast = Toast.makeText(v.getContext(), connectionSuccessful, duration);
                    toast.show();
                }else{
                    Toast toast = Toast.makeText(v.getContext(), connectionFailed, duration);
                    toast.show();
                    dataTransferAsyncTask.cancel(true);
                }
            }
        });

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        dataTransferAsyncTask.cancel(true);
        binding = null;
    }
}