public async Task <DateTime> ExecuteAsync(DateTime lastSync)
        {
            var maxDate = DateTime.UtcNow;

            var entries = await _tableStorageService.GetEntries(lastSync, maxDate);

            if (!entries.Any())
            {
                return(maxDate);
            }

            foreach (var settingsEntity in entries)
            {
                Console.WriteLine($"Downloading: {settingsEntity.RowKey}");

                //Prepare destination folder
                var currentSyncDir = $@"{_downloadDir}{maxDate.ToLocalTime():yyyy-MM-dd}\{settingsEntity.PartitionKey}\";
                if (!Directory.Exists(currentSyncDir))
                {
                    Directory.CreateDirectory(currentSyncDir);
                }

                //Download file
                await _blobStorageService.DownloadFile(currentSyncDir, settingsEntity.SettingFileName);

                //Rename file
                var splitedFileName = settingsEntity.SettingFileName.Split('.');
                var expectedName    = Path.Combine(currentSyncDir, settingsEntity.SettingFileName);
                var destinationName = Path.Combine(currentSyncDir, $"{splitedFileName[1]}.{splitedFileName[2]}");
                File.Move(expectedName, destinationName);
            }

            return(maxDate);
        }
        private void DownloadNewGames()
        {
            var service = new BlobStorageService(_mainViewModel.Connection);

            foreach (var game in remoteGames)
            {
                if (!GameList.Any(p => p.Model.FileName.Equals(game)))
                {
                    service.DownloadFile(game, $"{_mainViewModel.DataDirectory}\\{game}");
                }
            }
        }
        public static Email ReadEmail(string name = "", string cookieName = "set_emails")
        {
            if (string.IsNullOrEmpty(name))
            {
                name = ServicesCollection.Current.Resolve <CookiesService>().GetCookie(cookieName);
            }

            string tempFilePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.json");

            _blobStorageService.DownloadFile(name, tempFilePath, "emails");
            string fileContent = new FileFacade().ReadAllText(tempFilePath);
            var    email       = new JsonSerializer().Deserialize <Email>(fileContent);

            return(email);
        }
        private void SyncDifferences(GameType gameType)
        {
            var service = new BlobStorageService(_mainViewModel.Connection);


            List <string> keys = remoteFiles.Keys.Union(files.Keys).ToList();

            foreach (string key in keys)
            {
                var gameViewModel = _gameList.FirstOrDefault(p => p.Model.FileName == key && p.Model.GameType == gameType.Extension);

                if (gameViewModel == null)
                {
                    continue;
                }
                var game = gameViewModel.Model;

                if (!game.Players.Contains(_mainViewModel.PlayerName))
                {
                    continue;
                }

                var gameName = $"{game.Name}.json";
                var gamePath = $"{_mainViewModel.DataDirectory}\\{gameName}";
                var gameMd5  = GetFileHash(gamePath);

                var gameStatus = GameStatus.DoNothing;

                FileInfoModel remote = null;
                FileInfoModel local  = null;

                if (remoteFiles.ContainsKey(key))
                {
                    remote = remoteFiles[key];
                }

                if (files.ContainsKey(key))
                {
                    local = files[key];
                }

                if (remote == null)
                {
                    var bytes     = File.ReadAllBytes(local.FullPath);
                    var bytesGame = File.ReadAllBytes(gamePath);
                    service.UploadFileToBlob(key, local.Md5, bytes, "application/zip");
                    files[key].FileStatus = FileStatus.Uploaded;
                    remoteFiles.TryAdd(key, local);
                    gameStatus = GameStatus.Upload;
                }
                else if (local != null && remote.LastModified < local.LastModified && remote.Md5 != local.Md5)
                {
                    var bytes = File.ReadAllBytes(local.FullPath);
                    service.UploadFileToBlob(key, local.Md5, bytes, "application/zip");
                    files[key].FileStatus = FileStatus.Uploaded;
                    remoteFiles[key]      = local;
                    gameStatus            = GameStatus.Upload;
                }

                if (local == null)
                {
                    var fileinfoModel = service.DownloadFile(key, $"{gameType.Savegames}\\{key}");
                    files.TryAdd(key, fileinfoModel);
                    files[key].FileStatus = FileStatus.New;
                    gameStatus            = GameStatus.Download;
                }
                else if (local == null || (remote != null && local.LastModified < remote.LastModified) && remote.Md5 != local.Md5)
                {
                    var fileinfoModel = service.DownloadFile(key, $"{gameType.Savegames}\\{key}");
                    files[key]            = fileinfoModel;
                    files[key].FileStatus = FileStatus.Downloaded;
                    gameStatus            = GameStatus.Download;
                }

                if (local?.Md5 == remote?.Md5)
                {
                    files[key].FileStatus = FileStatus.NotChanged;
                }

                if (gameStatus == GameStatus.Download)
                {
                    service.DownloadFile(gameName, gamePath);
                }
                else if (gameStatus == GameStatus.Upload)
                {
                    game.LastPlayer   = _mainViewModel.PlayerName;
                    game.GameType     = gameType.Extension;
                    game.LastTurnTime = DateTime.UtcNow;
                    if (game.CurrentTurn.HasValue)
                    {
                        if (game.Players[0].Equals(_mainViewModel.PlayerName))
                        {
                            game.CurrentTurn++;
                        }
                    }
                    else
                    {
                        game.CurrentTurn = 1;
                    }
                    var jsonObject = JsonConvert.SerializeObject(game);
                    File.WriteAllText(gamePath, jsonObject);
                    var hash  = GetFileHash(gamePath);
                    var bytes = File.ReadAllBytes(gamePath);
                    service.UploadFileToBlob(gameName, hash, bytes, "application/json");
                }
            }
        }