// Update is called once per frame
    void Update()
    {
        if (newData)
        {
            FossFab.Command command = null;

            /*
             * foreach (byte b in bytes)
             * {
             *  Debug.Log(b);
             * }
             */

            command = FossFab.Command.Parser.ParseFrom(bytes);


            FossFab.Command.Types.CommandCode command_code = command.CommandCode;

            Debug.Log(command.CalculateSize());

            switch (command_code)
            {
            case FossFab.Command.Types.CommandCode.Reset:
                Reset();
                break;

            case FossFab.Command.Types.CommandCode.Move:
                this.printHead.GetComponent <MeshRenderer>().enabled = true;
                foreach (FossFab.HeadPosition pos in command.Positions)
                {
                    this.targets.Enqueue(new KeyValuePair <Vector3, bool>(FFVToVector3(pos.Pos), command.Extrude));
                }


                break;

            case FossFab.Command.Types.CommandCode.RenderMesh:
                this.printHead.GetComponent <MeshRenderer>().enabled = false;
                Mesh mesh = targetMesh.GetComponent <MeshFilter>().mesh;
                Debug.Log("Mesh is");
                Debug.Log(mesh);

                mesh.Clear();

                // make changes to the Mesh by creating arrays which contain the new values
                int num_triangles = command.Mesh.Triangles.Count;
                int num_vertices  = num_triangles * 3;

                List <Vector3> vertices = new List <Vector3>();

                mesh.vertices = new Vector3[num_vertices];
                for (int i = 0; i < num_triangles; ++i)
                {
                    FossFab.Vector3 vertex1 = command.Mesh.Triangles[i].Vertex1;
                    FossFab.Vector3 vertex2 = command.Mesh.Triangles[i].Vertex2;
                    FossFab.Vector3 vertex3 = command.Mesh.Triangles[i].Vertex3;


                    vertices.Add(FFVToVector3(vertex1));
                    vertices.Add(FFVToVector3(vertex2));
                    vertices.Add(FFVToVector3(vertex3));
                }



                mesh.triangles = Enumerable.Range(0, num_vertices).ToArray();
                mesh.vertices  = vertices.ToArray();


                /*
                 * mesh.vertices = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0) };
                 *
                 * mesh.triangles = new int[] { 0, 1, 2 };
                 */
                mesh.RecalculateNormals();



                break;

            case FossFab.Command.Types.CommandCode.RenderPath:
                this.printHead.GetComponent <MeshRenderer>().enabled = true;
                foreach (FossFab.HeadPosition pos in command.Positions)
                {
                    this.targets.Enqueue(new KeyValuePair <Vector3, bool>(FFVToVector3(pos.Pos), true));
                }
                break;

            case FossFab.Command.Types.CommandCode.RenderSlices:

                this.printHead.GetComponent <MeshRenderer>().enabled = false;
                Reset();
                //TODO: this is going to move the printhead.  Should fix this

                Debug.Log("SLICE SLICE BABY");
                foreach (FossFab.Slice slice in command.Slices)
                {
                    foreach (FossFab.Vector3 pos in slice.Positions)
                    {
                        this.targets.Enqueue(new KeyValuePair <Vector3, bool>(FFVToVector3(pos), true));
                    }
                    this.targets.Enqueue(new KeyValuePair <Vector3, bool>(FFVToVector3(slice.Positions[0]), true));
                }
                break;


            default:
                break;
            }



            newData = false;
        }

        //TODO: Need to schedule and animate in here.
        if (targets.Count > 0)
        {
            DrawHead(targets.Dequeue());
        }
    }
    // Update is called once per frame
    void Update()
    {
        Vector3 position = new Vector3(Mathf.Cos(Time.time), Mathf.Sin(Time.time), 0.0f);

        if (!clientConnected)
        {
            client = new TcpClient();
            client.Connect("127.0.0.1", 11000);
            clientConnected = true;
            s = client.Client;
        }



        if (clientConnected)
        {
            //mode 0:


            ++time;

            FossFab.Command command = new FossFab.Command();

            if (false)
            {
                command.CommandCode = FossFab.Command.Types.CommandCode.RenderSlices;


                FossFab.Slice slice = new FossFab.Slice();



                for (int i = 0; i < 20; ++i)
                {
                    FossFab.Vector3 pos = new FossFab.Vector3();
                    pos.X = Mathf.Cos((float)i / 5.0f) * 0.1f;
                    pos.Y = 0.1f;
                    pos.Z = Mathf.Sin((float)i / 5.0f) * 0.1f;



                    slice.Positions.Add(pos);
                }

                command.Slices.Add(slice);

                Debug.Log(command.CalculateSize());
            }
            if (false)
            {
                command.CommandCode = FossFab.Command.Types.CommandCode.RenderPath;
                FossFab.HeadPosition headPosition = new FossFab.HeadPosition();
                headPosition.Pos = new FossFab.Vector3();

                if (time % 100 == 0)
                {
                    for (int i = 0; i < 10000; ++i)
                    {
                        headPosition.Pos.X = position.x;
                        headPosition.Pos.Y = position.y * i / 1000.0f;
                        headPosition.Pos.Z = position.z;

                        command.Positions.Add(headPosition);
                    }

                    command.Extrude = true;
                }
            }
            if (true)
            {
                if (time % 200 == 0)
                {
                    command.CommandCode = FossFab.Command.Types.CommandCode.Move;

                    for (int i = 0; i < 10; ++i)
                    {
                        FossFab.HeadPosition headPosition = new FossFab.HeadPosition();
                        headPosition.Pos   = new FossFab.Vector3();
                        headPosition.Pos.X = i;
                        headPosition.Pos.Y = i;
                        headPosition.Pos.Z = i;
                        command.Positions.Add(headPosition);
                    }
                    command.Extrude = false;
                }
                else
                {
                    for (int i = 0; i < 10; ++i)
                    {
                        FossFab.HeadPosition headPosition = new FossFab.HeadPosition();
                        headPosition.Pos   = new FossFab.Vector3();
                        headPosition.Pos.X = i;
                        headPosition.Pos.Y = i;
                        headPosition.Pos.Z = i;
                        command.Positions.Add(headPosition);
                    }
                    command.Extrude = true;
                }
            }

            if (false)
            {
                command.CommandCode = FossFab.Command.Types.CommandCode.Move;
                FossFab.HeadPosition headPosition = new FossFab.HeadPosition();
                headPosition.Pos = new FossFab.Vector3();


                headPosition.Pos.X = position.x;
                headPosition.Pos.Y = position.y;
                headPosition.Pos.Z = position.z;

                command.Positions.Add(headPosition);

                command.Extrude = true;
            }
            if (false)
            {
                command.CommandCode = FossFab.Command.Types.CommandCode.RenderMesh;
                //make two triangles
                command.Mesh = new FossFab.Mesh();


                FossFab.Triangle triangle0 = new FossFab.Triangle();
                triangle0.Vertex1   = new FossFab.Vector3();
                triangle0.Vertex1.X = 0.0f;
                triangle0.Vertex1.Y = 0.0f;
                triangle0.Vertex1.Z = 0.0f;

                triangle0.Vertex2   = new FossFab.Vector3();
                triangle0.Vertex2.X = 1.0f;
                triangle0.Vertex2.Y = 0.0f;
                triangle0.Vertex2.Z = 0.0f;

                triangle0.Vertex3   = new FossFab.Vector3();
                triangle0.Vertex3.X = 0.0f;
                triangle0.Vertex3.Y = 2.0f;
                triangle0.Vertex3.Z = 0.0f;



                FossFab.Triangle triangle1 = new FossFab.Triangle();
                triangle1.Vertex1   = new FossFab.Vector3();
                triangle1.Vertex1.X = -1.0f;
                triangle1.Vertex1.Y = 2.0f;
                triangle1.Vertex1.Z = 3.0f;

                triangle1.Vertex2   = new FossFab.Vector3();
                triangle1.Vertex2.X = -10.0f;
                triangle1.Vertex2.Y = 2.0f;
                triangle1.Vertex2.Z = 3.0f;

                triangle1.Vertex3   = new FossFab.Vector3();
                triangle1.Vertex3.X = -10.0f;
                triangle1.Vertex3.Y = 25.0f;
                triangle1.Vertex3.Z = 35.0f;



                command.Mesh.Triangles.Add(triangle0);
                command.Mesh.Triangles.Add(triangle1);
            }

            if (time % 100 == 0)
            {
                //command.CommandCode = FossFab.Command.Types.CommandCode.Reset;



                byte[] msg = command.ToByteArray();

                int bytesSen1 = s.Send(msg);
            }
        }
    }