using ASPSnippets.GoogleAPI;
using EnvigoHiring.Models.Login;
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace EnvigoHiring.Controllers
{
    public class loginController : Controller
    {
        // GET: login
        public ActionResult Index(string code,string ReturnUrl)
        {
            LoginModel lm = new LoginModel();
            //lm.IsAntiForgery = false;
            if (code != null && code!="")
            {
                //lm.IsAntiForgery = true;
                ReturnUrl = (ReturnUrl == null) ? "" : ReturnUrl;
               
                lm.GetUserprofile(code,ReturnUrl);
                string redirectUrl = (ReturnUrl == null || ReturnUrl == "") ? "/dashboard" : ReturnUrl;
                return Redirect(redirectUrl);
            }

            return View(lm);
        }
        [HttpPost]
        public ActionResult Index(LoginModel lm, string btnSubmit)
        {
            if (ModelState.IsValid)
            {
                if (btnSubmit == "Login With Gmail")
                {
                    lm.IsAntiForgery = true;
                    GoogleConnect.Authorize("profile", "email");
                }
            }
            return View(lm);
        }
        public ActionResult thankyou()
        {
            string emails = CommonFunctions.getadminemailid(1);
            if (emails.Length > 0)
            {
                string FileName = AppDomain.CurrentDomain.BaseDirectory + "content\\mailer.html";
                System.IO.StreamReader ObjStrmRdr;
                ObjStrmRdr = System.IO.File.OpenText(FileName);
                string StrMail = ObjStrmRdr.ReadToEnd();
                StrMail = StrMail.Replace("$$Heading$$", "New User Sign-In!");
                StrMail = StrMail.Replace("$$Content$$", Request.QueryString["email"] + " has been signed up on Envigo Hiring application, Please review and assign a role.");
                ObjStrmRdr.Close();
                CommonFunctions.Email_Send("New Signin Request - Envigo Hiring", emails, StrMail);
            }
            //Email_Send
            return View();
        }
        public ActionResult logout()
        {
            try
            {
                FormsAuthentication.SignOut();
                GoogleConnect.Clear();
                return RedirectToAction("index", "login");
            }
            catch
            {
                throw;
            }
        }
    }
}