//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.Diagnostics;
using System.Collections.Generic;
using System.Text;
using Smile;

namespace Game
{
    public class TigerChannel : BayesianChannel
    {
        Network network;

        public TigerChannel()
        {
            network = new Network();
            network.ReadFile("Herbivore.xdsl");
        }

        public override void Run(WorldEntity controlledEntity)
        {
            Tiger tiger = controlledEntity as Tiger;
            if (tiger == null)
            {
                throw new ArgumentException("TigerChannel can only be associated with a Tiger entity!");
            }
            else
            {
                Run(tiger);
            }
        }

        public void Run(Tiger tiger)
        {
            network.ClearAllEvidence();
            network.ClearAllTargets();
            network.ClearControlledValue("Et_1");

            LoadInputs(tiger);
            network.UpdateBeliefs();
            ProcessOutputs(tiger);
        }

        public void LoadInputs(Tiger tiger)
        {
            // Setting current state.
            switch (tiger.currentState)
            {
                case State.Explore:
                    network.SetEvidence("Et", "Explore");
                    Debug.WriteLine("Et = Explore");
                    break;
                case State.Bois:
                    network.SetEvidence("Et", "Bois");
                    Debug.WriteLine("Et = Bois");
                    break;
                case State.Dort:
                    network.SetEvidence("Et", "Dort");
                    Debug.WriteLine("Et = Dort");
                    break;
                case State.Fuit:
                    network.SetEvidence("Et", "Fuit");
                    Debug.WriteLine("Et = Fuit");
                    break;
                case State.Mange:
                    network.SetEvidence("Et", "Mange");
                    Debug.WriteLine("Et = Mange");
                    break;
                default:
                    break;
            }

            // Sensors
            if (tiger.Faim)
            {
                network.SetEvidence("Faim", "Oui");
                Debug.WriteLine("Faim");
            }
            else
            {
                network.SetEvidence("Faim", "Non");
                Debug.WriteLine("Pas Faim");
            }
            if (tiger.Soif)
            {
                network.SetEvidence("Soif", "Oui");
                Debug.WriteLine("Soif");
            }
            else
            {
                network.SetEvidence("Soif", "Non");
                Debug.WriteLine("Pas Soif");
            }
            if (tiger.ProxNourriture)
            {
                network.SetEvidence("ProxNourriture", "Oui");
                Debug.WriteLine("ProxNourriture");
            }
            else
            {
                network.SetEvidence("ProxNourriture", "Non");
                Debug.WriteLine("Pas ProxNourriture");
            }
            if (tiger.ProxEau)
            {
                network.SetEvidence("ProxEau", "Oui");
                Debug.WriteLine("ProxEau");
            }
            else
            {
                network.SetEvidence("ProxEau", "Non");
                Debug.WriteLine("Pas ProxEau");
            }
            if (tiger.Sommeil)
            {
                network.SetEvidence("Sommeil", "Oui");
                Debug.WriteLine("Sommeil");
            }
            else
            {
                network.SetEvidence("Sommeil", "Non");
                Debug.WriteLine("Pas Sommeil");
            }
            if (tiger.Bruit)
            {
                network.SetEvidence("Bruit", "Oui");
                Debug.WriteLine("Bruit");
            }
            else
            {
                network.SetEvidence("Bruit", "Non");
                Debug.WriteLine("Pas Bruit");
            }
        }

        public void ProcessOutputs(Tiger tiger)
        {
            double[] values = network.GetNodeValue("Et_1");

            int indexOfHighestValue = 0;
            double highest = values[0];

            for (int i = 1; i < values.Length; i++)
            {
                Debug.WriteLine("Value[" + i + "]=" + values[i]);
                if (values[i] > highest)
                {
                    indexOfHighestValue = i;
                    highest = values[i];
                }
            }

            network.ClearAllEvidence();
            network.ClearAllTargets();

            switch (indexOfHighestValue)
            {
                case 0:
                    if (tiger.currentState != State.Bois)
                    {
                        tiger.currentState = State.Bois;
                        tiger.stateTime = 0;
                        tiger.stateJustChanged = true;
                    }
                    break;
                case 1:
                    if (tiger.currentState != State.Dort)
                    {
                        tiger.currentState = State.Dort;
                        tiger.stateTime = 0;
                        tiger.stateJustChanged = true;
                    }
                    break;
                case 2:
                    if (tiger.currentState != State.Explore)
                    {
                        tiger.currentState = State.Explore;
                        tiger.stateTime = 0;
                        tiger.stateJustChanged = true;
                    }
                    break;
                case 3:
                    if (tiger.currentState != State.Fuit)
                    {
                        tiger.currentState = State.Fuit;
                        tiger.stateTime = 0;
                        tiger.stateJustChanged = true;
                    }
                    break;
                case 4:
                    if (tiger.currentState != State.Mange)
                    {
                        tiger.currentState = State.Mange;
                        tiger.stateTime = 0;
                        tiger.stateJustChanged = true;
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
