//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
{
    // States
    public enum State {Explore, Fuit, Dort, Mange, Bois};

    public class Tiger : Animal
    {
        // Sensors
        public bool Faim = false;
        public double lastEat = 0;
        public double angryEvery = 30;
        public double fullLunch = 10;

        public bool Soif = false;
        public double lastDrink = 0;
        public double drinkEvery = 20;
        public double fullDrink = 5;

        public bool ProxNourriture = false;
        public Vector3 Nourriture;
        public bool ProxEau = false;
        public Vector3 Eau;

        public bool Sommeil = false;
        public double lastSleep = 0;
        public double sleepEvery = 50;
        public double fullSleep = 20;


        public bool Bruit = false;

        public State currentState = State.Explore;
        public double stateTime = 0;
        public bool stateJustChanged = false;

        public Random rand;

        public Tiger()
        {
            speed = 1.0f;
            selectString = "Ceci est un tigre!! Grrrr";
            rand = new Random();
        }

        public override void UpdateIA(float elapsedTime, double absTime)
        {
            //UpdateSensors( elapsedTime, absTime);
            //if (IAKnowledgeChanged)
            //{
            //    base.UpdateIA(elapsedTime, absTime);
            //    IAKnowledgeChanged = false;
            //}
            //PerformStateActions( elapsedTime,  absTime);
        }

        public void UpdateSensors(float elapsedTime, double absTime)
        {
            lastDrink += elapsedTime;

            if (lastDrink > drinkEvery)
            {
                Soif = true;
                IAKnowledgeChanged = true;
            }

            lastEat += elapsedTime;

            if (lastEat > angryEvery)
            {
                Faim = true;
                IAKnowledgeChanged = true;
            }

            lastSleep += elapsedTime;

            if (lastSleep > sleepEvery)
            {
                Sommeil = true;
                IAKnowledgeChanged = true;
            }
        }

        public void PerformStateActions(float elapsedTime, double absTime)
        {
            switch (currentState)
            {
                case State.Bois:
                    Bois(elapsedTime, absTime);
                    break;
                case State.Dort:
                    Dort(elapsedTime, absTime);
                    break;
                case State.Explore:
                    Explore(elapsedTime, absTime);
                    break;
                case State.Fuit:
                    Fuit(elapsedTime, absTime);
                    break;
                case State.Mange:
                    Mange(elapsedTime, absTime);
                    break;
                default:
                    break;
            }
        }

        public void Bois(float elapsedTime, double absTime)
        {
            if (stateJustChanged)
            {
                Moving move = new Moving(this);
                move.destination = new WorldLocation(Eau);
                CurrentAction = move;
                stateJustChanged = false;
            }
            else
            {
                stateTime += elapsedTime;
            }
            if (stateTime > fullDrink)
            {
                Soif = false;
                IAKnowledgeChanged = true;
                currentState = State.Explore;
                lastDrink = 0;
            }
        }

        public void Dort(float elapsedTime, double absTime)
        {
            if (stateJustChanged)
            {
                CurrentAction = new Action();
                stateJustChanged = false;
            }
            else
            {
                stateTime += elapsedTime;
            }
            if (stateTime > fullSleep)
            {
                Sommeil = false;
                IAKnowledgeChanged = true;
                currentState = State.Explore;
                lastSleep = 0;
            }
        }

        public void Explore(float elapsedTime, double absTime)
        {
            if (stateJustChanged)
            {
                Moving move = new Moving(this);
                move.destination = new WorldLocation(sceneObj.Position + new Vector3(rand.Next(5, 20), 0.0f, rand.Next(5, 20)));
                CurrentAction = move;
                stateJustChanged = false;
            }
            else
            {
                stateTime += elapsedTime;
            }
            if (stateTime > 30000)
            {
                Moving move = new Moving(this);
                move.destination = new WorldLocation(sceneObj.Position + new Vector3(rand.Next(5, 20), 0.0f, rand.Next(5, 20)));
                stateTime = 0;
            }
        }

        public void Fuit(float elapsedTime, double absTime)
        {
            if (stateJustChanged)
            {
                Moving move = new Moving(this);
                move.destination = new WorldLocation(sceneObj.Position + new Vector3(0.0f, 0.0f, 100.0f));
                CurrentAction = move;
                stateJustChanged = false;
            }
            else
            {
                stateTime += elapsedTime;
            }
        }

        public void Mange(float elapsedTime, double absTime)
        {
            if (stateJustChanged)
            {
                Moving move = new Moving(this);
                move.destination = new WorldLocation(Nourriture);
                CurrentAction = move;
                stateJustChanged = false;
            }
            else
            {
                stateTime += elapsedTime;
            }
            if (stateTime > fullLunch)
            {
                Faim = false;
                IAKnowledgeChanged = true;
                lastEat = 0;
                currentState = State.Explore;
            }
        }
    }
}
