using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Threading;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                        Started Date  : 12-Aug-2007
* Module          : Terminal Master                                  Last Modified : 14-Aug-2007
* Programmer      : 
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_TerminalView : Base.BasePage
{
    #region "Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    TerminalEventData objTerminalEventData = new TerminalEventData();
    cls_Common objCommon = new cls_Common();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    int EmployeeID = 0, intTerminal_ID = 0;
    string gstrUserGroupName = string.Empty;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        loadcss();
        EmployeeID = Convert.ToInt32(Session["EmpAutoID"]);
        UserRights();
        if (Session["UserGroupName"] != null)
        {
            gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        }
        if (gstrUserGroupName != "SuperAdmin")
        {
            lbl_DeleteStatus_Main.Visible = false;
            lbl_DeleteStatus.Visible = false;
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
        intTerminal_ID = Convert.ToInt32(Request.QueryString["Terminal_Id"]);
        GetTerminalDetails();
        btn_modify.Focus();
        btn_delete.Attributes.Add("onclick", "return confirm('" + rm.GetString("Alert_ConfirmDelete", ci) + "');");
        btn_Restore.OnClientClick = "return confirm('" + rm.GetString("Alert_Areyousuretorestore", ci) + "')";
        btn_PermanentDelete.OnClientClick = "return confirm('" + rm.GetString("Alert_AreyouwantDeleteRecordPermanently", ci) + "')";
        int Count = objTerminalEventData.GetTerminalMappingCount(intTerminal_ID);
        if (Count > 0)
        {
            btn_delete.Enabled = false;
            btn_PermanentDelete.Enabled = false;
        }
        if (!IsPostBack)
        {
            objCommon.menu("Terminal", "224");
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_Terminal.aspx");
    }
    protected void btn_modify_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_TerminalModify.aspx?Terminal_Id=" + intTerminal_ID);
    }
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        int Count = objTerminalEventData.GetTerminalMappingCount(intTerminal_ID);
        if (Count > 0)
        {
            base.Message(rm.GetString("Master_Terminal_Reader_Delete", ci));
            return;
        }
        objTerminalEventData.DeleteTerminals(Convert.ToString(intTerminal_ID), EmployeeID);
        base.MessageWithRedirect(rm.GetString("Alert_Terminaldeletedsuccessfully", ci), "TA_UI_Master_Terminal.aspx");
    }
    protected void btn_Restore_Click(object sender, EventArgs e)
    {
        int Count = objTerminalEventData.CheckRestoreTerminal(intTerminal_ID);
        if (Count > 0)
        {
            base.Message(rm.GetString("Message_Cannot_Restore", ci));
            return;
        }
        bool isAllowTerminalAdd = CheckNoOfTerminalForLicense();
        if (isAllowTerminalAdd)
        {
            objTerminalEventData.RestoreTerminals(Convert.ToString(intTerminal_ID), EmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_Terminalrestoredsuccessfully", ci), "TA_UI_Master_Terminal.aspx");
        }
    }
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        int Count = objTerminalEventData.GetTerminalMappingCount(intTerminal_ID);
        if (Count > 0)
        {
            base.Message(rm.GetString("Master_Terminal_Reader_Delete", ci));
            return;
        }
        objTerminalEventData.PermanentDeleteTerminals(Convert.ToString(intTerminal_ID), EmployeeID);
        base.MessageWithRedirect(rm.GetString("Alert_Terminaldeletedsuccessfully", ci), "TA_UI_Master_Terminal.aspx");
    }
    #endregion
    #region UserDefinedFunctions
    private bool CheckNoOfTerminalForLicense()
    {
        DemoVersion objDemoVersion = new DemoVersion();
        bool allowEmployeeAdd = false;
        string[] fileArr = Directory.GetFiles(Server.MapPath("../LicenseFile"), "*.lic");
        int arrCount = fileArr.Length;
        if (arrCount > 0)
        {
            string licenseFileName = Convert.ToString(fileArr[arrCount - 1]);
            FileStream licenseFileStream = new FileStream(licenseFileName, FileMode.Open);
            StreamReader licenseFileStreamReader = new StreamReader(licenseFileStream);
            string encryptedLicFileInfo = licenseFileStreamReader.ReadToEnd();
            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 += "location.href='../Index.aspx';";
                strScript += " </script>";
                this.RegisterStartupScript("AlertLicInfoNotCorrect", strScript);
                return false;
            }
            string[] decryptedLicFileInfoArr = decryptedLicFileInfo.Split('#');
            string basicLicInfo = Convert.ToString(decryptedLicFileInfoArr[0]);
            string[] basicLicInfoArr = basicLicInfo.Split(',');
            int noOfEmployeeLicFile = Convert.ToInt32(basicLicInfoArr[8]);
            allowEmployeeAdd = objDemoVersion.RestoreCheckTerminalCreation("License", noOfEmployeeLicFile);
            if (allowEmployeeAdd)
            {
                return true;
            }
            else
            {
                string strScript = "<script language='javascript' type='text/javascript'>";
                strScript += "alert('" + rm.GetString("Alert_LicensenotallowtoRestoreTerminal", ci) + "');";
                strScript += " </script>";
                this.RegisterStartupScript("setScript", strScript);
                return false;
            }
        }
        else
        {
            return true;
        }
    }
    private void GetTerminalDetails()
    {
        DataSet ds_TerminalDetails = new DataSet();
        ds_TerminalDetails = objTerminalEventData.SelectTerminalDetailsBYIdView(intTerminal_ID);
        if (ds_TerminalDetails.Tables[0].Rows.Count > 0)
        {
            lbl_DeleteStatus.Text = Convert.ToString(ds_TerminalDetails.Tables[0].Rows[0]["IsDeleted"]);
            if (lbl_DeleteStatus.Text == "True" || lbl_DeleteStatus.Text == rm.GetString("Grid_True", ci))
            {
                btn_delete.Visible = false;
            }
            else if (lbl_DeleteStatus.Text == "False" || lbl_DeleteStatus.Text == rm.GetString("Grid_False", ci))
            {
                btn_Restore.Visible = false;
            }
            lbl_TerReaderID.Text = Convert.ToString(ds_TerminalDetails.Tables[0].Rows[0]["Terminal_ReaderID"]);
            lbl_TerminalName.Text = Convert.ToString(ds_TerminalDetails.Tables[0].Rows[0]["Terminal_Name"]);
            lbl_TerminalDescription.Text = Convert.ToString(ds_TerminalDetails.Tables[0].Rows[0]["Terminal_Description"]);
            lbl_RawDataFormat.Text = Convert.ToString(ds_TerminalDetails.Tables[0].Rows[0]["Raw_Data_Format"]);
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_TerminalId.Text = rm.GetString("TerminalMaster_Terminal_Id", ci);
        lbl_TerminalName1.Text = rm.GetString("TerminalMaster_Terminal_Name", ci);
        lbl_TerminalDescription1.Text = rm.GetString("TerminalMaster_Terminal_Desc", ci); ;
        lbl_RawDataFormat1.Text = rm.GetString("TerminalDataType_RawDataFormat", ci);
        lbl_TerminalMaster.Text = rm.GetString("Masters_Terminal_TerminalMaster", ci);
        lbl_DeleteStatus_Main.Text = rm.GetString("DeleteStatus", ci);
        btn_modify.Text = rm.GetString("btn_Modify", ci);
        btn_delete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_Restore.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Restore", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_PermanentDelete", ci);
        btn_cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(10, 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("TA_UI_Master_Terminal.aspx");
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.btn_modify.Visible = true;
            }
            else
            {
                this.btn_modify.Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["deletePrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["deletePrv"]) == 1)
            {
                this.btn_delete.Visible = true;
                this.btn_PermanentDelete.Visible = true;
                this.btn_Restore.Visible = true;
            }
            else
            {
                this.btn_delete.Visible = false;
                this.btn_PermanentDelete.Visible = false;
                this.btn_Restore.Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    private void loadcss()
    {
        CultureInfo ci;
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        htmTerminal.Attributes.Add("dir", rm.GetString("Direction", ci));
        if (source_Language == "Resources.Resource_AR")
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            stylecustom.Href = "../dist/css/custom-rtl.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
        else
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min.css";
            stylecustom.Href = "../dist/css/custom.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
    }
    #endregion
}