﻿namespace GoogleDrive.Api
{
    using System;
    using System.Collections.Generic;

    using Google.Apis.Drive.v2;
    using Google.Apis.Drive.v2.Data;

    public class Program
    {
        // Connect with Oauth2 Ask user for permission
        private const string ClientId = "1099113978558-vbvbnjt20ctsue037l2gjucjmi3nc6p2.apps.googleusercontent.com";
        private const string ClientSecret = "pJm_H5UMEsuA1CSofUq8jbAB";

        private const string AuthenticationErrorMessage = "Authentication error";

        private const string RootDirectory = "root";

        public static void Main()
        {
            DriveService service = AuthManager.AuthenticateOauth(ClientId, ClientSecret, "Zeus Chat System");

            if (service == null)
            {
                Console.WriteLine(AuthenticationErrorMessage);
                Console.ReadLine();
            }

            try
            {
                string query = "title = 'Zeus Chat System' and mimeType = 'application/vnd.google-apps.folder'";
                IList<File> files = FileManager.RetrieveAllFiles(service, query);

                // If there isn't a directory with this name, create one.
                if (files.Count == 0)
                {
                    files.Add(FileManager.CreateFolder(service, "Zeus Chat System", "Zeus Chat System Files", RootDirectory));
                }
                else
                {
                    string directoryId = files[0].Id;

                    // Upload test file
                    File newFile = FileManager.UploadFile(service, @"../../batman.jpg", directoryId);
                    Console.WriteLine("The file was successfully uploaded");

                    // Download test file
                    FileManager.DownloadFile(service, newFile, @"../../downloadTest.jpg");
                    Console.WriteLine("The file was successfully downloaded");
                }

                // Retrieving a list of all users files
                query = string.Format("'{0}' in parents", files[0].Id);
                files = FileManager.RetrieveAllFiles(service, query);

                foreach (File file in files)
                {
                    Console.WriteLine(file.Title + " " + file.MimeType);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }
    }
}
