using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace xna_finall
{
    class GameComponen_Menu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Texture2D Texture;  // 第一張圖形
        Texture2D Texture_Selected; // 第二張圖形
        Texture2D Texture_BG; // 選單背景圖形
        SpriteBatch sBatch;
        GraphicsDevice device;
        Game game;

        Rectangle destRectAll; // 目的區 的整塊矩形區塊
        int MenuCount = 4;     // 假設 選單有四個選項
        Rectangle[] destRect;  // 目的區 的 矩形區塊
        Rectangle[] srcRect;   // 來源區 的 矩形區塊
        public int SelectedNo = 0; // 目前選到的選項

        Rectangle destRectBG; // 背景圖在目的區 的整塊矩形區塊

        KeyboardState oldState;  // 宣告一個KeyboardState 結構的變數

        public GameComponen_Menu(Game game, Texture2D texture2D,
                                            Texture2D texture2D_selected,
                                            Texture2D texture_BG)
            : base(game)
        {
            // TODO: Construct any child components here
            this.game = game;
            device = game.GraphicsDevice;
            Texture = texture2D;
            Texture_Selected = texture2D_selected;
            Texture_BG = texture_BG;

            srcRect = new Rectangle[MenuCount];
            destRect = new Rectangle[MenuCount];

            int h = Texture.Height / MenuCount;

            for (int i = 0; i < MenuCount; i++)
                srcRect[i] = new Rectangle(0, i * h, Texture.Width, h);

            int x = device.Viewport.Width / 2 - Texture.Width / 2-120;
            int y = device.Viewport.Height / 2 - Texture.Height / 2+160;

            for (int i = 0; i < MenuCount; i++)
                destRect[i] = new Rectangle(srcRect[i].X + x, srcRect[i].Y + y, Texture.Width, h);

            destRectAll = new Rectangle(x, y, Texture.Width, Texture.Height);

            destRectBG = new Rectangle(0,
                0,
                Texture_BG.Width, Texture_BG.Height);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            oldState = Keyboard.GetState();  //得到目前鍵盤每一個按鍵的狀況

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (GameStateClass.currentGameState != GameStateClass.GameState.Menu)
                return;

            // TODO: Add your update code here
            KeyboardState newState;  // 宣告一個KeyboardState 結構的變數
            newState = Keyboard.GetState();  //得到目前鍵盤每一個按鍵的狀況
            if (oldState.IsKeyUp(Keys.Up) && newState.IsKeyDown(Keys.Up)) //判斷Up鍵是否已經被按下
            {
                SelectedNo--;
                if (SelectedNo < 0) SelectedNo = MenuCount - 1;
            }
            if (oldState.IsKeyUp(Keys.Down) && newState.IsKeyDown(Keys.Down)) //判斷Down鍵是否已經被按下
            {
                SelectedNo++;
                if (SelectedNo > MenuCount - 1) SelectedNo = 0;
            }

            if (newState.IsKeyDown(Keys.Enter) || newState.IsKeyDown(Keys.Space))
            {
                if (SelectedNo == 0)
                    GameStateClass.changeState(GameStateClass.GameState.Game1, game);
                else if (SelectedNo == 1)
                    GameStateClass.changeState(GameStateClass.GameState.Help, game);
                else if (SelectedNo == 2)
                    GameStateClass.changeState(GameStateClass.GameState.about, game);
                else if (SelectedNo == 3) game.Exit();

            }

            oldState = newState;
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (GameStateClass.currentGameState != GameStateClass.GameState.Menu)
                return;
            device.Clear(Color.White);

            sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            if (sBatch == null) return;

            sBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied, SamplerState.LinearWrap, DepthStencilState.Default, RasterizerState.CullCounterClockwise);
            //呈現背景
            sBatch.Draw(Texture_BG,
                        destRectBG,
                        Color.White);

            //呈現 整個選單
            sBatch.Draw(Texture,           // 2D Texture
                       destRectAll,        // 目的區 的 矩形區塊
                       null,               // 來源區 的 矩形區塊
                       Color.White,        // 顏色 濾鏡
                       0,                  // 旋轉徑度
                       Vector2.Zero,       // 2D Texture旋轉中心點
                       SpriteEffects.None, // 旋轉效果
                       0.0f);              // 圖層深度 0.0 ~ 1.0 (後)

            //SelectedNo 呈現 選到的選單選項
            sBatch.Draw(Texture_Selected,    // 2D Texture
                       destRect[SelectedNo], // 目的區 的 矩形區塊
                       srcRect[SelectedNo],  // 來源區 的 矩形區塊
                       Color.White,          // 顏色 濾鏡
                       0,                    // 旋轉徑度
                       Vector2.Zero,         // 2D Texture旋轉中心點
                       SpriteEffects.None,   // 旋轉效果
                       0.0f);                // 圖層深度 0.0 ~ 1.0 (後)

            sBatch.End();
        }
    }
}
