using Google.Authenticator;
using System;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using CLS_BL;
using CLS_DL;
using CLS_PL;
using QRCoder;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace SUNWEB
{
    public partial class ShowgoogleAuth : System.Web.UI.Page
    {
        MYSQLDB ObjMain = new MYSQLDB();
        int L = 0;
        string Curr_Date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        protected void Page_Load(object sender, EventArgs e)
        {
            /*

            string googleAuthKey = ObjMain.ExecuteScalar(" select googleauthLogic from  portaldb.googleauthdetails where userid=" + Session["UserId"].ToString() + ";");
           // string googleAuthKey = WebConfigurationManager.AppSettings["GoogleAuthKey"];
            //Two Factor Authentication Setup
            TwoFactorAuthenticator TwoFacAuth = new TwoFactorAuthenticator();
            var setupInfo = TwoFacAuth.GenerateSetupCode("suntoyocloud.com", "Admin", ConvertSecretToBytes(googleAuthKey, false), 100);

            imgqrcode.ImageUrl = setupInfo.QrCodeSetupImageUrl;
            lblSetupCode.Text = setupInfo.ManualEntryKey;
            */
            imgqrcode.Visible = false;
            lblSetupCode.Visible = false;
            lblvalue.Visible = false;

           
        }

        private static byte[] ConvertSecretToBytes(string secret, bool secretIsBase32) =>
          secretIsBase32 ? Base32Encoding.ToBytes(secret) : Encoding.UTF8.GetBytes(secret);



        public bool ValidateToken(string token)
        {


            // Instead of getting it Webcofig , get in from database ..
            // string googleAuthKey = WebConfigurationManager.AppSettings["GoogleAuthKey"];

            string googleAuthKey = ObjMain.ExecuteScalar(" select googleauthLogic from  portaldb.googleauthdetails where userid="+ Session["UserId"].ToString() + ";");

            Log.Info("Googleauthkey:" + googleAuthKey);
            // Adjust the validation window to allow a time skew of 1 step (30 seconds)
            int validationWindow = 1;
            TwoFactorAuthenticator TwoFacAuth = new TwoFactorAuthenticator();
            return TwoFacAuth.ValidateTwoFactorPIN(googleAuthKey, token, validationWindow);


        }



        public void AuthenticateToken(string _token)
        {
           
            bool isValid = ValidateToken(_token);
            Log.Info("TokenValue - " + _token);
            Log.Info("IsValid - " + isValid);
            if (isValid)
            {
                
                Response.Redirect("Dashboard.aspx", false);


            }
            else
            {
                UPTActiveUserid(Session["UserId"].ToString());
                Response.Redirect("Login.aspx", false);
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            AuthenticateToken(txtEnteredCode.Text.ToString().Trim());
        }

        public void UPTActiveUserid(string UserId)
        {
            string ActiveVal = "0";
            L = ObjMain.ExecuteNonQuery("Update portaldb.Usermaster set  Active=" + ActiveVal + ", UPTTIME='" + Curr_Date + "' where   Userid=" + UserId + " ");

        }

    }
}