using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Threading;
public partial class System_Settings_SystemSetting_ViewLicenseInfo : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    string source_Language;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objClassCommon = new cls_Common();
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        CultureInfo ci;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        Session["Direction"] = rm.GetString("Direction", ci);
        LoadStrings(ci);
        //Call to a function to implement the UserRights
        UserRights();
        if (!IsPostBack)
        {

            objClassCommon.menu("LicenseManager", "199");

            string[] fileArr = Directory.GetFiles(Server.MapPath("..\\LicenseFile"), "*.lic");
            int arrCount = fileArr.Length;
            if (arrCount > 0)
            {
                string licenseFileName = fileArr[arrCount - 1].ToString();
                //File Stream pointing to License File,if exist
                FileStream licenseFileStream = new FileStream(licenseFileName, FileMode.Open);
                //Stream Reader pointing to License  File
                StreamReader licenseFileStreamReader = new StreamReader(licenseFileStream);
                // read license request file information from .lrf file
                string encryptedLicFileInfo = licenseFileStreamReader.ReadToEnd();
                //Close the Streams
                licenseFileStreamReader.Close();
                licenseFileStream.Close();
                string decryptedLicFileInfo = string.Empty;
                try
                {
                    decryptedLicFileInfo = Encryption.Decrypt(encryptedLicFileInfo, "");
                }
                catch
                {
                    string strScript = "<script language='javascript' type='text/javascript'>";
                    strScript += "alert('" + rm.GetString("Alert_Licenseinformationarenotcorrect", ci) + "');";
                    strScript += " </script>";
                    this.RegisterStartupScript("AlertLicInfoNotCorrect", strScript);
                    return;
                }
                string[] decryptedLicFileInfoArr = decryptedLicFileInfo.Split('#');
                string basicLicInfo = decryptedLicFileInfoArr[0].ToString();
                string[] basicLicInfoArr = basicLicInfo.Split(',');
                string customerIdLicFile = basicLicInfoArr[0].ToString();
                string packageIdLicFile = basicLicInfoArr[1].ToString();
                string packageVersionLicFile = basicLicInfoArr[2].ToString();
                string noOfConcurrentUserLicFile = basicLicInfoArr[3].ToString();
                string noOfEmployeeLicFile = basicLicInfoArr[4].ToString();
                string macAddressLicFile = basicLicInfoArr[5].ToString();
                string licenseGeneartionDateLicFile = basicLicInfoArr[6].ToString();
                string expiryDateLicFile = basicLicInfoArr[7].ToString();
                string noOfTerminalLicFile = basicLicInfoArr[8].ToString();
                DateTime dtexpiryDateLicFile = Convert.ToDateTime(expiryDateLicFile);
                string moduleLicFile = decryptedLicFileInfoArr[1].ToString();
                string module1LicFile = decryptedLicFileInfoArr[2].ToString();
                string module2LicFile = decryptedLicFileInfoArr[3].ToString();
                string module3LicFile = decryptedLicFileInfoArr[4].ToString();
                string module4LicFile = decryptedLicFileInfoArr[5].ToString();
                string module5LicFile = decryptedLicFileInfoArr[6].ToString();
                // get local machine's MACAddress
                NodeLocking nodeLocking = new NodeLocking();
                string localMACAdderss = nodeLocking.VerifyMACAddress(macAddressLicFile);
                if (macAddressLicFile != localMACAdderss)
                {
                    string strScript = "<script language='javascript' type='text/javascript'>";
                    strScript += "alert('" + rm.GetString("Alert_Licensecannotsupportothissystem", ci) + "');";
                    strScript += " </script>";
                    this.RegisterStartupScript("setScript", strScript);
                    return;
                }
                else
                {
                    txtNoConcurrentUser.Text = noOfConcurrentUserLicFile;
                    txtNoEmployee.Text = noOfEmployeeLicFile;
                    txtNoOfTerminal.Text = noOfTerminalLicFile;
                    txtLicGenDate.Text = Convert.ToDateTime(licenseGeneartionDateLicFile).ToString(Convert.ToString(Session["MDateFormat"]));
                    txtExpiryDate.Text = Convert.ToDateTime(expiryDateLicFile).ToString(Convert.ToString(Session["MDateFormat"]));
                    lblVersionType.Text = Convert.ToString(Session["Demo"]);
                }
            }
            else
            {
                // lic file not exist
            }
        }
        else
        {
        }
    }
    #endregion
    #region UserDefinedFunctions
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(199, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] == DBNull.Value || Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) != 1)
            {
                Response.Redirect("SystemLandingPage.aspx");
            }
        }
        else
        {
            Response.Redirect("SystemLandingPage.aspx");
        }
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("License_Info", ci);
        lblNoConcurrentUser.Text = rm.GetString("No_Of_Concurrent_Users", ci);
        lblNoemployee.Text = rm.GetString("No_Of_Employees", ci);
        lblNoOfTerminal.Text = rm.GetString("No_Of_Terminals", ci);
        lblLicGenDate.Text = rm.GetString("License_Generation_Date", ci);
        lblExpiryDate.Text = rm.GetString("License_Expiry_Date", ci);
        btnBack.Text = rm.GetString("btn_Back", ci);
        lblVT.Text = rm.GetString("lblVerisonType", ci);
    }
    #endregion
    #region ButtonEvents
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("System_LicenseManager.aspx");
    }
    #endregion
}