using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using WindowsGame1.Scene;
using Microsoft.Xna.Framework.Audio;

namespace WindowsGame1
{
    public class MainMenu : GameScene
    {
        SpriteBatch spriteBatch;
        Texture2D mySpriteTexture;
        AudioEngine audioEngine;
        WaveBank waveBank;
        SoundBank soundBank;

        private enum MenuStateEnum
        {
            Select1,
            Select2,
            //Select3,
        }

        private class MenuStruct
        {
            public string Text;
            public Vector2 Position;
            public Color Color;
        }

        private SpriteFont font;
        private MenuStateEnum State;
        private Color selected, unselect;
        private List<MenuStruct> menuList = new List<MenuStruct>();
        private KeyboardState preKeyboardState;

        public MainMenu(Game game)
            : base(game, "MainMenu")
        {
            State = MenuStateEnum.Select1;
            selected = Color.Red;
            unselect = Color.Blue;
        }

        protected override void LoadContent()
        {
            audioEngine = new AudioEngine("Content\\new Project2.xgs");
            waveBank = new WaveBank(audioEngine, "Content\\Wave Bank.xwb");
            soundBank = new SoundBank(audioEngine, "Content\\Sound Bank.xsb");

            spriteBatch = new SpriteBatch(GraphicsDevice);
            mySpriteTexture = Game.Content.Load<Texture2D>("Images\\狗頂球");

            font = Game1.DefaultFont;
            Vector2 size;
            MenuStruct ms = new MenuStruct();
            ms.Color = selected;
            ms.Text = "Play";
            size = font.MeasureString(ms.Text) / 2;
            ms.Position = new Vector2((float)Math.Ceiling(400 - size.X), (float)Math.Ceiling(300 - size.Y));
            menuList.Add(ms);

            ms = new MenuStruct();
            ms.Color = unselect;
            ms.Text = "Exit";
            size = font.MeasureString(ms.Text) / 2;
            ms.Position = new Vector2((float)Math.Ceiling(400 - size.X), (float)Math.Ceiling(340 - size.Y));
            menuList.Add(ms);

            //ms = new MenuStruct();
            //ms.Color = unselect;
            //ms.Text = "Exit";
            //ms.Position = new Vector2(400, 380) - font.MeasureString(ms.Text) / 2;
            //menuList.Add(ms);

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            //soundBank.PlayCue("bubble");
            //BeginPause();
            KeyboardState keyboardState = Keyboard.GetState();
            switch (State)
            {
                case MenuStateEnum.Select1:
                    if (oneClick(keyboardState, Keys.Up))
                    {
                        selectMenu(menuList[0], menuList[2]);
                        State = MenuStateEnum.Select2;
                    }
                    else if (oneClick(keyboardState, Keys.Down))
                    {
                        selectMenu(menuList[0], menuList[1]);
                        State = MenuStateEnum.Select2;
                    }
                    else if (oneClick(keyboardState, Keys.Enter))
                    {
                        Game1.SceneManager.AddScene(new GamePlay(Game));
                    }
                    break;
                case MenuStateEnum.Select2:
                    if (oneClick(keyboardState, Keys.Up))
                    {
                        selectMenu(menuList[1], menuList[0]);
                        State = MenuStateEnum.Select1;
                    }
                    else if (oneClick(keyboardState, Keys.Down))
                    {
                        selectMenu(menuList[1], menuList[2]);
                        State = MenuStateEnum.Select2;
                    }
                    else if (oneClick(keyboardState, Keys.Enter))
                    {
                        Game.Exit();
                    }
                    break;
                //case MenuStateEnum.Select3:
                //    if (oneClick(keyboardState, Keys.Up)) {
                //        selectMenu(menuList[2], menuList[1]);
                //        State = MenuStateEnum.Select2;
                //    } else if (oneClick(keyboardState, Keys.Down)) {
                //        selectMenu(menuList[2], menuList[0]);
                //        State = MenuStateEnum.Select1;
                //    } else if (oneClick(keyboardState, Keys.Enter)) {
                //        Game.Exit();
                //    }
                //    break;
            }
            preKeyboardState = keyboardState;
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Sprite.Begin();           
            Vector2 pos = new Vector2(0, 0);
            Rectangle rec = new Rectangle(-10, 0, 810, 490);
            Sprite.Draw(mySpriteTexture, rec, Color.White); // Color.White 是過濾顏色 也就是完全不過濾
            foreach (MenuStruct ms in menuList)
            {
                Sprite.DrawString(font, ms.Text, ms.Position, ms.Color);
            }
            Sprite.End();
            base.Draw(gameTime);
        }

        private void selectMenu(MenuStruct from, MenuStruct to)
        {
            from.Color = unselect;
            to.Color = selected;
        }

        private bool oneClick(KeyboardState nowKeyboardState, Keys key)
        {
            return preKeyboardState.IsKeyUp(key) && nowKeyboardState.IsKeyDown(key);
        }
    }
}
