private void TestAccount(TraktAuthentication account)
        {
            TraktUserToken response = null;
            if (NewAccount)
            {
                // No longer supported with v2 API.
                //if (lblTestConnect != null)
                //    GUIControl.SetControlLabel(GetID, lblTestConnect.GetID, Translation.CreatingAccount);

                //GUIWindowManager.Process();
                //response = TraktAPI.v1.TraktAPI.CreateAccount(account);
            }
            else
            {
                if (lblTestConnect != null)
                    GUIControl.SetControlLabel(GetID, lblTestConnect.GetID, Translation.SigningIntoAccount);

                GUIWindowManager.Process();
                response = TraktAPI.TraktAPI.Login(account.ToJSON());
            }

            if (response == null || string.IsNullOrEmpty(response.Token))
            {
                GUIUtils.ShowNotifyDialog(Translation.Error, Translation.FailedLogin);
                if (lblTestConnect != null)
                    GUIControl.SetControlLabel(GetID, lblTestConnect.GetID, string.Empty);
            }
            else
            {
                // Save User Token
                TraktAPI.TraktAPI.UserToken = response.Token;

                // Save New Account Settings
                TraktSettings.Username = account.Username;
                TraktSettings.Password = account.Password;
                if (!TraktSettings.UserLogins.Exists(u => u.Username == TraktSettings.Username))
                {
                    TraktSettings.UserLogins.Add(new TraktAuthentication { Username = TraktSettings.Username, Password = TraktSettings.Password });
                }
                TraktSettings.AccountStatus = ConnectionState.Connected;
                HideAccountControls();
                InitProperties();

                // clear caches
                // watchlists are stored by user so dont need clearing.
                GUINetwork.ClearCache();
                GUICalendar.ClearCache();
                GUIRecommendationsMovies.ClearCache();
                GUIRecommendationsShows.ClearCache();

                // clear any stored user data
                TraktCache.ClearSyncCache();
            }
        }
        protected override void OnClicked(int controlId, GUIControl control, Action.ActionType actionType)
        {
            // wait for any background action to finish
            if (GUIBackgroundTask.Instance.IsBusy) return;

            switch (controlId)
            {
                // Disconnect
                case ((int)SkinControls.Disconnect):
                    DisconnectAccount();
                    break;

                case ((int)SkinControls.Create):
                    ShowAccountControls(true);
                    break;

                case ((int)SkinControls.Login):
                    bool autoLogin = ShowLoginMenu();
                    ShowAccountControls(false);
                    if (autoLogin)
                    {
                        GUIControl.SetControlLabel(GetID, btnUsername.GetID, this.Username);
                        GUIControl.SetControlLabel(GetID, btnPassword.GetID, GetMaskedPassword(this.Password));
                        var account = new TraktAuthentication
                        {
                            Username = this.Username,
                            Password = this.Password
                        };
                        TestAccount(account);
                    }
                    break;

                case ((int)SkinControls.Username):
                    string username = Username;
                    if (GUIUtils.GetStringFromKeyboard(ref username))
                    {
                        GUIUtils.SetProperty("#Trakt.Settings.Account.Dialog.HasUsername", "true");
                        if (!username.Equals(Username))
                        {
                            Username = username;
                            GUIControl.SetControlLabel(GetID, btnUsername.GetID, username);
                        }
                    }
                    break;

                case ((int)SkinControls.Password):
                    string password = Password;
                    if (GUIUtils.GetStringFromKeyboard(ref password, true))
                    {
                        GUIUtils.SetProperty("#Trakt.Settings.Account.Dialog.HasPassword", "true");
                        if (!password.Equals(Password))
                        {
                            Password = password;
                            GUIControl.SetControlLabel(GetID, btnPassword.GetID, GetMaskedPassword(password));
                        }
                    }
                    break;

                case ((int)SkinControls.EmailButton):
                    string email = Email;
                    if (GUIUtils.GetStringFromKeyboard(ref email))
                    {
                        if (!email.Equals(Email))
                        {
                            Email = email;
                            GUIControl.SetControlLabel(GetID, btnEmail.GetID, email);
                        }
                    }
                    break;

                case ((int)SkinControls.Ok):
                    if (ValidateFields())
                    {
                        var account = new TraktAuthentication
                        {
                            Username = this.Username,
                            Password = this.Password,
                            //Email = this.Email
                        };
                        TestAccount(account);
                    }
                    break;

                case ((int)SkinControls.Cancel):
                    HideAccountControls();
                    break;

                default:
                    break;
            }
            base.OnClicked(controlId, control, actionType);
        }