/*  static void Main(string[] args)
        {
            var user = new SampleUser
            {
                ControlPlane = "sharefile.com",
                Username = "******",
                Password = "******",
                Subdomain = "isoa2015"
            };

            string oauthClientId = "fZYqiGVX1ZwFZvSWn2UqjZNtOT1uSyWT";
            string oauthClientSecret = "7sKJVwIpcIGYHWBLeXnSNcRIXE2al2AgVGihKEgdq0W4CZpJ";

            if (string.IsNullOrEmpty(oauthClientId) || string.IsNullOrEmpty(oauthClientSecret))
            {
                Console.WriteLine("You must provide oauthClientId and oauthClientSecret");
                return;
            }

            if (string.IsNullOrEmpty(user.Username) || string.IsNullOrEmpty(user.Password) ||
                string.IsNullOrEmpty(user.Subdomain))
            {
                Console.WriteLine("You must provide username, password and subdomain");
                return;
            }

            RunSample(user, oauthClientId, oauthClientSecret).Wait();
        }*/
        

        public static async Task RunSample(SampleUser user, string clientId, string clientSecret)
        {
            // Authenticate with username/password
            ShareFileClient sfClient = await PasswordAuthentication(user, clientId, clientSecret);

            // Create a Session
            await StartSession(sfClient);

            var allSharedAlias = sfClient.Items.GetAlias("allshared");
            var sharedFolders = sfClient.Items.GetChildren(allSharedAlias).Execute();

            Console.WriteLine("Loaded - " + allSharedAlias.ToString());
            Console.WriteLine("Loaded - " + sharedFolders.ToString());

          

            // Load Folder and Contents
            var defaultUserFolder = await LoadFolderAndChildren(sfClient);
            Console.WriteLine("Loaded - " + defaultUserFolder.Name);
            Console.WriteLine("Loaded - " + defaultUserFolder.ToString());

            // Create a Folder
            var createdFolder = await CreateFolder(sfClient, defaultUserFolder);
            Console.WriteLine("Created a new folder - " + createdFolder.Name);
            Console.WriteLine("Loaded - " + createdFolder.ToString());

            // Upload a file
            var uploadedFileId = await Upload(sfClient, createdFolder);
            var itemUri = sfClient.Items.GetAlias(uploadedFileId);
            var uploadedFile = await sfClient.Items.Get(itemUri).ExecuteAsync();
            Console.WriteLine("Uploaded - " + uploadedFile.Name);
            Console.WriteLine("Loaded - " + uploadedFileId.ToString());
            Console.WriteLine("alias - " + itemUri.ToString());
            Console.WriteLine("uploadedfile - " + uploadedFile.ToString());

            Console.ReadKey();
            // Download a file
          /*  await Download(sfClient, uploadedFile);
            Console.WriteLine("Downloaded - " + uploadedFile.Name);

            // Share a file using a Link
            /*var share = await ShareViaLink(sfClient, uploadedFile);
            Console.WriteLine("Successfully created a share, it be be accessed using: " + share.Uri);

            // Share a file via ShareFile
            string recipientEmailAddress = "[EnterEmailAddress]";
            await ShareViaShareFileEmail(sfClient, uploadedFile, recipientEmailAddress);*/

           // Console.ReadKey();
        }
        public static async Task<ShareFileClient> PasswordAuthentication(SampleUser user, string clientId, string clientSecret)
        {
            // Initialize ShareFileClient.
            var configuration = Configuration.Default();
            configuration.Logger = new DefaultLoggingProvider();

            var sfClient = new ShareFileClient("https://secure.sf-api.com/sf/v3/", configuration);
            var oauthService = new OAuthService(sfClient, clientId, clientSecret);

            // Perform a password grant request.  Will give us an OAuthToken
            var oauthToken = await oauthService.PasswordGrantAsync(user.Username, user.Password, user.Subdomain, user.ControlPlane);

            // Add credentials and update sfClient with new BaseUri
            sfClient.AddOAuthCredentials(oauthToken);
            sfClient.BaseUri = oauthToken.GetUri();

            return sfClient;
        }