internal void SignIn(string userEmailId, string masterPassword)
        {
            logger.Debug("SignIn for user {0} ", userEmailId);
            try
            {
                if (string.IsNullOrWhiteSpace(masterPassword))
                {
                    logger.Error("MasterPassword is empty");
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        //MessageBox.Show("Username or Password is incorrect");
                        ErrorMessage = (string)System.Windows.Application.Current.FindResource("IncorrectMailOrPasswordMessage");
                        //UserEmail = String.Empty;
                    });
                    return;
                }
                var correctEmail = userEmailId != null && (_commonObj.IsEmailValid(userEmailId));
                if (!correctEmail)
                {
                    logger.Error("Incorrect email - {0}", userEmailId);
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        //MessageBox.Show("Incorrect Email Format.");
                        ErrorMessage = (string)System.Windows.Application.Current.FindResource("IncorrectMailFormatMessage");
                        //UserEmail = String.Empty;
                    });
                    return;
                }

                bool profileExists = false;
                bool hasDeviceID   = false;

                logger.Debug("Authenticate user start");
                var isAuthenticated = pbData.AuthenticateUser(userEmailId, masterPassword, out profileExists);
                logger.Debug("Authenticate user end. IsAuthenticated - {0}", isAuthenticated);
                if (isAuthenticated)
                {
                    pbData.OpenProfile(UserEmail, masterPassword, out profileExists);
                    _masterPass  = masterPassword;
                    UserPassword = string.Empty;

                    if (profileExists)
                    {
                        logger.Debug("Proifle exists");
                        hasDeviceID = pbData.GetDevice(pbData.InstallationUUID) != null;
                        logger.Debug("HasDeviceId = {0}", hasDeviceID);
                    }

                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        pbData.ActivatePinIfEnabled(masterPassword, DefaultProperties.Configuration_Key_EnablePinAccess);
                    });

                    Task.Factory.StartNew(() => Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        Task.Factory.StartNew(() =>
                        {
                            logger.Debug("SyncImagesHelper called in async");
                            new SyncImagesHelper(pbData, webApi).SyncImages();
                        });
                    }));
                }
                else
                {
                    _masterPass = null;
                    if (profileExists)
                    {
                        logger.Error("ProfileExists");
                        Application.Current.Dispatcher.Invoke((Action) delegate
                        {
                            //MessageBox.Show("Username or Password is incorrect");
                            ErrorMessage = (string)System.Windows.Application.Current.FindResource("IncorrectMailOrPasswordMessage");
                            //UserEmail = String.Empty;
                        });
                        return;
                    }
                }

                logger.Debug("Calling tryToGetDeviceID");
                var tryToGetDeviceId = TryToGetDeviceID(userEmailId, masterPassword, profileExists, hasDeviceID);
                logger.Debug("TryToGetDeviceId result - {0}", tryToGetDeviceId);

                if (!tryToGetDeviceId)
                {
                    logger.Debug("Calling TryToShowMainWindow");
                    var tryToShowMainWindowResult = TryToShowMainWindow(isAuthenticated);
                    logger.Debug("TryToShowMainWindow result - {0}", tryToShowMainWindowResult);
                    if (!tryToShowMainWindowResult)
                    {
                        logger.Error("Unable to show main window");
                        Application.Current.Dispatcher.Invoke((Action) delegate
                        {
                            //MessageBox.Show("Username or Password is incorrect");
                            ErrorMessage = (string)System.Windows.Application.Current.FindResource("IncorrectMailOrPasswordMessage");
                            //UserEmail = String.Empty;
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show((string)System.Windows.Application.Current.FindResource("GeneralErrorText"));
                logger.Error(ex.Message);
            }
            logger.Debug("SignIn finished");
        }