

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.Storage;
using Microsoft.Xna.Framework.Content;

namespace WindowsGame1
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class G2D_Bar : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game game;
        Texture2D Texture_Bar; // 內部的白色紋理圖

        public Vector2 Pos = Vector2.Zero; // 擺放的位置
        
        public int score_Max = 100;       // 最高的分數值 
        public int score = 100;            // 目前的的分數值 0 ~ score_Max

        public Color colorForground = Color.White;  // 前景顏色
        public Color colorBackground = Color.Black;// 背景顏色

        public int Width = 256; // 紋理圖 寬
        public int Height = 16; // 紋理圖 高

        SpriteFont Font1; // 文字

        public G2D_Bar(Game game, SpriteFont Font1)
            : base(game)
        {
            // TODO: Construct any child components here
            this.game = game;
            this.Font1 = Font1; 
        }

        /// <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
            // 新增一張白色、不透明紋理圖 (Width * Height)
            Texture_Bar = new Texture2D(game.GraphicsDevice, Width, Height, true, SurfaceFormat.Color);
            Color[] color = new Color[Width * Height];
            for (int i = 0; i < Width * Height; i++)
                color[i] = new Color(255, 255, 255, 255);
            Texture_Bar.SetData(color); // 白色 不透明 紋理圖
            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)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch =
                (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            if (spriteBatch == null) return;

            // 將分數限制在 (0, score_Max) 之間
            score = (int)MathHelper.Clamp(score, 0, score_Max);

            Rectangle Dest1 = new Rectangle( // 前景區域
                                (int)Pos.X,
                                (int)Pos.Y,
                                (int)(Texture_Bar.Width * score * (1.0f / score_Max)),
                                Texture_Bar.Height);
            Rectangle Dest2 = new Rectangle( // 背景區域
                                (int)(Pos.X + Texture_Bar.Width * score * (1.0f / score_Max)),
                                (int)Pos.Y,
                                (int)(Texture_Bar.Width * (score_Max - score) * (1.0f / score_Max)),
                                Texture_Bar.Height);
            Rectangle Src1 = new Rectangle(0, 0, (int)(Texture_Bar.Width * score * (1.0f / score_Max)), Texture_Bar.Height);
            Rectangle Src2 = new Rectangle(0, 0, (int)(Texture_Bar.Width * (score_Max - score) * (1.0f / score_Max)), Texture_Bar.Height);

            // 字型設定
            string output = "Score: " + Convert.ToString(score); // 顯示出來的文字
            Vector2 FontOrigin = Font1.MeasureString(output) / 2; // Find the center of the string
            Vector2 FontPos = new Vector2(Pos.X + Texture_Bar.Width * 0.5f, Pos.Y + Texture_Bar.Height + 20);

            spriteBatch.Begin(); //SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            spriteBatch.Draw(Texture_Bar, Dest1, Src1, colorForground);
            spriteBatch.Draw(Texture_Bar, Dest2, Src2, colorBackground);

            spriteBatch.DrawString(Font1,     // 字型
                              output,         // 字串
                              FontPos,        // 位置
                              Color.Black, // 字的顏色
                              0,           // 旋轉角度
                              FontOrigin,  // 字串中心點
                              1.0f,        // 縮放倍數
                              SpriteEffects.None,
                              1);       // 圖層深度 0.0 ~ 1.0 (後)
            if (score == 0)
            {
                Vector2 Font = new Vector2(512, 384);
                string message1 = " Game Over! ";
                Vector2 FontO = Font1.MeasureString(message1) / 2; // 字串中心

                spriteBatch.DrawString(Font1, // 字型
                    message1, // 字串
                    Font, // 位置
                    Color.Red, // 字的顏色
                    0, // 旋轉角度
                    FontO,// 字串中心點
                   9.0f, // 縮放倍數
                    SpriteEffects.None,// 旋轉校果
                    0); // 圖層深度 0.0 ~ 1.0 (後)
            }

            if (score == 100)
            {
                Vector2 Font = new Vector2(512, 384);
                string message1 = " You Win! ";
                Vector2 FontO = Font1.MeasureString(message1) / 2; // 字串中心

                spriteBatch.DrawString(Font1, // 字型
                    message1, // 字串
                    Font, // 位置
                    Color.Red, // 字的顏色
                    0, // 旋轉角度
                    FontO,// 字串中心點
                    10.0f, // 縮放倍數
                    SpriteEffects.None,// 旋轉校果
                    0); // 圖層深度 0.0 ~ 1.0 (後)
                
            }
            spriteBatch.End();

            base.Update(gameTime);
        }
    }
}