using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;


public class RemoteCam : MonoBehaviour
{
    public const string CAM_URI_INTERNAL = "http://192.168.1.254";
    public const string CAM_REC_START = "/?custom=1&cmd=2001&par=1";
    public const string CAM_REC_STOP = "/?custom=1&cmd=2001&par=0";

    public static bool waitingRemoteCamAnswer = false;
    public static Dictionary<uint, string> camQueryBuffer = new Dictionary<uint, string>();
    public static string camUriExternal = "";

    public Text camIPInputField;
    //public Button camConnectButton, camDisconnectButton, camRecStartButton, camRecStopButton;

    void Start()
    {
        //camConnectButton.onClick.AddListener(CamConnectButton_OnClick);
        //camDisconnectButton.onClick.AddListener(CamDisconnectButton_OnClick);
        //camRecStartButton.onClick.AddListener(CamRecStartButton_OnClick);
        //camRecStopButton.onClick.AddListener(CamRecStopButton_OnClick);
    }

    private void CamConnectButton_OnClick()
    {
        string camIP = "http://" + camIPInputField.text;

        if (camIPInputField.text != "")
        {
            Debug.Log("Connecting to remote camera, address: " + camIP);
            //ANutils.Main.RemoteCamConnection("CONNECT", camIP);
        }
        else
            Debug.Log("Incorrect remote camera address: " + camIP);
    }

    private void CamDisconnectButton_OnClick()
    {

    }

    private void CamRecStartButton_OnClick()
    {
        CamRecStart();
    }

    private void CamRecStopButton_OnClick()
    {
        CamRecStop();
    }

    public void CamRecStart()
    {
        if (!waitingRemoteCamAnswer)
        {
            camQueryBuffer.Add(0, CAM_REC_START);
            RemoteCamAPI(camUriExternal, camQueryBuffer);
        }
        else
            Debug.Log("CamRecStart: query aborted, another query in process...");
    }

    public void CamRecStop()
    {
        if (!waitingRemoteCamAnswer)
        {
            camQueryBuffer.Add(0, CAM_REC_STOP);
            RemoteCamAPI(camUriExternal, camQueryBuffer);
        }        
        else
            Debug.Log("CamRecStop: query aborted, another query in process...");
    }

    public void CamRecStopStart()
    {
        if (!waitingRemoteCamAnswer)
        {
            camQueryBuffer.Add(0, CAM_REC_STOP);
            camQueryBuffer.Add(1, CAM_REC_START);
            RemoteCamAPI(camUriExternal, camQueryBuffer);
        }        
        else
            Debug.Log("CamRecStopStart: query aborted, another query in process...");
    }

    public void RemoteCamAPI(string camURI, Dictionary<uint, string> queryBuffer)
    {
        StartCoroutine(RemoteCamAPIAsync(camURI, queryBuffer));
    }

    private IEnumerator RemoteCamAPIAsync(string camURI, Dictionary<uint, string> queryBuffer)
    {
        if (camURI != "")
        {
            foreach (KeyValuePair<uint, string> camCommand in queryBuffer)
            {
                Debug.Log("RemoteCam Send: " + camURI + camCommand.Value);

                waitingRemoteCamAnswer = true;
                using (UnityWebRequest uwr = UnityWebRequest.Get("http://" + camURI + camCommand.Value))
                {
                    yield return uwr.SendWebRequest();

                    if (uwr.isNetworkError || uwr.isHttpError)
                    {
                        Debug.Log("RemoteCam Error: " + uwr.error);
                        waitingRemoteCamAnswer = false;
                        camQueryBuffer.Clear();
                        yield break;
                    }
                    else
                    {
                        Debug.Log("RemoteCam Response: " + uwr.downloadHandler.text);
                        waitingRemoteCamAnswer = false;
                    }
                }
            }        
        }
        else
            Debug.Log("RemoteCam: IP address is empty.");

        camQueryBuffer.Clear();
    }
}
