using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Web;

namespace EnvigoHiring.Models.Calendar
{
    public class Calendar
    {
        static string[] Scopes = { "https://www.googleapis.com/auth/calendar.events", "https://www.googleapis.com/auth/calendar", "https://www.googleapis.com/auth/calendar.readonly" };
        static string ApplicationName = "Envigo Recruiter Login";
        public void Method(string email, string text)
        {
            ServiceAccountCredential credentials = GoogleCredential.FromFile(HttpContext.Current.Server.MapPath("client-service.json")).CreateScoped(Scopes).UnderlyingCredential as ServiceAccountCredential;
            if (credentials.RequestAccessTokenAsync(CancellationToken.None).Result)
            {
                
            }
            //UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            //                new ClientSecrets
            //                {
            //                    ClientId = "261213523242-fjma3ocn8aes9dda9grlu15jpmkti799.apps.googleusercontent.com",
            //                    ClientSecret = "c2btFMstqHWoVi2qi7QgdrpQ",
            //                },
            //                new[] { CalendarService.Scope.Calendar },
            //                "user",
            //                CancellationToken.None).Result;

            // Create the service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credentials,
                ApplicationName = "Calendar API Sample",
            });
            //UserCredential credential;


            //using (var stream =
            //    new FileStream(HttpContext.Current.Server.MapPath("credentials.json"), FileMode.Open, FileAccess.Read))
            //{
            //    // The file token.json stores the user's access and refresh tokens, and is created
            //    // automatically when the authorization flow completes for the first time.
            //    string credPath = "token.json";
            //    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            //        clie
            //        GoogleClientSecrets.Load(stream).Secrets,
            //        Scopes,
            //        "user",
            //        CancellationToken.None,
            //        new FileDataStore(HttpContext.Current.Server.MapPath(credPath), true)).Result;
            //}

            // Create Google Calendar API service.

            //var service = new CalendarService();

            Event myEvent = new Event
            {
                Summary = "Interview Schedule",
                Location = "Envigo Office",
                Description = "Interview Scheduled with Ajeet",
                
                Start = new EventDateTime()
                {
                    DateTime = new DateTime(2019, 8, 27, 10, 5, 3),
                    TimeZone = "Asia/Kolkata"
                },
                End = new EventDateTime()
                {
                    DateTime = new DateTime(2019, 8, 27, 10, 35, 4),
                    TimeZone = "Asia/Kolkata"
                },
                //              Recurrence = new String[] {
                //    "RRULE:FREQ=WEEKLY;BYDAY=MO"
                //},
                Attendees = new List<EventAttendee>()
      {
        new EventAttendee() { Email = "vivek@envigo.co.uk" }
      }
            };
            Event recurringEvent = service.Events.Insert(myEvent, "primary").Execute();

        }


    }
    
}