using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;

namespace EnvigoHiring.Models.Login
{
    public class GoogleLogin
    {
        public string clientid = "473716435734-bvubpa7ggdgus63255ku8mqh9buq7eki.apps.googleusercontent.com";
        public string clientsecret = "30RPoRXZTZVCq6cqs9yWXg8W";
        public string redirection_url = ConfigurationManager.AppSettings["SiteUrl"].ToString().Trim() + "login";
        public string url = "https://accounts.google.com/o/oauth2/token";
        public Tokenclass GetToken(string code)
        {
            string poststring = "grant_type=authorization_code&code=" + code + "&client_id=" + clientid + "&client_secret=" + clientsecret + "&redirect_uri=" + redirection_url + "";
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            UTF8Encoding utfenc = new UTF8Encoding();
            byte[] bytes = utfenc.GetBytes(poststring);
            Stream outputstream = null;
            try
            {
                request.ContentLength = bytes.Length;
                outputstream = request.GetRequestStream();
                outputstream.Write(bytes, 0, bytes.Length);
            }
            catch
            { }
            var response = (HttpWebResponse)request.GetResponse();
            var streamReader = new StreamReader(response.GetResponseStream());
            string responseFromServer = streamReader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            Tokenclass obj = js.Deserialize<Tokenclass>(responseFromServer);
            return obj;
            //UserProfile(obj.access_token);
        }


        public Userclass UserProfile(string code)
        {
            var token = GetToken(code);
            string url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=" + token.access_token + "";
            WebRequest request = WebRequest.Create(url);
            request.Credentials = CredentialCache.DefaultNetworkCredentials;
            WebResponse response = request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            response.Close();
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Deserialize<Userclass>(responseFromServer);
        }

    }
}

public class Tokenclass
{
    public string access_token { get; set; }
    public string token_type { get; set; }
    public int expires_in { get; set; }
    public string refresh_token { get; set; }
}
public class Userclass
{
    public string id { get; set; }
    public string name { get; set; }
    public string given_name { get; set; }
    public string family_name { get; set; }
    public string link { get; set; }
    public string picture { get; set; }
    public string gender { get; set; }
    public string locale { get; set; }
    public string Email { get; set; }
}