//BSD License  
//DXGfx® - http://www.eteractions.com
//Copyright (c) 2005
//by Guillaume Randon
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software
//and associated documentation files (the "Software"), to deal in the Software without restriction,
//including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
//and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
//subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all copies or substantial
//portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
//AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
//DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX;

namespace Game
{
    public delegate void ActionCompletedHandler(Action thisisfinished);

    public class Action
    {
        protected IWorldEntity actor;

        public IWorldEntity complement = null;

        public bool iamdone;

        public event ActionCompletedHandler completed;

        public Action()
        {
            iamdone = false;
        }

        public Action(IWorldEntity theActor) : base()
        {
            SetActor(theActor);
        }

        public virtual void SetActor(IWorldEntity theActor)
        {
            actor = theActor;
        }

        public virtual bool CheckFeasibility(WorldEntity selectedEntity)
        {
            return false;
        }

        public virtual void Update(double appTime, float elapsedTime)
        {
            if (iamdone)
            {
                IAmDone();
            }
        }

        public virtual void IAmDone()
        {
            iamdone = true;
            if (completed != null)
            {
                completed(this);
            }
        }

        public virtual bool Similar(Action action)
        {
            return false;
        }

        public static bool Similar(Action ac1, Action ac2)
        {
            return ac1.Similar(ac2);
        }
    }

    public interface ICanMove : IWorldEntity
    {
        void Move(Vector3 dir, float elapsedTime);
    }

    public class Moving : Action
    {
        public WorldLocation destination;

        public Moving(ICanMove theActor)
            : base(theActor)
        {
        }

        public override void SetActor(IWorldEntity theActor)
        {
            ICanMove movingActor = theActor as ICanMove;

            if (movingActor == null)
            {
                throw new ArgumentException("The actor can not move (doesn't implement ICanMove) which is not accepted for this type of action!");
            }
            else
            {
                base.SetActor(theActor);
            }
        }

        public override bool CheckFeasibility(WorldEntity selectedEntity)
        {
            return true;
        }

        public override void Update(double appTime, float elapsedTime)
        {
            base.Update(appTime, elapsedTime);

            Vector3 dir = Vector3.Subtract(destination.SceneObject.Position, actor.SceneObject.Position);

            if (dir.Length() > destination.radius)
            {
                ((ICanMove)actor).Move(Vector3.Normalize(dir), elapsedTime);
            }
            else
            {
                IAmDone();
            }
        }

        public override bool Similar(Action action)
        {
            bool ret = false;
            Moving mov = action as Moving;
            if (mov!=null)
            {
                if (destination.Distance(mov.destination) < 5.0f)
                    ret = true;
            }
            return ret;
        }
    }

    public class Sleeping : Action
    {
        public Sleeping(IWorldEntity theActor)
            : base(theActor)
        {
        }

        public override bool Similar(Action action)
        {
            // Any action should override this. This is needed to check if a completed action
            // was equivalent to the action embedded in a step that some needed to perform to finish a mission.
            return false;
        }
    }

    public class Eating : Action
    {
        public Eating(IWorldEntity theActor)
            : base(theActor)
        {
        }

        public override void Update(double appTime, float elapsedTime)
        {
            IAmDone();
        }
        public override bool Similar(Action action)
        {
            return (complement == action.complement);
        }
    }

    public class MovingAround : Action
    {
        public MovingAround(IWorldEntity theActor)
            : base(theActor)
        {
        }

        public override bool Similar(Action action)
        {
            // Any action should override this. This is needed to check if a completed action
            // was equivalent to the action embedded in a step that some needed to perform to finish a mission.
            return false;
        }
    }

    public class LookingForFood : Action
    {
        public LookingForFood(IWorldEntity theActor)
            : base(theActor)
        {
        }

        public override bool Similar(Action action)
        {
            // Any action should override this. This is needed to check if a completed action
            // was equivalent to the action embedded in a step that some needed to perform to finish a mission.
            return false;
        }
    }

    public class Drinking : Action
    {
        public Drinking(IWorldEntity theActor)
            : base(theActor)
        {
        }

        public override bool Similar(Action action)
        {
            // Any action should override this. This is needed to check if a completed action
            // was equivalent to the action embedded in a step that some needed to perform to finish a mission.
            return false;
        }
    }

    public class Fleeing : Action
    {
        public Fleeing(IWorldEntity theActor)
            : base(theActor)
        {
        }

        public override bool Similar(Action action)
        {
            // Any action should override this. This is needed to check if a completed action
            // was equivalent to the action embedded in a step that some needed to perform to finish a mission.
            return false;
        }
    }

    public class Talking : Action
    {
        public Talking(IWorldEntity theActor)
            : base(theActor)
        {
        }

        public override bool Similar(Action action)
        {
            // Any action should override this. This is needed to check if a completed action
            // was equivalent to the action embedded in a step that some needed to perform to finish a mission.
            return false;
        }
    }
}
