/// <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)
        {
            if(primitiveDrawer == null)
            {
                primitiveDrawer = new PrimitiveDrawer(GraphicsDevice.Viewport.TitleSafeArea.Width, GraphicsDevice.Viewport.TitleSafeArea.Height);
                primitiveDrawer.Setup(GraphicsDevice, 0, 0);
                dragDropInterface = new DragDropInterface(VirtualWidth, VirtualHeight);

            }

            handler.Update();

            if(handler.CheckPressedKey(Keys.LeftShift) && handler.CheckPressedKey(Keys.Escape))
            {
                state = 0;
            }

            currentMouseX = handler.GetMouseX();
            currentMouseY = handler.GetMouseY();

            mainMenu.Update(handler);

            if(state == 2)
            {

                if (handler.CheckJustPressedKey(Keys.Escape))
                {
                    state = 7;
                }

                editor.Update(handler);
                if(editor.Finished())
                {
                    string fileName = editor.GetFileName();
                    string safeFileName = editor.GetSafeFileName();
                    dragDropInterface.SetFile(fileName, safeFileName);
                    state = 3;
                }
            }
            else
            if(state ==4)
            {
                cache.Update(handler);
                pieceMode.Update(handler);
                back.Update(handler);
                optionsMenu.Update(handler);
                if (handler.CheckMouseIn(back) && handler.CheckLeftMouseJustPressed())
                {
                    state = 0;
                }

                if (handler.CheckMouseIn(pieceMode) && handler.CheckLeftMouseJustPressed())
                {
                    pieceModeA = !pieceModeA;

                    pieceMode.SetText((pieceModeA) ? "Piece Mode B" : "Piece Mode A");
                    ClassicTetrisGame.SetPieceMode(pieceModeA);
                }
            }
            else
            if (state == 0)
            {
                classic.Update(handler);
                maker.Update(handler);
                story.Update(handler);

                anyLevel.Update(handler);
                options.Update(handler);
                exit.Update(handler);

                if (handler.CheckMouseIn(options) && handler.CheckLeftMouseJustPressed())
                {
                    state = 4;
                    //classicLink.CreateGame("TajLevel.taj");
                }

                if (handler.CheckMouseIn(story) && handler.CheckLeftMouseJustPressed())
                {
                    string[] levels = package.GetProtectedLevels();

                    this.levels = new MainMenuButton[levels.Length];

                    for (int i = 0;i < levels.Length; i++)
                    {
                        this.levels[i] = new MainMenuButton(levels[i]);
                        this.levels[i].ModifyY(51 * i);
                    }

                        state = 9;
                }

                if (handler.CheckMouseIn(anyLevel) && handler.CheckLeftMouseJustPressed())
                {
                    string[] levelsN = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/TajTetris/LevelPacks");

                    List<string> levels = new List<string>();
                    for (int i = 0;i < levelsN.Length; i++)
                    {
                        if(levelsN[i].EndsWith(".taj"))
                        levels.Add(levelsN[i]);
                    }

                    this.levels = new MainMenuButton[levels.Count];

                    for (int i = 0;i < levels.Count;i++)
                    {
                        this.levels[i] = new MainMenuButton(Path.GetFileNameWithoutExtension(levels[i]));
                        this.levels[i].ModifyY(51 * i);
                    }

                    state = 8;
                }

                if (handler.CheckMouseIn(classic) && handler.CheckLeftMouseJustPressed())
                {
                    classicLink.CreateGame("TajLevel.taj");
                    Logger.WriteLine("Started new Classic Game.");
                    state = 1;
                }

                if (handler.CheckMouseIn(maker) && handler.CheckLeftMouseJustPressed())
                {
                    state = 2;
                }

                if (handler.CheckMouseIn(exit) && handler.CheckLeftMouseJustPressed())
                {
                    this.Exit();
                }
            }
            else
            if(state == 1)
            {

                classicLink.Update(handler);

                if(handler.CheckJustPressedKey(Keys.Escape))
                {
                    state = 5;
                }
            }
            else
            if(state == 3)
            {
                dragDropInterface.Update(handler);
            }
            else
               //dragDropInterface.Update(handler);

            if (state == 5 || state == 12 || state == 13)
            {
                Save.Update(handler);
                Load.Update(handler);
                back.Update(handler);

                if (handler.CheckMouseIn(back) && handler.CheckLeftMouseJustPressed())
                {
                    state = 0;
                }

                if (state == 5)
                {
                    if (handler.CheckMouseIn(Save) && handler.CheckLeftMouseJustPressed())
                    {

                        state = 1;
                        classicLink.SetFullSave(true);

                        Logger.WriteLine("Saved Classic Game.");
                        SaveFileSystem.SaveObjectToFile(classicLink, "ClassicGame.tajSave");
                    }

                    if (handler.CheckMouseIn(Load) && handler.CheckLeftMouseJustPressed())
                    {
                        classicLink = SaveFileSystem.LoadObjectFromFile<TajLevelLinker>("ClassicGame.tajSave", new GameLoader());

                        Logger.WriteLine("Loaded Classic Game.");
                        state = 1;
                    }

                    if (handler.CheckJustPressedKey(Keys.Escape))
                    {
                        state = 1;
                    }

                }

                if (state == 12)
                {
                    if (handler.CheckMouseIn(Save) && handler.CheckLeftMouseJustPressed())
                    {

                        state = 11;
                        freePlayLink.SetFullSave(true);
                        Logger.WriteLine("Saved Free Play Game.");
                        SaveFileSystem.SaveObjectToFile(freePlayLink, "FreePlayGame.tajSave");
                    }

                    if (handler.CheckMouseIn(Load) && handler.CheckLeftMouseJustPressed())
                    {
                        freePlayLink = SaveFileSystem.LoadObjectFromFile<TajLevelLinker>("FreePlayGame.tajSave", new GameLoader());

                        Logger.WriteLine("Loaded Freeplay Game.");
                        state = 11;
                    }

                    if (handler.CheckJustPressedKey(Keys.Escape))
                    {
                        state = 11;
                    }

                }

                if (state == 13)
                {
                    if (handler.CheckMouseIn(Save) && handler.CheckLeftMouseJustPressed())
                    {

                        state = 10;
                        classicLink.SetFullSave(true);

                        Logger.WriteLine("Saved Story Game.");
                        SaveFileSystem.SaveObjectToFile(classicLink, "StoryGame.tajSave");
                    }

                    if (handler.CheckMouseIn(Load) && handler.CheckLeftMouseJustPressed())
                    {
                        classicLink = SaveFileSystem.LoadObjectFromFile<TajLevelLinker>("StoryGame.tajSave", new GameLoader());

                        Logger.WriteLine("Loaded Story Game.");
                        state = 10;
                    }

                    if (handler.CheckJustPressedKey(Keys.Escape))
                    {
                        state = 10;
                    }

                }
            }

            else if (state ==7)
            {
                back.Update(handler);
                if (handler.CheckJustPressedKey(Keys.Escape))
                {
                    state = 2;
                }
                if (handler.CheckMouseIn(back) && handler.CheckLeftMouseJustPressed())
                {
                    state = 0;
                }

            }
            else if (state == 8)
            {
                optionsMenu.Update(handler);

                if (handler.CheckJustPressedKey(Keys.Escape))
                {
                    state = 0;
                }

                foreach (MainMenuButton b in levels)
                {
                    b.Update(handler);
                    b.ModifyY((int)(handler.LeftMouseDraggedBy().Y / 40.0f));

                    if (handler.CheckMouseIn(b) && handler.CheckLeftMouseJustPressed())
                    {

                            freePlayLink = SaveFileSystem.LoadObjectFromFile<TajLevelLinker>("LevelPacks/" + b.GetText() + ".taj", new GameLoader());
                            state = 11;
                            break;
                    }
                }
            }
            else if (state == 9)
            {
                optionsMenu.Update(handler);

                if(handler.CheckJustPressedKey(Keys.Escape))
                {
                    state = 0;
                }

               foreach(MainMenuButton b in levels)
               {
                   b.Update(handler);
                   b.ModifyY((int)(handler.LeftMouseDraggedBy().Y / 40.0f));

                   if (handler.CheckMouseIn(b) && handler.CheckLeftMouseJustPressed())
                   {
                       if(b.GetText() != "Locked")
                       {
                           storyLink = SaveFileSystem.LoadObjectFromFile<TajLevelLinker>("LevelPacks/" + b.GetText() + ".taj", new GameLoader());
                           storyLink.SetLevelPack(package);
                           state = 10;
                           break;
                       }
                   }
               }

            } else if (state == 10)
            {
                if (handler.CheckJustPressedKey(Keys.Escape))
                {
                    state = 13;
                }
                storyLink.Update(handler);

            }
            else if (state == 11)
            {

                if (handler.CheckJustPressedKey(Keys.Escape))
                {
                    state = 12;
                }
                freePlayLink.Update(handler);
            }

            //lnk.Update(handler);
            //tGame.Update(handler);
            base.Update(gameTime);
        }