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

public class TowerController : ObjectBaseController, IFoWRevealer
{
    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;

    [SerializeField]
    protected float _viewRadius;
    public float ViewRadius
    {
        get
        {
            return _viewRadius;
        }
    }

    public bool ViewActive
    {
        get
        {
            return Active;
        }
    }

    public override void initialize(LevelController level) {
		base.initialize (level);

		var parent = Level.GetNearestTower(Position);
        Level.Towers.Add(this);
		Level.Objects.Add (this);
		parent.AddChild(this);

		//Check if within range of Target
		var target = Level.Target.GetComponentInChildren<TowerController>();
		Debug.Log (target);
		Debug.Log (Level.Target);
		var radius = ViewRadius;
		var tDistance = (target.Position - Position).sqrMagnitude;
		Debug.Log (tDistance < Mathf.Pow(radius * 2, 2));
		if (tDistance < Mathf.Pow(radius, 2)) { //Just want their light to touch
			AddChild (target);
			Level.Towers.Add (target);
		}
	}

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

    // Use this for initialization
    void Start () {
        FoWController.Instance.RegisterRevealer(this);
	}
	
	// Update is called once per frame
	void Update () {
		
	}

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

    public virtual void ActivateTower()
    {
        if (Parent != null && !Active)
        {
            Parent.DeactivateChildren(this);
            Active = true;
            Parent.ActiveChild = this;
            SetStrobeColor(ActiveColor);
            if (Children.Count == 1)
            {
                ActiveChild = Children[0];
                ActiveChild.ActivateTower();
            }
            Parent.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.Parent = this;
        child.ActivateTower();
    }
}
