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

public class TowerEditor : EditorWindow {

    CastleAnimator rootObject;

    GameObject[] SelectedObjects;
    Vector2 scrollPos;

    public enum OPTIONS { SelectBases, SelectTowers, SelectBridges }
    public OPTIONS mode;

    public CastleAnimator.ScaleDirection scaleDirection;
    public CastleAnimator.ScaleDirection prevScaleDirection;

    public int index = 0;
    public int prevIndex = -1;


    public GameObject SelectedBase;

    // Add menu named "My Window" to the Window menu
    [MenuItem("Paco/TowerEditor")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        TowerEditor window = (TowerEditor)EditorWindow.GetWindow(typeof(TowerEditor));
        window.Show();
    }

    #region GUI Functions
    void OnGUI()
    {
        GUILayout.Label("Root Object", EditorStyles.boldLabel);
        rootObject = (CastleAnimator)EditorGUILayout.ObjectField(rootObject, typeof(CastleAnimator), true);

        GUILayout.Box("", new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(1) });
        mode = (OPTIONS)EditorGUILayout.EnumPopup(mode);

        //if (GUILayout.Button("FIX SCALE POINTS")) FixScalePoints();
        
        GUILayout.Box("", new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(1) });

        switch (mode) {
            case OPTIONS.SelectBases:
                BaseGUI();
                break;
            case OPTIONS.SelectTowers:
                TowerGUI();
                break;
            case OPTIONS.SelectBridges:
                BridgeGUI();
                break;
        }

    }

    public void OnInspectorUpdate()
    {
        // This will only get called 10 times per second.
        Repaint();
    }

    public void BaseGUI()
    {

        scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false, GUILayout.Height(position.height - 100));

        foreach (Transform selection in Selection.transforms) {
            GUILayout.Label(selection.name);
        }

        EditorGUILayout.EndScrollView();

        GUI.enabled = (rootObject != null);
        if (GUILayout.Button("Add Bases")) {
            AddBase(Selection.transforms);
        }
        GUI.enabled = true;
    }

    public void TowerGUI()
    {
        scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false, GUILayout.Height(position.height - 100));

        foreach (Transform selection in Selection.transforms) {
            GUILayout.Label(selection.name);
        }

        EditorGUILayout.EndScrollView();


        EditorGUILayout.BeginHorizontal();

        GUI.enabled = (rootObject != null);

        string[] options = { "Select CastleObject" };
        if (rootObject != null)
            options = rootObject.GetBaseNames();

        index = EditorGUILayout.Popup(index, options);

        if (GUILayout.Button("Add Tower")) {
            AddTower(index, Selection.transforms);
        }
        GUI.enabled = true;

        EditorGUILayout.EndHorizontal();
        if (index != prevIndex) {
            ChangeTower(index);
            prevIndex = index;
        }
    }

    public void BridgeGUI()
    {
        scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false, GUILayout.Height(position.height - 118));

        foreach (Transform selection in Selection.transforms) {

            if (rootObject != null && !rootObject.Bases[index].tower.TowerPieces.Contains(selection.gameObject)) {

                GUILayout.BeginHorizontal();
                GUILayout.Label(selection.name);
                GUILayout.Label((CheckInUse(selection))?"(In Use)": "", EditorStyles.boldLabel);
                GUILayout.EndHorizontal();

            } else if (rootObject == null) {
                GUILayout.Label(selection.name);
            }
        }

        EditorGUILayout.EndScrollView();

        GUI.enabled = (rootObject != null);

        scaleDirection = (CastleAnimator.ScaleDirection)EditorGUILayout.EnumPopup(scaleDirection);

        EditorGUILayout.BeginHorizontal();
        string[] options = { "Select CastleObject" };
        if (rootObject != null)
            options = rootObject.GetBaseNames();

        index = EditorGUILayout.Popup(index, options);


        if (GUILayout.Button("Add Bridge")) {
            AddBridge(index,scaleDirection , Selection.transforms);
        }

        GUI.enabled = true;

        EditorGUILayout.EndHorizontal();
        if (index != prevIndex || scaleDirection != prevScaleDirection) {
            ChangeBridge(index, scaleDirection);
            prevIndex = index;
            prevScaleDirection = scaleDirection;
        }


    }

    #endregion GUI Functions

    private void AddBase(Transform[] newBases)
    {
        foreach (Transform selectedBase in newBases) {
            rootObject.AddBase(selectedBase);
        }
    }

    private void FixScalePoints()
    {
        if (rootObject == null)
            return;

        for (int i = 0; i < rootObject.Bases.Count; i++) {
            CastleAnimator.Base tempBase = rootObject.Bases[i];
            tempBase.tower.ScalePoints = new GameObject[4];

            for (int j = 0; j < 4; j++) {
                tempBase.tower.ScalePoints[j] = new GameObject();
                tempBase.tower.ScalePoints[j].transform.parent = tempBase.gameObject.transform;
                tempBase.tower.ScalePoints[j].transform.localPosition = Vector3.zero;
                tempBase.tower.ScalePoints[j].name = "ScalePoint" + rootObject.Bases.Count + ", " + i;
            }

            rootObject.Bases[i] = tempBase;
        }

    }

    private void AddTower(int index, Transform[] newTower)
    {
        rootObject.AddTower(rootObject.Bases[index], newTower);
    }

    private void AddBridge(int index, CastleAnimator.ScaleDirection dir, Transform[] newBridge)
    {
        rootObject.AddBridge(rootObject.Bases[index],dir , newBridge);
    }


    private void ChangeTower(int index)
    {
        if (rootObject == null)
            return;

        CastleAnimator.Base curBase = rootObject.Bases[index];

        List<GameObject> Tower = new List<GameObject>();
        Tower.Add(curBase.gameObject);
        foreach (GameObject towerPiece in curBase.tower.TowerPieces)
            Tower.Add(towerPiece);

        Selection.objects = Tower.ToArray();
    }

    private void ChangeBridge(int index, CastleAnimator.ScaleDirection direction)
    {
        if (rootObject == null)
            return;

        CastleAnimator.Base curBase = rootObject.Bases[index];

        List<GameObject> Bridge = new List<GameObject>();
        //Bridge.Add(curBase.gameObject);
        foreach (GameObject towerPiece in curBase.tower.TowerPieces)
            Bridge.Add(towerPiece);

        foreach (Transform child in curBase.tower.ScalePoints[(int)direction].transform)
            Bridge.Add(child.gameObject);

        Selection.objects = Bridge.ToArray();
    }

    private bool CheckInUse(Transform selection)
    {
        if (rootObject == null)
            return false;

        foreach (CastleAnimator.Base Base in rootObject.Bases) {

            foreach(GameObject ScalePoint in Base.tower.ScalePoints) {
                if (selection.IsChildOf(ScalePoint.transform))
                    return true;

            }

        }
        return false;

    }

}