using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class client_udp : MonoBehaviour
{

    // UDP send port
    private const int sendPort = 1234;

    // fingertips
    public GameObject index_tip;
    public GameObject thumb_tip;

    // slider values
    float slider_thumb_value;
    public GameObject ext_slider_thumb;
    float slider_index_value;
    public GameObject ext_slider_index;

    // values
    private float index_value;
    private float thumb_value;

    // mess to send
    private string mess;


    void UDPTest(string message)
    {
        UdpClient client = new UdpClient(sendPort);
        //client.Connect("192.168.200.68", sendPort);
        client.Connect("192.168.4.1", sendPort);
        try
        {

            byte[] sendBytes = Encoding.ASCII.GetBytes(message);
            client.Send(sendBytes, sendBytes.Length);

            IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);

            client.Close();

        }
        catch (Exception e)
        {
            print("Exception thrown" + e.Message);
        }
    }

    private void Update()
    {

        string specifier;
        CultureInfo culture;
        specifier = "G";
        culture = CultureInfo.CreateSpecificCulture("eu-ES");

        index_value = index_tip.GetComponent<fingertip_interaction_manager>().internal_displacement;
        thumb_value = thumb_tip.GetComponent<fingertip_interaction_manager>().internal_displacement;

        slider_thumb_value = ext_slider_thumb.GetComponent<Slider>().value;
        slider_index_value = ext_slider_index.GetComponent<Slider>().value;

        index_value = index_value + slider_index_value;
        thumb_value = thumb_value + slider_thumb_value;

        mess = index_value.ToString(specifier, CultureInfo.InvariantCulture) + ",            " + thumb_value.ToString(specifier, CultureInfo.InvariantCulture);
        UDPTest(mess);
        Debug.Log(mess);

    }

}
