using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using Gamelogic.Grids2;

public class GameCell : TileCell
{
    public bool HighLight = false;
    public bool IsAimable = false;
    
    private TurnActor actor;
    public TurnActor Actor { get { return actor; } }
    public string DEBUG;

    private List<Transform> contents;
    private List<Transform> Contents { get { if (contents == null) contents = new List<Transform>(); return contents; } }

    public bool HasActor()
    {
        return actor != null;
    }

    public void RemoveActor()
    {
        actor = null;
    }

    public void SetActor(TurnActor a)
    {
        actor = a;
        Touch();
    }

    public void AddContents(Transform c)
    {
        Contents.Add(c);
        if(HasActor())
            c.SendMessage("Touch", actor, SendMessageOptions.DontRequireReceiver);
    }

    public void RemoveContents(Transform c)
    {
        if (Contents == null)
            return;

        Contents.Remove(c);
    }

    public IEnumerable<Transform> GetContents()
    {
        return Contents;
    }

    public bool HasContents()
    {
        return contents != null;
    }

    public bool Contains(string contentName)
    {
        if (contents == null)
            return false;

        return (contents.Where(t => t.name == contentName || t.name == contentName+"(Clone)").Count() > 0);
    }

    private void Touch()  // how to call touch after an actor doesn't move, eg gas wants to do effect each turn
    {
        if (!HasContents())
            return;

        foreach(Transform t in Contents.ToArray())
        {
            if (t == null)
                Contents.Remove(t);
            else 
                t.SendMessage("Touch", actor.gameObject, SendMessageOptions.DontRequireReceiver);
        }
    }

    void Update()
    {
        DebugUpdate();
    }

    private void DebugUpdate()
    {
        DEBUG = ((actor == null) ? "Empty" : actor.name) +
            ((Contents == null) ? " - Empty" : " - " + Contents.Count);
    }

}
