#region Using Statements
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
#endregion

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

        KeyboardState _curState;
        KeyboardState _lastState;

        StorageDevice _device;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

        }
        
        /// <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()
        {
            Object obj = (Object)"GetDevice for Player One";
            Guide.BeginShowStorageDeviceSelector(StorageDeviceSelected, obj);
            
            base.Initialize();
        }

        private void StorageDeviceSelected(IAsyncResult result)
        {
            _device = Guide.EndShowStorageDeviceSelector(result);
            if (_device.IsConnected)
            {
                //all is good
            }
        }

        /// <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 default game to exit on Xbox 360 and Windows
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            _curState = Keyboard.GetState();

            if (_curState.IsKeyDown(Keys.A) == true && _lastState.IsKeyDown(Keys.A) == false)
            {
                StorageContainer container = _device.OpenContainer("Chapter 6");

                string data = "my game. my world.";

                FileStream fs = new FileStream(container.Path + "\\data.dat",FileMode.Create);
                BinaryWriter br = new BinaryWriter(fs);

                Char newChar;

                foreach (Char ch in data)
                {
                    br.Write((byte)(ch + 96));
                }

                br.Close();
                fs.Close();
            }

            _lastState = _curState;

        }

        /// <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)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);
        }
    }
}

