private void button1_Click(object sender, EventArgs e)
        {
            PicasaClient picasa = new PicasaClient();
            var alreadyFoundExt = new HashSet<string>();

            //picasa.Test();

            var foundAlbums = new HashSet<string>();
            var picasaFoundFiles = picasa.GetPhotos(null, ".avi");
            PicasaEntry pe = null;
            foreach (var file in picasaFoundFiles)
            {
                var ph = new PhotoAccessor(file);

                if (ph.PhotoTitle.ToLower()== "2005-12-16Mafia.avi".ToLower())
                {
                    pe = file;
                    var value = ph.Timestamp;
                    var tmp = new DateTime(1970, 1, 1, 0, 0, 0);
                    tmp = tmp.AddMilliseconds(value);

                    break;
                }
            }


            picasa.SetPhotoCreationDate(pe, DateTime.Now);





            MessageBox.Show(pe.Title.ToString());
        }
        public void Organize(List<TreeNode> rootNodes, string drivePhotoDirId, bool driveOrg = true, bool albumOrg = true, bool useDateTag = true)
        {
            LogText("Search for files on local drive");
            localFiles = GetFilesFromNodes(rootNodes);
            if (localFiles.Count ==0)
            {
                LogText("No files found");
                return;
            }

            GoogleDriveClient drive = null;
            if (driveOrg)
                drive = new GoogleDriveClient();
            PicasaClient picasa = null;
            if (albumOrg)
                picasa = new PicasaClient();

            var filesForMoving = new HashSet<string>();
            
            LogText("Search for files already in Google Photos directory on Google Drive. It can take a long time...");
            var googleFilesLst = drive.GetFiles(null, drivePhotoDirId);
            googleFiles = new Dictionary<string, List<File>>();
            foreach (var file in googleFilesLst)
            {
                //Do not need trash files here
                //var x = file.ExplicitlyTrashed;
                if (!googleFiles.ContainsKey(file.OriginalFilename))
                    googleFiles.Add(file.OriginalFilename, new List<File>());
                googleFiles[file.OriginalFilename].Add(file);
            }
            if (googleFilesLst.Count == 0)
            {
                LogText("No files found");
                return;
            }
            else
                LogText("Found " + googleFilesLst.Count + " files");
                
            filesForMoving.UnionWith(googleFiles.Keys);

            //Search for picassa files 
            LogText("Search for exist photo albums");
            var picasaAlbumsLst = picasa.GetAlbums();
            //Album by name 
            picasaAlbumsByName = new Dictionary<string, AlbumAccessor>();
            foreach (var picasaAlbum in picasaAlbumsLst)
            {
                if (!picasaAlbumsByName.ContainsKey(picasaAlbum.AlbumTitle))
                    picasaAlbumsByName.Add(picasaAlbum.AlbumTitle, picasaAlbum);
            }
            LogText("Found " + picasaAlbumsLst.Count + " albums");

            
            picasaFiles = new Dictionary<string, List<PicasaEntry>>();
            picasaFilesId = new HashSet<string>();
            googleDirs = new Dictionary<string, string>();
            preferableDirs = new Dictionary<string, Tuple<string, string>>();


            /*
            LogText("Search for media files in Picasa api");
            var alreadyFoundExt = new HashSet<string>();
            
            foreach (var localFile in localFiles.Keys)
            {
                var ext = System.IO.Path.GetExtension(localFile).ToLower();
                if (alreadyFoundExt.Contains(ext) || !videoExt.Contains(ext))
                    continue;
                alreadyFoundExt.Add(ext);
                var foundAlbums = new HashSet<string>();
                var picasaFoundFiles = picasa.GetPhotos(null, ext);
                foreach (var file in picasaFoundFiles)
                {
                    AddPicasaFile(file, picasaFiles, picasaFilesId);
                    var picasaFoto = new PhotoAccessor(file);
                    foundAlbums.Add(picasaFoto.AlbumId);
                }
                //Also try to add any files from album of found files
                foreach (var albumId in foundAlbums)
                    AddPicasaFiles(picasa, picasaFiles, picasaFilesId, albumId);
            }
            LogText("Found " + picasaFiles.Count + " files");*/

            //filesForMoving.UnionWith(picasaFiles.Keys);

            //System.IO.File.WriteAllLines("localFiles", localFiles.Keys.ToList());

            LogText("MOVING FILES...");
            ResetProgress(filesForMoving.Count);
            bool hasError = false;

            var opt = new ParallelOptions() { MaxDegreeOfParallelism = 10 };
            //foreach (var googleFilePair in googleFiles)
            Parallel.ForEach(filesForMoving, opt, (googleFilePair) =>
            {
                if (!localFiles.ContainsKey(googleFilePair))
                {
                    IncreaseProgress();
                    return;
                }

                if (hasError)
                    return;

                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        MoveFile(googleFilePair, drivePhotoDirId);
                        if (i != 0)
                            LogText("Try succeeded.");
                        break;
                    }
                    catch (Exception ex)
                    {
                        if (i < 2)
                        {
                            LogText("Error moving " + googleFilePair + ". Try again...");
                            if (i==1)
                            {
                                lock (picasaFiles)
                                {
                                    //Sometimes picassa and all already found files creditals coul be expired
                                    //Will delete them all and reinit as usual.
                                    picasaFiles.Clear();
                                    picasaFilesId.Clear();
                                }
                            }
                        }
                        else
                        {
                            hasError = true;
                            throw ex;
                        }
                    }
                }
                IncreaseProgress();
            });
            LogText("\r\nREADY");
            ResetProgress(100);
        }
        public void PicasaDeleteAll(string localPath)
        {
            var picasa = new PicasaClient();

            LogText("GetAlbums...");

            var albums = picasa.GetAlbums();
            ResetProgress(albums.Count);
            LogText("Delete from albums...");
            foreach (var album in albums)
            {
                var picFiles = picasa.GetPhotos(album.Id);
                foreach (var picFile in picFiles)
                {
                    picasa.DeletePhoto(picFile);
                }
                IncreaseProgress();
            }

            LogText("Get all files in " + localPath);
            var files = System.IO.Directory.GetFiles(localPath, "*", System.IO.SearchOption.AllDirectories);
            ResetProgress(files.Length);
            var searched = new HashSet<string>();
            
            foreach (var localFile in files)
            {
                var fName = System.IO.Path.GetFileName(localFile);
                if (searched.Contains(fName))
                    continue;
                searched.Add(fName);

                var filesForDel = picasa.GetPhotos(null, fName);
                foreach (var picFile in filesForDel)
                {
                    picasa.DeletePhoto(picFile);
                }
                IncreaseProgress();
            }
            ResetProgress(10);
            LogText("READY");
        }
 void AddPicasaFiles(PicasaClient picasa, 
     Dictionary<string, List<PicasaEntry>> picasaFiles, HashSet<string> picasaFilesId, 
     string albumId = null, string searchText = null)
 {
     var pf = picasa.GetPhotos(albumId, searchText);
     foreach (var entry in pf)
         AddPicasaFile(entry, picasaFiles, picasaFilesId);            
 }