public GameManagerController(GameManagerScope scope, UIManagerService uiManager, CreateUIService createUIService,
            ClientSiteManagerService clientSiteManagerService, MessageService messageService)
        {
            myScope = scope;
            myUIManager = uiManager;
            this.createUIService = createUIService;
            myClientSiteManagerService = clientSiteManagerService;
            myMessageService = messageService;
            myScope.Model = new GameManagerModel();
            myScope.Visible = true;
            myClientSiteManagerService.GetGamesByUser(myUIManager.ClientInfo.LoggedInUser.Hash);

            myClientSiteManagerService.OnGetGamesByUserReceived += OnOnGetGamesByUserReceivedFn;
            myClientSiteManagerService.OnDeveloperCreateGameReceived += OnDeveloperCreateGameReceivedFn;
            myClientSiteManagerService.OnDoesGameNameExistReceived += OnDoesGameNameExistReceivedFn;

            myScope.Model.CreateGame += CreateGameFn;
            myScope.Model.DeleteGame += DeleteGameFn;
            myScope.Watch("model.selectedGame",
                () =>
                {
                    if (myScope.Model.SelectedGame == null) return;

                    createUIService.CreateSingleton<GameEditorScope>(GameEditorController.View,
                        (_scope, elem) =>
                        {
                            _scope.Model = new GameEditorModel();
                            _scope.Model.Game = myScope.Model.SelectedGame;
                        });
                    if (!scope.Minimized)
                        scope.Minimize();
                });
        }
        public LoginController(LoginScope scope, UIManagerService uiManager,
            ClientSiteManagerService clientSiteManagerService, MessageService messageService,
            CreateUIService createUIService, ClientManagerService clientManagerService)
        {
            myScope = scope;
            myScope.Visible = true;
            myUIManager = uiManager;
            myclientSiteManagerService = clientSiteManagerService;
            myMessageService = messageService;
            myCreateUIService = createUIService;
            this.clientManagerService = clientManagerService;
            myScope.Model = new LoginScopeModel();
/*
            scope.Model.dosomething += (o) =>
            {
                Console.WriteLine(o);
            };
*/
            scope.Model.Username = "******";
            scope.Model.Password = "******";


            myScope.Model.WindowClosed = () => { Window.Alert("woooo"); };
            myScope.Model.LoginAccount = LoginAccountFn;
            myScope.Model.CreateAccount = CreateAccountFn;
            myclientSiteManagerService.OnLogin += OnLoginFn;
            myclientSiteManagerService.OnUserCreate += OnUserCreateFn;

            /* myScope.Model.Username = "******";
            myScope.Model.Password = "******";

            */
//            Window.SetTimeout(LoginAccountFn, 250);
        }
        public GameTestEditorController(GameTestEditorScope scope, ClientSiteManagerService clientSiteManagerService, ClientDebugManagerService clientDebugManagerService, MessageService messageService, CreateUIService createUIService)
        {
            this.scope = scope;
            myClientSiteManagerService = clientSiteManagerService;
            this.clientDebugManagerService = clientDebugManagerService;
            myMessageService = messageService;
            myCreateUIService = createUIService;
            this.scope.Visible = false;
            scope.Model.Log = new List<string>();

            this.scope.OnReady += () =>
            {
                this.scope.SwingAway(SwingDirection.Right, true, null);
                this.scope.SwingBack(null);
            };

            this.scope.Model.StartGame = StartGameFn;
            this.scope.Model.DestroyGame = DestroyGameFn;

            this.scope.Model.GameRunning = false;



            this.scope.Watch("model.game", () => { this.scope.Model.UpdateStatus = UpdateStatusType.Dirty; }, true);
            myClientSiteManagerService.OnDeveloperUpdateGameReceived += OnDeveloperUpdateGameReceivedFn;
            this.scope.Model.UpdateStatus = UpdateStatusType.Synced;
            this.scope.Model.UpdateGame = UpdateGameFn;


            clientDebugManagerService.OnGetDebugLog += clientDebugManagerService_OnGetDebugLog;
            clientDebugManagerService.OnGameStarted += (user, roomModel) =>
            {
                this.scope.Model.GameRunning = true;
                this.scope.Model.Room = roomModel;
                if (this.scope.Model.CodeEditor != null)
                    this.scope.Model.CodeEditor.Scope.Model.Room = roomModel;
            };

            this.scope.Model.DebugCode = () =>
                                         {
                                             this.scope.Model.CodeEditor = myCreateUIService.CreateSingleton<DebugGameCodeScope>(DebugGameCodeController.View, (innerScope, elem) =>
                                                                                                                                        {
                                                                                                                                            innerScope.Model = new DebugGameCodeScopeModel();
                                                                                                                                            innerScope.Model.Game = this.scope.Model.Game;
                                                                                                                                            innerScope.Model.Selection = this.scope.Model.Selection;
                                                                                                                                        });
                                         };


            /*
                        Window.SetTimeout(() =>
                                          {
                                              StartGameFn();
                                              scope.Minimize();
                                          }, 250);
            */


        }
        public GameEditorController(GameEditorScope scope,
            ClientSiteManagerService clientSiteManagerService, ClientDebugManagerService clientDebugManagerService,
            MessageService messageService, CreateUIService createUIService)
        {
            myScope = scope;
            myClientSiteManagerService = clientSiteManagerService;
            this.clientDebugManagerService = clientDebugManagerService;
            myMessageService = messageService;
            myCreateUIService = createUIService;
            myScope.Model.OpenCode = OpenCodeFn;
            myScope.Model.OpenEffects = OpenEffectsFn;
            myScope.Model.OpenLayout = OpenLayoutFn;
            myScope.Model.OpenTest = OpenTestFn;
            myScope.Model.Selection = new GameEditorSelectionScopeModel()
                                      {
                                          ShowGrid = true,
                                          ShowCards = true,
                                          SelectedScenarioPieces = new SelectedScenarioPieces()
                                                                   {
                                                                       Piece = SelectedScenarioPieceType.None
                                                                   }
                                      };

            myScope.Visible = false;
            myScope.OnClose += () =>
                               {
                                   //todo destroy spawned
                               };
            myScope.Watch("model.game",
                () => { myScope.Model.UpdateStatus = UpdateStatusType.Dirty; },
                true);
            myClientSiteManagerService.OnDeveloperUpdateGameReceived += OnDeveloperUpdateGameReceivedFn;
            myScope.Model.UpdateStatus = UpdateStatusType.Synced;
            myScope.Model.UpdateGame = UpdateGameFn;
            myScope.OnReady += () =>
                               {
                                   myScope.SwingAway(SwingDirection.TopRight, true, null);
                                   myScope.SwingBack(null);
                               };
/*
            Window.SetTimeout(() =>
                              {
                                  OpenTestFn();
                                  scope.Minimize();
                              }, 100);
*/

        }
        public GameCodeController(GameCodeScope scope,
            ClientManagerService clientManagerService, MessageService messageService)
        {
            Instance = this;
            //scope.Model.
            myScope = scope;
            myClientManagerService = clientManagerService;
            myMessageService = messageService;
            scope.Visible = true;
/*
            scope.Model.CodeMirrorOptions =new {lineNumbers= true,theme="midnight",mode="javascript"   ,         
                onGutterClick= (cm, n) =>{
                var info = cm.lineInfo(n);
                if (info.markerText) {
                    window.shuffUIManager.codeArea.breakPoints.splice(window.shuffUIManager.codeArea.breakPoints.indexOf(n-1), 0);
                    cm.clearMarker(n);
                } else {
                    window.shuffUIManager.codeArea.breakPoints.push(n-1);
                    cm.setMarker(n, "<span style='color: #900'>●</span> %N%");}},extraKeys=new { "Ctrl-Space"= "autocomplete","Ctrl-S"="save" }}
            ;
*/


            scope.Watch("model.game.gameCode.code", () => { });

            myScope.Watch("model.game",
                () => { myScope.Model.UpdateStatus = UpdateStatusType.Dirty; },
                true);

             scope.Model.ForceUpdate = false;
            scope.OnReady += () =>
                             {
                                 scope.Model.ForceUpdate = true;
                                 scope.Apply();
                             };

            myClientManagerService.ClientSiteManagerService.OnDeveloperUpdateGameReceived += OnDeveloperUpdateGameReceivedFn;
            myScope.Model.UpdateStatus = UpdateStatusType.Synced;
            myScope.Model.UpdateGame = UpdateGameFn;
        }
        public GameLayoutEditorController(GameLayoutEditorScope scope,
            ClientSiteManagerService clientSiteManagerService, MessageService messageService,
            CreateUIService createUIService)
        {
            myScope = scope;
            myClientSiteManagerService = clientSiteManagerService;
            myMessageService = messageService;
            myCreateUIService = createUIService;
            myScope.Visible = true;
            myScope.Model.ToggleGrid = ToggleGridFn;
            myScope.Model.ToggleCards = ToggleCardsFn;
            myScope.Model.AddText = AddTextFn;
            myScope.Model.AddArea = AddAreaFn;
            myScope.Model.AddSpace = AddSpaceFn;
            myScope.Model.RemoveArea = RemoveAreaFn;
            myScope.Model.RemoveSpace = RemoveSpaceFn;
            myScope.Model.RemoveText = RemoveTextFn;
            myScope.Model.OpenScenarios = OpenScenariosFn;
            myScope.Model.Selection.SelectedLayoutPiece = SelectedGameLayoutPiece.None;
            myScope.Watch("model.selection.selectedSpace", () =>
                                                           {
                                                               if (myScope.Model.Selection.SelectedSpace == null)
                                                                   return;
                                                               myScope.Model.Selection.SelectedText = null;
                                                               myScope.Model.Selection.SelectedArea = null;
                                                               myScope.Model.Selection.SelectedLayoutPiece = SelectedGameLayoutPiece.Space;
                                                               myScope.Model.Selection.SelectedCard = null;
                                                           });
            myScope.Watch("model.selection.selectedText", () =>
                                                          {
                                                              if (myScope.Model.Selection.SelectedText == null) return;
                                                              myScope.Model.Selection.SelectedSpace = null;
                                                              myScope.Model.Selection.SelectedArea = null;
                                                              myScope.Model.Selection.SelectedLayoutPiece = SelectedGameLayoutPiece.Text;
                                                          });
            myScope.Watch("model.selection.selectedArea", () =>
                                                          {
                                                              if (myScope.Model.Selection.SelectedArea == null) return;
                                                              myScope.Model.Selection.SelectedText = null;
                                                              myScope.Model.Selection.SelectedSpace = null;
                                                              myScope.Model.Selection.SelectedLayoutPiece = SelectedGameLayoutPiece.Area;
                                                          });
            myScope.Watch("model.game", () => { myScope.Model.UpdateStatus = UpdateStatusType.Dirty; }, true);
            myClientSiteManagerService.OnDeveloperUpdateGameReceived += OnDeveloperUpdateGameReceivedFn;
            myScope.Model.UpdateStatus = UpdateStatusType.Synced;
            myScope.Model.UpdateGame = UpdateGameFn;

            var testgame = myCreateUIService.CreateSingleton<TestGameControllerScope>(TestGameController.View, (_scope, elem) =>
                                                                                                            {
                                                                                                                _scope.Model = new TestGameControllerScopeModel();
                                                                                                                _scope.Model.Game = myScope.Model.Game;
                                                                                                                _scope.Model.Selection = myScope.Model.Selection;
                                                                                                            });
            myScope.OnClose += () =>
                               {
                                   testgame.Destroy();
                                   if (scenario != null)
                                   {
                                       scenario.Destroy();
                                   }
                               };

        }
        public GameScenarioEditorController(GameScenarioEditorScope scope,
            ClientSiteManagerService clientSiteManagerService, MessageService messageService,
            CreateUIService createUIService)
        {
            myScope = scope;
            myClientSiteManagerService = clientSiteManagerService;
            myMessageService = messageService;
            myCreateUIService = createUIService;
            myScope.Visible = true;

            myScope.Watch("model.game",
                () => { myScope.Model.UpdateStatus = UpdateStatusType.Dirty; },
                true);

            myScope.Watch("model.selection.selectedScenario", () =>
                                                              {
                                                                  if (myScope.Model.Selection.SelectedScenario == null)
                                                                      return;
                                                                  myScope.Model.Selection.SelectedScenarioEffect = null;
                                                                  myScope.Model.Selection.SelectedScenarioSpace = null;
                                                                  if (myScope.Model.Selection.SelectedSpace == null)
                                                                      return;

                                                                  foreach (
                                                                      var gameLayoutScenarioSpace in
                                                                          myScope.Model.Selection.SelectedScenario
                                                                              .Spaces)
                                                                  {
                                                                      if (gameLayoutScenarioSpace.SpaceGuid ==
                                                                          myScope.Model.Selection.SelectedSpace.Guid)
                                                                      {
                                                                          myScope.Model.Selection.SelectedScenarioSpace
                                                                              = gameLayoutScenarioSpace;
                                                                          break;
                                                                      }
                                                                  }
                                                              });

            myScope.Watch("model.selection.selectedScenarioSpace", () =>
                                                                   {
                                                                       if (
                                                                           myScope.Model.Selection.SelectedScenarioSpace ==
                                                                           null) return;
                                                                       myScope.Model.Selection.SelectedScenarioEffect =
                                                                           null;
                                                                       myScope.Model.Selection.SelectedScenarioPiece =
                                                                           SelectedGameScenarioPiece.Space;
                                                                   });
            myScope.Watch("model.selection.selectedScenarioEffect", () =>
                                                                    {
                                                                        if (
                                                                            myScope.Model.Selection
                                                                                .SelectedScenarioEffect == null) return;
                                                                        myScope.Model.Selection.SelectedScenarioSpace =
                                                                            null;
                                                                        myScope.Model.Selection.SelectedScenarioPiece =
                                                                            SelectedGameScenarioPiece.Effect;
                                                                    });

            myScope.Model.GetSpaceBySpaceGuid = GetSpaceBySpaceGuidFn;
            myScope.Model.GetAreaByAreaGuid = GetAreaByAreaGuidFn;
            myScope.Model.GetTextByTextGuid = GetTextByTextGuidFn;
            myScope.Model.GetCardByCardGuid = GetCardByCardGuidFn;

            myScope.Model.RemoveSpaceFromEffect = RemoveSpaceFromEffectFn;
            myScope.Model.RemoveAreaFromEffect = RemoveAreaFromEffectFn;
            myScope.Model.RemoveCardFromEffect = RemoveCardFromEffectFn;
            myScope.Model.RemoveTextFromEffect = RemoveTextFromEffectFn;

            myScope.Model.GetEffectByScenarioEffect = GetEffectByScenarioEffectFn;
            myScope.Model.AddCardToSpace = AddCardToSpaceFn;
            myScope.Model.RemoveCardFromSpace = RemoveCardFromSpaceFn;
            myScope.Model.AddNewScenario = AddNewScenarioFn;
            myScope.Model.CloneNewScenario = CloneNewScenarioFn;
            myScope.Model.DeleteScenario = DeleteScenarioFn;
            myScope.Model.GetCurrentlySelected = GetCurrentlySelectedFn;
            myScope.Model.ApplyEffectToCurrentlySelected = ApplyEffectToCurrentlySelectedFn;


            myClientSiteManagerService.OnDeveloperUpdateGameReceived += OnDeveloperUpdateGameReceivedFn;
            myScope.Model.UpdateStatus = UpdateStatusType.Synced;
            myScope.Model.UpdateGame = UpdateGameFn;
        }
        public DebugGameCodeController(DebugGameCodeScope scope, ClientManagerService clientManagerService, MessageService messageService)
        {
            Instance = this;
            //scope.Model.
            this.scope = scope;
            myClientManagerService = clientManagerService;
            myMessageService = messageService;
            scope.Visible = true;
            scope.Model.Breakpoints = new List<int>();


            var extraKeys = new JsDictionary<string, string>();
            extraKeys["Ctrl-Space"] = "autocomplete";
            extraKeys["Ctrl-S"] = "save";

            scope.Model.CodeMirrorOptions = new CodeMirrorOptions()
                                            {
                                                LineNumbers = true,
                                                Theme = "midnight",
                                                Mode = "javascript",
                                                Gutters = new[] { "CodeMirror-linenumbers", "breakpoints" },
                                                OnGutterClick = (cm, n, gutter, evt) =>
                                                {
                                                    scope.Model.CodeMirror = cm;

                                                    var info = cm.LineInfo(n);
                                                    if (info.GutterMarkers != null && info.GutterMarkers["breakpoints"] != null)
                                                    {
                                                        scope.Model.Breakpoints.Remove(n + 1);
                                                        cm.SetGutterMarker(n, "breakpoints", null);

                                                    }
                                                    else
                                                    {
                                                        scope.Model.Breakpoints.Add(n+1);

                                                        cm.SetGutterMarker(n, "breakpoints", makeMarker());
                                                    }
                                                    if (scope.Model.Room != null)
                                                    {
                                                        clientManagerService.ClientDebugManagerService.DebugResponse(new DebugResponse(scope.Model.Room.RoomID, scope.Model.Breakpoints, StepType.Continue, false));
                                                    }
                                                },
                                                OnLoad = (editor) =>
                                                        {
                                                            scope.Model.CodeMirror = editor;
                                                        },
                                                ExtraKeys = extraKeys
                                            };

            clientManagerService.ClientDebugManagerService.OnGetDebugBreak += (user, debugBreak) =>
                                                                             {
                                                                                 
                                                                                 for (int i = 0; i < scope.Model.CodeMirror.LineCount(); i++)
                                                                                 {
                                                                                     scope.Model.CodeMirror.RemoveLineClass(i, "background", "codemirror-highlight-line");
                                                                                 }

                                                                                 scope.Model.CodeMirror.AddLineClass(debugBreak.LineNumber-1, "background", "codemirror-highlight-line");
                                                                                 scope.Model.CodeMirror.SetCursor(debugBreak.LineNumber-1, 0);

                                                                                 scope.Model.VariableLookupResult = debugBreak.VariableLookupResult;
                                                                                 scope.Apply();

                                                                             };


            scope.Model.StepInto = () =>
            {
                clientManagerService.ClientDebugManagerService.DebugResponse(new DebugResponse(scope.Model.Room.RoomID, scope.Model.Breakpoints, StepType.Into, true));
            };
            scope.Model.StepOver = () =>
            {
                clientManagerService.ClientDebugManagerService.DebugResponse(new DebugResponse(scope.Model.Room.RoomID, scope.Model.Breakpoints, StepType.Over, true));
            };
            scope.Model.Continue = () =>
            {
                clientManagerService.ClientDebugManagerService.DebugResponse(new DebugResponse(scope.Model.Room.RoomID, scope.Model.Breakpoints, StepType.Continue, true));

            };
            scope.Model.LookupVariable = () =>
            {
                clientManagerService.ClientDebugManagerService.DebugResponse(new DebugResponse(scope.Model.Room.RoomID, scope.Model.Breakpoints, StepType.Lookup, true) { VariableLookup = scope.Model.VariableLookup });
            };
            scope.Watch("model.game.gameCode.code", () => { });

            this.scope.Watch("model.game",
                () => { this.scope.Model.UpdateStatus = UpdateStatusType.Dirty; },
                true);


            scope.Model.ForceUpdate = false;
            scope.OnReady += () =>
                             {
                                 scope.Model.ForceUpdate = true;
                                 scope.Apply();
                             };

            myClientManagerService.ClientSiteManagerService.OnDeveloperUpdateGameReceived += OnDeveloperUpdateGameReceivedFn;
            this.scope.Model.UpdateStatus = UpdateStatusType.Synced;
            this.scope.Model.UpdateGame = UpdateGameFn;
        }