//---------------------------------------------------------------------------------------------------------------------------------------------------
// <copyright file="Challenge3.cs" company="DarkWynter Studios">
//     Copyright (C)2007 DarkWynter Studios.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------------------------------------------------------------------------------------
// {Contact : darkwynter.com for licensing information
//---------------------------------------------------------------------------------------------------------------------------------------------------

namespace DarkWynter.App.ChallengeSet
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Xml;
    using DarkWynter.Engine.Globals;
    using DarkWynter.Engine.Utilities;
    using Microsoft.Xna.Framework;
    using DarkWynter.Engine.GameObjects;
    using DarkWynter.Engine.EventControl;
    using DarkWynter.Engine.Compiler;
    #endregion
    /// <summary>
    /// The third challenge the player must complete
    /// Located in level 3 - Hippocampus
    /// Recursive Depth First Traversal
    /// </summary>
    public class Challenge2 : ChallengeAbstract
    {
        string stringCheck1 = "node.returnLeft()";
        string stringCheck2 = "depthFirstSearch(node.returnLeft());";
        string stringCheck3 = "node.returnRight() != null";
        string stringCheck4 = "depthFirstSearch(node.returnLeft());";
        string outCheck = "I found the number 13 that you are looking for!";
        string secretCheck = "1234353267686219ABACA9DEDFD91";
        /// <summary>
        /// Constructor for the third challenge, calls base (challengeAbstract)
        /// </summary>
        /// <param name="problemNode">XML Node</param>
        public Challenge2(XmlNode problemNode)
            : base(problemNode)
        {
            this.id = 0;

            foreach (XmlNode instrNode in problemNode)
            {
                challengeDialogue.Add(instrNode.Attributes["text"].Value);
                
            }
        }

        /// <summary>
        /// Scaffolding code for the challenge
        /// </summary>
        /// <returns>The String holding the scaffolding code (challengeSource)</returns>
        public override string GetScaffolding()
        {
            challengeSource =
 @"using System;
using DarkWynter.Engine.Compiler;
    class Test
    {
        public int findMe = 324;
        public string result;

        public void TreeTraversal()
        {
            Tree myTree = new Tree();
            depthFirstSearch(myTree.root);
            if (result == null)
                result = ""I did not find the number "" + findMe.ToString() + "" that you are looking for!"";
            Console.WriteLine ( result);  
        }
        public string depthFirstSearch(Node node)
        {
            Thought.moveTo(node);
            if ([INSERT CODE HERE] && node.getData() == findMe)
            {
                result = "" I found the number "" + node.getData().ToString() + "" that you are looking for!"";
                return result;
            }

            else
            {
                [INSERT CODE HERE]
            }
            return result;
        }
    }

";
            return challengeSource;
        }
        /// <summary>
        /// Parses the student code for validity
        /// </summary>
        /// <param name="studentCode">Student's Code</param>
        /// <param name="studentOutput">Output from the compiler</param>
        /// <returns>bool</returns>
        public override bool ValidateStudentCode(string studentCode, string studentOutput)
        {
            // If base fails, return
            if (!base.ValidateStudentCode(studentCode, studentOutput))
            { return false; }
            //return true;
            //  Clean up the output first because forms are dumb shits
            studentOutput = studentOutput.Replace('\r', ' ');
            studentOutput = studentOutput.Replace('\n', ' ');
            studentOutput = studentOutput.Trim();

            // Check Input
            if (studentCode.Contains(stringCheck1))
            {
                if (studentCode.Contains(stringCheck2))
                {
                    if (studentCode.Contains(stringCheck3))
                    {
                        if (studentCode.Contains(stringCheck4))
                        {
                            if (studentOutput.Contains(outCheck))
                            {

                            // Check Output
                                if (Thought.secretCode.Equals(secretCheck))
                                {
                                    Statics_Engine.GameSettings.triesNumber++;
                                    Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Success Message] Student input and output correct [End Success Message]");
                                    App.CompilerControl.customErrorMessage = null;
                                    base.setExpPoints();
                                    return true;

                                }
                                else Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Error Message] Student did not have correct output. [End Error Message]");
                                App.CompilerControl.customErrorMessage = "Check your traversal and try again.";
                                Statics_Engine.GameSettings.triesNumber++;
                                Thought.secretCode = "";
                                return false;
                            }
                            else Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Error Message] Student did not correctly complete the second call. [End Error Message]");
                            App.CompilerControl.customErrorMessage = "Oh, look at that, the number you are looking for is not there.  Why don't you look for a different number instead?";
                            Statics_Engine.GameSettings.triesNumber++;
                            Thought.secretCode = "";
                            return false;
                        }
                        else Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Error Message] Student did not actually complete challenge. [End Error Message]");
                        App.CompilerControl.customErrorMessage = "Make sure you're following the instructions and try again.";
                        Statics_Engine.GameSettings.triesNumber++;
                        Thought.secretCode = "";
                        return false;
                    }
                    else Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Error Message] Student did not correctly set up the if-statement for the first call. [End Error Message]");
                    App.CompilerControl.customErrorMessage = "Make sure you've set up the if-statement for the first recursive call correctly.  You need to check if a right-child exists for the current node.";
                    Statics_Engine.GameSettings.triesNumber++;
                    Thought.secretCode = "";
                    return false;
                }
                else Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Error Message] Student did not include an aspect of the second recursive call [End Error Message]");
                App.CompilerControl.customErrorMessage = "Make sure you are correctly calling depthFirstSearch two times.  You need to call depthFirstSearch within each if-statement for left and right children respectively.";
                Statics_Engine.GameSettings.triesNumber++;
                Thought.secretCode = "";
                return false;
            }
            else
                Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Error Message] Student did not include an aspect of the second recursive call [End Error Message]");
            App.CompilerControl.customErrorMessage = "Make sure that you have correctly completed the if-statements for both calls.  The second if-statement should check if a left-child exists for the current node.";
            Statics_Engine.GameSettings.triesNumber++;
            Thought.secretCode = "";
            return false;
        }
        /// <summary>
        /// Run the set visualization
        /// </summary>
        /// <returns>bool</returns>
        public override bool RunVizualization()
        {
            // Go Fullscreen so they don't miss the viz...
            //   DarkWynter.Shell.UCAD.UCAD_Left_SplitContainer.Panel1Collapsed = true;
            //    DarkWynter.Shell.UCAD.UCAD_Right_SplitContainer.Panel2Collapsed = true;
            //    DarkWynter.Shell.UCAD.UCAD_Bottom_SplitContainer.Panel2Collapsed = true;

            Statics_Engine.GameSettings.isProblemFinished = true;

            GameEventHandler.CurrentGameConditions.lastNodeVisited = 1;
            GameEventHandler.CurrentGameConditions.triggerSanity = -1;

            // DEBUG HACK : Increment AIEvent Index
            GameEventHandler.CurrentGameConditions._trigger_ID = 0;
            return true;

        }

    }



}