package com.example.lfrmobileapp.ui.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
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.R;
import com.example.lfrmobileapp.databinding.FragmentManuellBinding;
import com.example.lfrmobileapp.ui.notifications.NotificationsFragment;

import io.github.controlwear.virtual.joystick.android.JoystickView;

public class HomeFragment extends Fragment {

    //Keys for Shared Preferences
    String ipKey = "ipkey";
    String portKey = "portkey";

    //Default Ip-Address and Port
    String ipAddress = "192.168.0.1";
    int port = 8000;

    Communication com = new Communication();
    DataTransferAsyncTask dataTransferAsyncTask;

    private FragmentManuellBinding binding;

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

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

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

        //Get saved Ip-Adress and Port from Shared Preferences
        ipAddress = sharedPref.getString(ipKey, "");
        port = sharedPref.getInt(portKey, 0);

        //Set text from Textview
        final TextView textView = binding.textHome;
        homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);

        dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port);
        dataTransferAsyncTask.execute();

        binding.rotateLeft.setOnTouchListener(new View.OnTouchListener(){
            @SuppressLint("ClickableViewAccessibility")
            @Override
                public boolean onTouch(View v, MotionEvent event) {

                    if(event.getAction() == MotionEvent.ACTION_UP){
                        binding.rotateLeft.performClick();
                        dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, 0, 0, 0}, 0));
                        return true;
                    }else{
                        dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{-1, 1, -1, 1}, 100));
                    }
                    return false;
                }
            });

        binding.rotateRight.setOnTouchListener(new View.OnTouchListener(){
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if(event.getAction() == MotionEvent.ACTION_UP){
                    binding.rotateRight.performClick();
                    dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, 0, 0, 0}, 0));
                    return true;
                }else{
                    dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{1, -1, 1, -1}, 100));
                }
                return false;
            }
        });


        JoystickView joystick = (JoystickView) binding.joystick;
        joystick.setOnMoveListener(new JoystickView.OnMoveListener() {
            @Override
            public void onMove(int angle, int strength) {
                homeViewModel.setText(Integer.toString(angle), Integer.toString(strength));
                //8 segments of the joystick
                if((angle >= 338 || angle < 22) && strength != 0){
                    joystick.setBackgroundResource(R.mipmap.right);
                    dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{1, -1, -1, 1}, strength));
                }else if((angle >= 22 && angle < 67) && strength != 0){
                    joystick.setBackgroundResource(R.mipmap.right_forward);
                    dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{1, 0, 0, 1}, strength));
                }else if((angle >=67 && angle < 112) && strength != 0){
                    joystick.setBackgroundResource(R.mipmap.forward);
                    dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{1, 1, 1, 1}, strength));
                }else if((angle >=112 && angle < 157) && strength != 0){
                    joystick.setBackgroundResource(R.mipmap.left_forward);
                    dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, 1, 1, 0}, strength));
                }else if((angle >=157 && angle < 202) && strength != 0){
                    joystick.setBackgroundResource(R.mipmap.left);
                    dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{-1, 1, 1, -1}, strength));
                }else if((angle >=202 && angle < 247) && strength != 0){
                    joystick.setBackgroundResource(R.mipmap.left_back);
                    dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{-1, 0, 0, -1}, strength));
                }else if((angle >=247 && angle < 292) && strength != 0){
                    joystick.setBackgroundResource(R.mipmap.back);
                    dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{-1, -1, -1, -1}, strength));
                }else if((angle >=292 && angle < 337) && strength != 0){
                    joystick.setBackgroundResource(R.mipmap.right_back);
                    dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, -1, -1, 0}, strength));
                }
                else{
                    joystick.setBackgroundResource(R.mipmap.blank);
                    dataTransferAsyncTask.writeTelegram(com.telegram(new int[]{0, 0, 0, 0}, strength));
                }
            }
        });

        return root;
    }

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

