using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SelectTool : MonoBehaviour {


    public static string selected = "Cube";
    void OnSelect()
    {
        TextMesh instruct = GameObject.Find("Instructions").GetComponent<TextMesh>();
        if (this.tag.Equals("Cube"))
        {
            print("selected a cube!");
            SelectTool.selected = "Cube";
            instruct.text = "Click and Drag pen to draw a cube.";
        }
        else if (this.tag.Equals("Sphere"))
        {
            print("selected a Sphere!");
            SelectTool.selected = "Sphere";
            instruct.text = "Click and Drag pen to draw a sphere.";
        }
        else if (this.tag.Equals("Line"))
        {
            print("selected a Line!");
            SelectTool.selected = "Line";
            instruct.text = "Click and Drag pen to draw a line.";
        }
        else if (this.tag.Equals("DrawThinLines"))
        {
            print("selected thinLines!");
            SelectTool.selected = "DrawThinLines";
            instruct.text = "Click and Drag pen to draw doodles.";
        }
        else if (this.tag.Equals("Polylines"))
        {
            print("selected Polylines!");
            SelectTool.selected = "Polylines";
            instruct.text = "Click, then let go. Click again to draw straight line from last clicked point.";
        }
        else if (this.tag.Equals("ThickPolylines"))
        {
            print("selected ThickPolylines!");
            SelectTool.selected = "ThickPolylines";
            SimpleShapeManipulation.firstOne = true;
            instruct.text = "Click, then let go. Click again to draw straight line from last clicked point. \nHold button to preview.";
        }
        else if (this.tag.Equals("CubeFromSameCorner"))
        {
            print("selected CubeFromSameCorner!");
            SelectTool.selected = "CubeFromSameCorner";
            SimpleShapeManipulation.firstOne = true;
            instruct.text = "Click and Drag pen to draw a cube. \nThe next cube's starting corner will be the same as the last cube's finishing corner.";
        }
        else if (this.tag.Equals("Erase"))
        {
            print("selected Erase!");
            SelectTool.selected = "Erase";
            foreach (GameObject obj in SimpleShapeManipulation.Cubes) obj.tag = "";
            foreach (GameObject obj in SimpleShapeManipulation.Spheres) obj.tag = "";
            foreach (GameObject obj in SimpleShapeManipulation.thickLines) obj.tag = "";
            foreach (GameObject obj in SimpleShapeManipulation.thickPolylines) obj.tag = "";
            foreach (GameObject obj in SimpleShapeManipulation.thickPolylinesEnds) obj.tag = "";

            foreach (GameObject obj in SimpleShapeManipulation.Cubes) Destroy(obj);
            foreach (GameObject obj in SimpleShapeManipulation.Spheres) Destroy(obj);
            foreach (GameObject obj in SimpleShapeManipulation.thickLines) Destroy(obj);
            foreach (GameObject obj in SimpleShapeManipulation.thickPolylines) Destroy(obj);
            foreach (GameObject obj in SimpleShapeManipulation.thickPolylinesEnds) Destroy(obj);
            instruct.text = "Erases Everything";
        }
        else if (this.tag.Equals("Move"))
        {
            print("selected Move!");
            SelectTool.selected = "Move";
        }

    }
    

    // Update is called once per frame
    void Update()
    {
        if (this.tag.Equals(SelectTool.selected))
        {
            if (this.tag.Equals("DrawThinLines") || this.tag.Equals("Line") || this.tag.Equals("Polylines") ||
                this.tag.Equals("ThickPolylines") || this.tag.Equals("CubeFromSameCorner"))
                foreach (Transform child in transform) child.GetComponent<Renderer>().material.color = Color.green;
            else if (this.tag.Equals("Move"))
            {
                foreach (Transform child in transform) child.GetComponent<Renderer>().material.color = Color.green;
                foreach (GameObject obj in SimpleShapeManipulation.Cubes) obj.GetComponent<HapticProperties>().fixedObj = false;
                foreach (GameObject obj in SimpleShapeManipulation.Spheres) obj.GetComponent<HapticProperties>().fixedObj = false;
                foreach (GameObject obj in SimpleShapeManipulation.thickLines) obj.GetComponent<HapticProperties>().fixedObj = false;
                foreach (GameObject obj in SimpleShapeManipulation.thickPolylines) obj.GetComponent<HapticProperties>().fixedObj = false;
                foreach (GameObject obj in SimpleShapeManipulation.thickPolylinesEnds) obj.GetComponent<HapticProperties>().fixedObj = false;
            }
            else if (this.tag.Equals("Erase"))
            {
            }
            else GetComponent<Renderer>().material.color = Color.green;

        }
        else
        {
            if (this.tag.Equals("DrawThinLines") || this.tag.Equals("Line") || this.tag.Equals("Polylines") ||
                this.tag.Equals("ThickPolylines") || this.tag.Equals("CubeFromSameCorner") || this.tag.Equals("Move") || this.tag.Equals("Erase"))
            {
                GetComponent<Renderer>().material.color = Color.clear;
                foreach (Transform child in transform) child.GetComponent<Renderer>().material.color = Color.white;
            }
            else GetComponent<Renderer>().material.color = Color.white;
        }
    }
}


