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

namespace DarkWynter.Engine.Menus
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using DWGame.Globals;
    using DWGame;
    using DW.Stream;


    #endregion


    /// <summary>
    /// Generic Game Screen.
    /// </summary>
    public class GameScreen: Game
    {
        /// <summary>
        /// Menu Object used by this screen.
        /// </summary>
        protected GameMenu menu;

        /// <summary>
        /// Stores the EngineState associated with this screen
        /// </summary>
        protected Enums_Engine.MenuState engineState;

        /// <summary>
        /// Background texture used by this screen.
        /// </summary>
        protected Texture2D backgroundTexture;

        /// <summary>
        /// Overlay Graphics list drawn from first to last.
        /// </summary>
        protected List<Texture2D> graphics;

        /// <summary>
        /// Alpha value to draw this screen with.
        /// </summary>
        protected float backgroundAlpha = 1.0f;

        /// <summary>
        /// String location of the screen
        /// </summary>
        protected string backgroundTexturePath;
        /// <summary>
        /// Location of the image on the screen
        /// </summary>
        protected Rectangle drawRectangle;

        protected float alpha;
        protected Enums_Engine.MenuState menuState;
        protected Enums_Engine.EngineState eState;
        protected bool hasSprites;
        protected bool hasMenu;
        protected Texture2D bgTexture;


        /// <summary>
        /// Game Screen constructor.
        /// </summary>
        /// <param name="GameEngineState">Tells this screen what EngineState it will run under.</param>
        public GameScreen(Enums_Engine.MenuState GameEngineState)
        {
            engineState = GameEngineState;
        }

        /// <summary>
        /// Returns the Engine State associated with this screen.
        /// </summary>
        /// <returns></returns>
        public Enums_Engine.MenuState GetEngineState()
        {
            return engineState;
        }

        /// <summary>
        /// Returns the game menu.
        /// </summary>
        /// <returns>This screen's game menu.</returns>
        public GameMenu GetMenu()
        {
            return menu;
        }

        /// <summary>
        /// Set the background texture
        /// </summary>
        /// <param name="backgroundTexture">Texture used in background.</param>
        public void SetBackground(Texture2D backgroundTexture)
        {
            this.backgroundTexture = backgroundTexture;
            backgroundAlpha = 1.0f;
        }

        /// <summary>
        /// Set the background texture with a specific alpha value.
        /// </summary>
        /// <param name="backgroundTexture">Texture used in background.</param>
        /// <param name="alpha">Alpha value used to draw.</param>
        public void SetBackground(Texture2D backgroundTexture, float alpha)
        {
            this.backgroundTexture = backgroundTexture;
            backgroundAlpha = alpha;
        }
        public void SetAlpha(float alpha)
        {
            backgroundAlpha = alpha;
        }
        /// <summary>
        /// Draw this game screen.
        /// </summary>
        /// <param name="spriteBatch">SpriteBatch used to draw used to draw.</param>
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            // First draw the background texture if it is not null
            if (backgroundTexture != null)
            {
                spriteBatch.Draw(backgroundTexture, this.drawRectangle,
                                 new Color(255, 255, 255, (byte)(255 * backgroundAlpha)));
            }

            //now draw the menu elements
            menu.Draw(spriteBatch);
        }

        /// <summary>
        /// Check to see if menu has been updated by user.
        /// </summary>
        /// <param name="objectLibrary">Current ObjectLibrary.</param>
        public virtual void Update()
        {
        }
        /// <summary>
        /// Method invoked when the right menu button has been pressed
        /// </summary>
        public virtual void MenuRight()
        {
            UpdateMenuController(GetMenu().GetActiveMenuElement(), Enums_Engine.MenuDirection.RIGHT);
        }
        /// <summary>
        /// Method invoked when the left menu button has been pressed
        /// </summary>
        public virtual void MenuLeft()
        {
            UpdateMenuController(GetMenu().GetActiveMenuElement(), Enums_Engine.MenuDirection.LEFT);
        }
        /// <summary>
        /// Method invoked when the down menu button has been pressed
        /// </summary>
        public virtual void MenuDown()
        {
            GetMenu().NextMenuElement();
        }
        /// <summary>
        /// Method invoked when the up menu button has been pressed
        /// </summary>
        public virtual void MenuUp()
        {
            GetMenu().PreviousMenuElement();
        }
        /// <summary>
        /// Method invoked when the start menu button has been pressed
        /// </summary>
        /// <param name="objectLibrary">Object Library</param>
        public virtual void MenuStart(int index)
        {
            bool nextGameState = false;

            if (Statics.menuState == Enums_Engine.MenuState.FINALIZE_G2LSTUFF)
            {
                Statics.menuState = Enums_Engine.MenuState.TITLE_SCREEN;
            }

            if (Statics.menuState == Enums_Engine.MenuState.GAME_OVER)
            {
                Statics.menuState = Enums_Engine.MenuState.TITLE_SCREEN;
            }

            if (DW.Stream.Window.menuSystem.gameScreens[index].engineState == Enums_Engine.MenuState.LOAD_LEVEL)
                {
                    Statics.engineState = Enums_Engine.EngineState.GAME_MODE;
                }


            if (Statics.menuState != Enums_Engine.MenuState.GAME_PAUSE)
            {
                nextGameState = true;
                Statics.menuState++;
            }

            // Commit Game and Player
            if (Statics.menuState == Enums_Engine.MenuState.LOAD_LEVEL && nextGameState)
            {
               // DarkWynterEngine.Load_LevelIndex();
            }
        }
        /// <summary>
        /// Method invoked when the down menu button has been pressed
        /// </summary>
        public virtual void MenuBack()
        {
            if (Statics.menuState != Enums_Engine.MenuState.TITLE_SCREEN &&
                Statics.menuState != Enums_Engine.MenuState.GAME_OVER)
            {
                Statics.menuState--;        // Exit
            }
        }

        /// <summary>
        /// Update the active element based on user input
        /// </summary>
        /// <param name="activeElement">Active menu element</param>
        /// <param name="menuDirection">Menu direction key that was pressed</param>
        private void UpdateMenuController(GameMenuElement activeElement, Enums_Engine.MenuDirection menuDirection)
        {
            // OptionBox
            if (activeElement.GetElementType() == Enums_Engine.MenuElementType.OPTION)
            {
                // Cast as option and change value
                GameMenuOption option = (GameMenuOption)activeElement;
                // If they pressed left, get previous option
                if (menuDirection == Enums_Engine.MenuDirection.LEFT)
                {
                    //Audio.MenuLeft();
                    option.PreviousOption();
                }
                // Or if they pressed right, get next option
                else if (menuDirection == Enums_Engine.MenuDirection.RIGHT)
                {
                   // Audio.MenuRight();
                    option.NextOption();
                }
            }

            // ValueSlider
            else if (activeElement.GetElementType() == Enums_Engine.MenuElementType.VALUE)
            {
                // Cast as ValueInput type and change value
                GameMenuValueInput valueInput = (GameMenuValueInput)activeElement;
                //int activeIndex = playerSetup.GetMenu().GetActiveIndex();

                // If they pressed left, decrease the value
                if (menuDirection == Enums_Engine.MenuDirection.LEFT)
                {
                    //Audio.MenuLeft();
                    valueInput.Decrement();
                }
                // Or if they pressed right, increase it
                else if (menuDirection == Enums_Engine.MenuDirection.RIGHT)
                {
                    //Audio.MenuRight();
                    valueInput.Increment();
                }
            }

            else if (activeElement.GetElementType() == Enums_Engine.MenuElementType.IMAGE_SCROLL)
            {
                // Cast as ValueInput type and change value
                GameMenuImageScroller scroller = (GameMenuImageScroller)activeElement;
                //int activeIndex = playerSetup.GetMenu().GetActiveIndex();

                // If they pressed left, decrease the value
                if (menuDirection == Enums_Engine.MenuDirection.LEFT)
                {
                    //Audio.MenuLeft();
                    scroller.PreviousItem();
                }
                // Or if they pressed right, increase it
                else if (menuDirection == Enums_Engine.MenuDirection.RIGHT)
                {
                   // Audio.MenuRight();
                    scroller.NextItem();
                }
            }
        }



    }
}