using UnityEngine;
using System.Collections.Generic;
using Assets;
using System.Linq;
using System;
using System.Text;


public class InputScript : MonoBehaviour
    {

        private InputProcess _readProcess;

        public List<ControllMessage> msg_list;
        public string msg_strs;
        public ControllMessage LastMessage;
        public ControllMessage LastValidSquare { get; set; }

        public double ulx;
        public double uly;

        public double urx;
        public double ury;

        public double dlx;
        public double dly;

        public double drx;
        public double dry;


    public string LastLine { get; set; }

        // Use this for initialization
    void Start () {
            _readProcess = new InputProcess() { AppPath = Application.dataPath + "/StreamingAssets/dist/webcam_motion.exe" };
            _readProcess.Start();
        }
	
        // Update is called once per frame
        void Update ()
        {





        LastMessage = _readProcess.LastMessage;
        LastLine = _readProcess.LastLine;
        var messages = _readProcess.GetCurrentMessages();
        msg_list = new List<ControllMessage>();





            if (messages != null)
            {
                foreach (var m in messages)
                    msg_list.Add(m);
            }
            msg_strs = msg_list.LastOrDefault() != null ? msg_list.LastOrDefault().ToString() : null ;
         LastValidSquare = _readProcess.LastValidSquare;
        if(LastValidSquare != null)
        {
            ulx = LastValidSquare.Square.UL.X;
            uly = LastValidSquare.Square.UL.Y;

            urx = LastValidSquare.Square.UR.X;
            ury = LastValidSquare.Square.UR.Y;

            dlx = LastValidSquare.Square.DL.X;
            dly = LastValidSquare.Square.DL.Y;

            drx = LastValidSquare.Square.DR.X;
            dry = LastValidSquare.Square.DR.Y;
        }


        //Debug.Log("square_msg: " + LastValidSquare);

    }

        void OnDestroy()
        {
            Debug.Log("OnDestroy");
            if(_readProcess != null)
                _readProcess.Dispose();
            _readProcess = null;

        }


    }
