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

namespace DarkWynter.Game.Menus
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Storage;
    using System.Threading;
    using System.Xml;
    #endregion
    using DarkWynter.Engine;
    using DarkWynter.Engine.Controllers;
    using DarkWynter.Engine.Globals;
    using DarkWynter.Engine.ObjectLib;
    using DarkWynter.Engine.Menus;

    
    /// <summary>
    /// The Game Setup screen.
    /// </summary>
    public class GameSetup : GameScreen
    {

        private Texture2D storyModeTexture;
        private Texture2D deathMatchTexture;
        private Texture2D survivalTexture;
        private Texture2D lastmanTexture;


        /// <summary>
        /// Menu that allows the user to select from multiple game types.
        /// </summary>
        public GameSetup(Enums_Engine.EngineState GameEngineState)
            : base(GameEngineState)
        {

            // Instantiate the GameMenu
            menu = new GameMenu("Game Setup", 100, 100);
            menu.SetDrawMenuTitle(false);

            menu.AddLabel("Game options:");
            menu.AddLabel("");
            
            menu.AddMenuOption("Game type:", "Deathmatch", "Story Mode", "Survival", "Last man standing");
            menu.AddLabel("");
            
            menu.AddValueInput("# Players:",
                Statics_Engine.GameSettings.MIN_NUMBER_OF_PLAYERS,
                Statics_Engine.GameSettings.MAX_NUMBER_OF_PLAYERS,
                Statics_Engine.GameSettings.DEFAULT_NUMBER_OF_PLAYERS,
                Statics_Engine.GameSettings.NUMBER_OF_PLAYERS_PER_TEAM
            );
            menu.AddLabel("");

            menu.AddMenuOption("Difficulty:", "Lamer", "Average Joe", "Uber Mental");
            menu.AddLabel("");
            
            menu.AddValueInput("Max kills:", 1, 10, 3, 1);
            menu.AddLabel("");
            
            menu.AddValueInput("Bots:", 0, 10, 1, 1);
            menu.AddLabel("");

            menu.AddValueInput("Bot intelligence:", 1, 10, 5, 1);
            menu.AddLabel("");

            storyModeTexture = Statics_Engine.SystemSettings.content.Load<Texture2D>("Content/_textures/DMatch");

        }

        /// <summary>
        /// Check for updates to Game Setup menu.
        /// </summary>
        /// <param name="objectLibrary">Current ObjectLibrary.</param>
        public override void Update(ObjectLibrary objectLibrary)
        {
            base.Update(objectLibrary);

            //Check for recently connecting players and update
            //DarkWynterGame.menuController.SetBlockStartBackInput(false);

            int numControllersBefore = Statics_Engine.SystemSettings.TOTAL_PLAYERS;
            Controller.CheckForControllers(objectLibrary);

            if (Statics_Engine.SystemSettings.TOTAL_PLAYERS != numControllersBefore)
            {
                ((GameMenuValueInput)GetMenu().GetMenuElement("# Players:")).SetRange(1, Statics_Engine.SystemSettings.TOTAL_PLAYERS);
                ((GameMenuValueInput)GetMenu().GetMenuElement("# Players:")).SetValue(Statics_Engine.SystemSettings.TOTAL_PLAYERS);
            }

            if (GetMenu().GetActiveMenuElement().GetTitle() == "Game type:")
            {
                GameMenuOption option = (GameMenuOption)GetMenu().GetActiveMenuElement();
                //need to change the settings depending on what is available here

                if (option.GetActiveOption() == "Story Mode")
                {
                    GetMenu().GetMenuElement("# Players:").Hide();
                    GetMenu().GetMenuElement("Max kills:").Hide();
                    GetMenu().GetMenuElement("Difficulty:").Show();
                    GetMenu().GetMenuElement("Bots:").Hide();
                    GetMenu().GetMenuElement("Bot intelligence:").Show();
                    SetBackground(storyModeTexture);
                }

                else if (option.GetActiveOption() == "Last man standing")
                {
                    GetMenu().GetMenuElement("# Players:").Show();
                    GetMenu().GetMenuElement("Max kills:").Hide();
                    GetMenu().GetMenuElement("Difficulty:").Hide();
                    GetMenu().GetMenuElement("Bots:").Show();
                    GetMenu().GetMenuElement("Bot intelligence:").Show();
                    SetBackground(lastmanTexture);
                }
                else if (option.GetActiveOption() == "Deathmatch")
                {
                    GetMenu().GetMenuElement("# Players:").Show();
                    GetMenu().GetMenuElement("Max kills:").Show();
                    GetMenu().GetMenuElement("Difficulty:").Hide();
                    GetMenu().GetMenuElement("Bots:").Show();
                    GetMenu().GetMenuElement("Bot intelligence:").Show();
                    SetBackground(deathMatchTexture);
                }
                else if (option.GetActiveOption() == "Survival")
                {
                    GetMenu().GetMenuElement("# Players:").Show();
                    GetMenu().GetMenuElement("Max kills:").Hide();
                    GetMenu().GetMenuElement("Difficulty:").Show();
                    GetMenu().GetMenuElement("Bots:").Hide();
                    GetMenu().GetMenuElement("Bot intelligence:").Show();
                    SetBackground(survivalTexture);
                }
            }
        }

        /// <summary>
        /// Commit user selections.
        /// </summary>
        public void GameSetupCommit()
        {
            GameMenuOption option;      // Text Option
            GameMenuValueInput value;   // Numeric Option

            // Number of players that will play
            value = (GameMenuValueInput)GetMenu().GetMenuElement("# Players:");
            Statics_Engine.GameSettings.numberOfHumansActive = value.GetValue();

            // Game type selection
            option = (GameMenuOption)GetMenu().GetMenuElement("Game type:");

            if (option.GetActiveOption() == "Story Mode")
            {
                Statics_Engine.GameSettings.gameType = Enums_Engine.GameType.STORY_MODE;
            }
            else if (option.GetActiveOption() == "Last man standing")
            {
                Statics_Engine.GameSettings.gameType = Enums_Engine.GameType.LAST_MAN_STANDING;
            }
            else if (option.GetActiveOption() == "Deathmatch")
            {
                Statics_Engine.GameSettings.gameType = Enums_Engine.GameType.DEATHMATCH;
            }
            else if (option.GetActiveOption() == "Survival")
            {
                Statics_Engine.GameSettings.gameType = Enums_Engine.GameType.SURVIVAL;
            }
            // Max number of kills to win
            value = (GameMenuValueInput)GetMenu().GetMenuElement("Max kills:");
            Statics_Engine.GameSettings.maxNumberOfKills = value.GetValue();

            // Number of AI Bots
            if (option.GetActiveOption() != "Survival" && option.GetActiveOption() != "Story Mode")
            {
                value = (GameMenuValueInput)GetMenu().GetMenuElement("Bots:");
                Statics_Engine.GameSettings.botCount = value.GetValue();
            }
            else
            {
                string difficultyLevel = ((GameMenuOption)GetMenu().GetMenuElement("Difficulty:")).GetActiveOption();
                if (difficultyLevel == "Lamer")
                {
                    Statics_Engine.GameSettings.botCount = 3;
                }
                else if (difficultyLevel == "Average Joe")
                {
                    Statics_Engine.GameSettings.botCount = 6;
                }
                else if (difficultyLevel == "Uber Mental")
                {
                    Statics_Engine.GameSettings.botCount = 10;
                }
            }
            // Bot intelligence (1= dumb, 10= super smart)
            value = (GameMenuValueInput)GetMenu().GetMenuElement("Bot intelligence:");
            Statics_Engine.GameSettings.botIntelligence = value.GetValue();
        }
    }
}