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

public class UIManager : MonoBehaviour
{
    List<GameObject> points;
    List<GameObject> lines;
    List<GameObject> constraints;

    public GameObject constraintDropdown;
    public GameObject constraintInput;

    public Transform pointsParent;
    public Transform linesParent;
    public Transform constraintParent;

    public GameObject button;
    public GameObject line;
    public GameObject constraint;

    string chosen1 = "";
    string chosen2 = "";

    public CanvasManager3D CM;

    enum UIState
    {
        none,
        createLine,
        linelength,
        lineEqual,
        lineNormal
    }

    UIState state = UIState.none;
    
    // Start is called before the first frame update
    void Start()
    {
        points = new List<GameObject>();
        lines = new List<GameObject>();
        constraints = new List<GameObject>();
    }

    // Update is called once per frame
    void Update()
    {
        switch (state)
        {
            case UIState.none:
                break;
            case UIState.createLine:
                if(!chosen1.Equals("") && !chosen2.Equals(""))
                {
                    createLine();
                }
                break;
            case UIState.linelength:
                if (!chosen1.Equals("") && !constraintInput.GetComponent<InputField>().text.Equals(""))
                {
                    createConstraint();
                }
                break;
            case UIState.lineEqual:
                if (!chosen1.Equals("") && !chosen2.Equals(""))
                {
                    createConstraint();
                }
                break;
            case UIState.lineNormal:
            if (!chosen1.Equals("") && !chosen2.Equals(""))
                {
                    createConstraint();
                }
                break;
        }
    }

    public void createPoint()
    {
        string n = CM.createPoint();
        GameObject tmp = Instantiate(button);
        tmp.transform.SetParent(pointsParent);
        points.Add(tmp);
        tmp.transform.localPosition = new Vector3(100 + 70 * (points.Count - 1), 0, 0);
        tmp.GetComponentInChildren<Text>().text = n;
        tmp.GetComponent<Button>().onClick.AddListener(delegate(){ chooseObject(n); });
    }

    public void createLineButton()
    {
        state = UIState.createLine;
    }

    void createLine()
    {
        string n = CM.createLine(chosen1, chosen2);
        chosen1 = "";
        chosen2 = "";
        GameObject tmp = Instantiate(line);
        tmp.transform.SetParent(linesParent);
        lines.Add(tmp);
        tmp.transform.localPosition = new Vector3(100 + 70 * (lines.Count - 1), 0, 0);
        tmp.GetComponentInChildren<Text>().text = n;
        tmp.GetComponent<Button>().onClick.AddListener(delegate () { chooseObject(n); });
        state = UIState.none;
    }

    public void chooseObject(string obj)
    {
        if(state != UIState.none)
        {
            if (chosen1.Equals(""))
                chosen1 = obj;
            if (!chosen1.Equals("") && !chosen1.Equals(obj) && chosen2.Equals(""))
                chosen2 = obj;
            Debug.Log(obj);
        }
        
    }

    public void LineButton()
    {
        if(state == UIState.none)
        {

        }
    }

    public void createConstraintButton()
    {
        constraintDropdown.SetActive(true);
    }

    public void constraintDropdownChange()
    {
        string text = constraintDropdown.GetComponent<Dropdown>().captionText.text;
        ConstraintType type = (ConstraintType)System.Enum.Parse(typeof(ConstraintType), text);
        switch (type)
        {
            case ConstraintType.LineLength:
                state = UIState.linelength;
                constraintInput.SetActive(true);
                break;
            case ConstraintType.LineEqual:
                state = UIState.lineEqual;
                break;
            case ConstraintType.LineNormal:
                state = UIState.lineNormal;
                break;
        }
    }

    void createConstraint()
    {
        Line l1 = null;
        Line l2 = null;

        float length;

        string n = "";
        switch (state)
        {
            case UIState.linelength:
                l1 = CM.GetLine(chosen1);
                length = float.Parse(constraintInput.GetComponent<InputField>().text);
                n = CM.createConstraint(l1, length, ConstraintType.LineLength);
                constraintInput.SetActive(false);
                break;
            case UIState.lineEqual:
                l1 = CM.GetLine(chosen1);
                l2 = CM.GetLine(chosen2);
                n = CM.createConstraint(l1, l2, ConstraintType.LineEqual);
                break;
            case UIState.lineNormal:
                l1 = CM.GetLine(chosen1);
                l2 = CM.GetLine(chosen2);
                n = CM.createConstraint(l1, l2, ConstraintType.LineNormal);
                break;

        }
        chosen1 = "";
        chosen2 = "";
        GameObject tmp = Instantiate(line);
        tmp.transform.SetParent(constraintParent);
        constraints.Add(tmp);
        tmp.transform.localPosition = new Vector3(100 + 70 * (constraints.Count - 1), 0, 0);
        tmp.GetComponentInChildren<Text>().text = n;
        state = UIState.none;
        constraintDropdown.GetComponent<Dropdown>().captionText.text = "None";
        constraintDropdown.SetActive(false);
    }
}