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

public class TowerController : MonoBehaviour {
    public static readonly Color OriginColor = new Color(0, 255, 0);
    public static readonly Color TargetColor = new Color(255, 0, 0);
    public static readonly Color InactiveColor = new Color(255, 255, 0);
    public static readonly Color ActiveColor = new Color(0, 255, 0);

    public bool Active;
    public List<TowerController> Children;
    public TowerController Parent;
    public TowerController ActiveChild;
    public string DebugInfo;

    public Light Strobe;

    public LevelController Level;

	public Vector3 Position { get
        {
            return this.transform.position;
        }
    }

	void OnMouseOver() {
		if(Input.GetMouseButtonDown(0)) {
			if (Active == true) {
				Active = false;
			}
			else {
				Active = true;
			}
		}
	}

    // Use this for initialization
    void Start () {
        ActivateTower();
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    public void SetStrobeColor(Color color)
    {
        Strobe.color = color;
    }

    public void ActivateTower()
    {
        Parent.DeactivateChildren(this);
        Active = true;
        SetStrobeColor(ActiveColor);
        if (Children.Count == 1)
        {
            ActiveChild = Children[0];
            ActiveChild.ActivateTower();
        }
    }
    public void DeactivateTower()
    {
        if (Parent != null)
        {
            Active = false;
            DeactivateChildren();
            ActiveChild = null;
            SetStrobeColor(InactiveColor);
        }
    }
    void DeactivateChildren(TowerController eventSource=null)
    {
        foreach (var child in Children)
        {
            if(!ReferenceEquals(child, eventSource))
            {
                child.DeactivateTower();
            }
        }
    }
    public void AddChild(TowerController child)
    {
        Children.Add(child);
        child.ActivateTower();
    }
}
