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

namespace WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        MouseState oldMouseState;

        
        

        

        int []p=new int [4*4];
        int[] count = new int[8];
        Texture2D[] a = new Texture2D[8];

        Texture2D backP;
        Texture2D back1;
        Texture2D back2;
        Texture2D back3;
        Texture2D back4;
        Texture2D back5;

        Texture2D victory;

        int[] back = new int[4 * 4];   //0=post

        Random x = new Random();
        int xx;

        int c1=-1;
        int c2 = -1;

        int pair = 0;


        int clickC = 0;

        Boolean turn1=false;
        Boolean turn2 = false;

        Double t1T = 0;
        Double t2T = 0;


        Double fT=0;

        Double tT = 0;
        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.IsMouseVisible = true;  // 呈現滑鼠游標
            this.Window.AllowUserResizing = false; // 允許視窗縮放
            graphics.PreferredBackBufferWidth = 800;  //視窗的預設寬
            graphics.PreferredBackBufferHeight = 800; //視窗的預設高
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            a[0] = Content.Load<Texture2D>("1");
            a[1] = Content.Load<Texture2D>("2");
            a[2] = Content.Load<Texture2D>("3");
            a[3] = Content.Load<Texture2D>("4");
            a[4] = Content.Load<Texture2D>("5");
            a[5] = Content.Load<Texture2D>("6");
            a[6] = Content.Load<Texture2D>("7");
            a[7] = Content.Load<Texture2D>("8");

            backP = Content.Load<Texture2D>("back");
            back1 = Content.Load<Texture2D>("back 1");
            back2 = Content.Load<Texture2D>("back 2");
            back3 = Content.Load<Texture2D>("back 3");
            back4 = Content.Load<Texture2D>("back 4");
            back5 = Content.Load<Texture2D>("back 5");

            victory = Content.Load<Texture2D>("victory");

            
            

            Rectangle recDest = new Rectangle(0, 0, 150, 150);

            for (int i = 0; i < 16; i++)
            {
                xx = x.Next(8);
                while (count[xx] == 2)
                {
                    xx = x.Next(8);
                }
                count[xx]++;
                p[i] =xx;


            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            tT += gameTime.ElapsedGameTime.Milliseconds;


            MouseState mouseState = Mouse.GetState();  //得到目前滑鼠的狀態
            if (mouseState.LeftButton == ButtonState.Pressed &&
                oldMouseState.LeftButton == ButtonState.Released)   //滑鼠的左鍵被按下
            {
                if (mouseState.X <= 800 && mouseState.Y <= 800&&clickC==0) {
                    c1 = 4 * (mouseState.Y / 200) + mouseState.X / 200;
                    if (back[c1] == 0) {
                        back[c1] = 1;
                        clickC++;
                        
                        turn1 = true;
                        t1T = tT;
                    }
                    
                }
                if (mouseState.X <= 800 && mouseState.Y <= 800 && clickC == 1)
                {
                    c2 = 4 * (mouseState.Y / 200) + mouseState.X / 200;
                    if (back[c2] == 0)
                    {
                        
                        back[c2] = 1;
                        clickC++;
                        fT= tT;
                        
                        turn2 = true;
                        t2T = tT;
                    }
                    
                }
            }
            oldMouseState = mouseState;

            

            if (tT >fT+1500 &&clickC==2) {
                clickC = 0;
                if (p[c1] == p[c2])
                {
                    pair++;
                }
                else {
                    back[c1] = 0;
                    back[c2] = 0;
                }

                
            }

            

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            Rectangle recDest = new Rectangle(0, 0, 200, 200);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            for (int i = 0; i < 16; i++) {                
                
                recDest = new Rectangle(0+(i%4)*200,0+200*(i/4),200,200);

                spriteBatch.Draw(a[p[i]], recDest, Color.White);

                if(back[i]==0){
                    spriteBatch.Draw(backP, recDest, Color.White);
                }
            }

            recDest = new Rectangle((c1%4)*200,(c1/4)*200, 200, 200);

            int iii = 100;

            if(turn1==true){
                if (tT < t1T + iii*1) {
                    spriteBatch.Draw(back1, recDest, Color.White);
                }
                else if (tT  < t1T + iii * 2)
                {
                    spriteBatch.Draw(back2, recDest, Color.White);
                }
                else if (tT < t1T + iii * 3)
                {
                    spriteBatch.Draw(back3, recDest, Color.White);
                }
                else if (tT< t1T +iii * 4)
                {
                    spriteBatch.Draw(back4, recDest, Color.White);
                }
                else if (tT< t1T + iii * 5)
                {
                    spriteBatch.Draw(back5, recDest, Color.White);
                }

                if (tT >= t1T + iii * 5) {
                    turn1 = false;
                }
            }

            recDest = new Rectangle((c2 % 4) * 200, (c2 / 4) * 200, 200, 200);

            if (turn2 == true)
            {
                if (tT < t2T + iii * 1)
                {
                    spriteBatch.Draw(back1, recDest, Color.White);
                }
                else if (tT  < t2T + iii * 2)
                {
                    spriteBatch.Draw(back2, recDest, Color.White);
                }
                else if (tT  < t2T + iii * 3)
                {
                    spriteBatch.Draw(back3, recDest, Color.White);
                }
                else if (tT < t2T + iii * 4)
                {
                    spriteBatch.Draw(back4, recDest, Color.White);
                }
                else if (tT< t2T + iii * 5)
                {
                    spriteBatch.Draw(back5, recDest, Color.White);
                }

                if (tT >= t2T + iii * 5)
                {
                    turn2 = false;
                }
            }

            if (pair == 8) {
                recDest = new Rectangle(200,250,400,300);
                spriteBatch.Draw(victory, recDest, Color.White);

            }

            
            
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
