using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Resources;
using System.Globalization;
using System.Threading;
using System.IO;
public partial class TimeCheck : System.Web.UI.MasterPage
{
    #region "Variable Declaration"
    ResourceManager rm;
    cls_Common objCommon = new cls_Common();
    LoginListEventData loginList = new LoginListEventData();
    EmployeeEventData ObjEmployeeEventData = new EmployeeEventData();
    DataSet ds_Image = new DataSet();
    BrandingEventdata obj_Branding = new BrandingEventdata();
    string strDelarLogo;
    DataSet ds_Logo = new DataSet();
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        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;
        // LoadStrings Function fills the Label from the Resource File        
        if (Session["EmployeeId"] != null)
        {
            string sUser = Convert.ToString(Cache[Session["EmployeeId"].ToString().ToUpper()]);
            if (sUser == null || sUser == String.Empty)
            {
                Response.Redirect("index.aspx");
            }
            else
            {
                string cValues = Convert.ToString(Cache[Session["EmployeeId"].ToString().ToUpper()]);
                string[] cData = cValues.Split(',');
                string cSessionId = cData[2].ToString();
                if (Session.SessionID != cSessionId)
                {
                    string currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;
                    RedirectToLoginPage(currentPageFileName);
                }
            }
        }
        if (Session["user"] == null || Session["user"].ToString() == "")
        {
            Response.Redirect("Index.aspx");
        }
        else
        {
            if (Convert.ToString(Session["EmployeeId"]).ToUpper() != "ADMIN")
            {
                lblEmployeeID.Text = Convert.ToString(Session["EmployeeId"]);
            }
            else
            {
                lblID.Style.Add("display", "none");
                lblEmployeeID.Style.Add("display", "none");
            }
            lbl_User2.ToolTip = lbl_user.ToolTip = lbl_User2.Text = lbl_user.Text = (Convert.ToString(Session["user"]).Length > 15) ?
            ci.TextInfo.ToTitleCase(Convert.ToString(Session["user"]).Substring(0, 15)) + "..." : ci.TextInfo.ToTitleCase(Convert.ToString(Session["user"]));
            //Convert.ToString(Session["user"]).Substring(0, 15).ToUpper() + "..." : Convert.ToString(Session["user"]).ToUpper();
        }
        if (!Page.IsPostBack)
        {
            LoadStrings(ci);
            if (Convert.ToString(Session["TopMenu"]) != string.Empty)
            {
                this.MainMenu.InnerHtml = Convert.ToString(Session["TopMenu"]);
                LoadYear();
            }
            else
            {
                loadMenu();
            }
            lbtn_logout.OnClientClick = "javascript:return confirm('" + rm.GetString("Alert_Logout", ci) + "');";
            //LoadNews();          
            cls_master_levels class1 = new cls_master_levels();
            DataSet ds = new DataSet();
            if (Session["UserGroupName"] != null)
            {
                ds = class1.getOrgLevelUpdated(0, Convert.ToString(Session["LevelRights"]), 0, Convert.ToString(Session["UserGroupName"]));
            }
            Session["OrgLevels"] = ds;
        }
        loadcss();
        GetImagePath();
        //get the culture info to set the language
        //LDAP
        if (Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["LDAPrequired"]) == "1")
        {
            if (Convert.ToString(Session["isDomainLoginRequiredflag"]) != "")
                if (Convert.ToBoolean(Session["isDomainLoginRequiredflag"]) == true)
                    A1.Style.Add("display", "none");
        }
    }
    private static void RedirectToLoginPage(string currentPageFileName)
    {
        switch (currentPageFileName.ToUpper())
        {
            case "DASHBOARD.ASPX":
            case "LANDING.ASPX":
            case "MYCALENDAR.ASPX":
                HttpContext.Current.Response.Redirect("Index.aspx");
                break;
            default:
                HttpContext.Current.Response.Redirect("../Index.aspx");
                break;
        }
    }
    protected void GetImagePath()
    {
        try
        {
            if (System.Configuration.ConfigurationManager.AppSettings["DelarLogo"].ToString() != "")
            {
                strDelarLogo = "~/" + System.Configuration.ConfigurationManager.AppSettings["DelarLogo"].ToString();
                IMG2.Visible = true;
                IMG2.Src = strDelarLogo;
            }
            else
            {
                IMG2.Visible = false;
            }
            ds_Logo = obj_Branding.SelectCusLogo();
            if (ds_Logo != null && ds_Logo.Tables.Count > 0)
            {
                if (ds_Logo.Tables[0].Rows.Count > 0)
                {
                    if (ds_Logo.Tables[0].Rows[0]["Cus_Image"] != System.DBNull.Value)
                    {
                        byte[] img = (byte[])ds_Logo.Tables[0].Rows[0]["Cus_Image"];
                        GetImage(img);
                        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        Random rnd = new Random();
                        this.IMG1.Src = "~/Customer_Logo.jpg?Rnd=" + rnd.NextDouble();
                    }
                    else
                    {
                        IMG1.Visible = false;
                    }
                }
                else
                {
                    IMG1.Visible = false;
                }
            }
            ds_Logo.Dispose();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    public void GetImage(byte[] Logo)
    {
        string strPath = AppDomain.CurrentDomain.BaseDirectory + "Customer_Logo.jpg";
        if (File.Exists(strPath))
        {
            File.Delete(strPath);
        }
        MemoryStream stream = new MemoryStream(Logo);
        System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
        img.Save(strPath);
        stream.Dispose();
        stream.Close();
    }
    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;
        //if Standard Demo
        MasHtml.Attributes.Add("dir", rm.GetString("Direction", ci));
        Session["Edit"] = rm.GetString("Grid_Edit", ci);
        Session["Delete"] = rm.GetString("btn_Delete", ci);
        Session["View"] = rm.GetString("btn_View", 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";
            stylebuild.Href = "../bootstrap/font-awesome/css/build-rtl.css";
            stylejvectormap.Href = "../plugins/jvectormap/jquery-jvectormap-1.2.2-rtl.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            styleallskins.Href = "../dist/css/_all-skins.min-rtl.css";
            stylecustom.Href = "../dist/css/custom-rtl.css";
            stylefonts.Href = "../dist/css/fonts.css";
            styledatepicker3.Href = "../plugins/datepicker/datepicker3-rtl.css";
            styledaterangepicker.Href = "../plugins/daterangepicker/daterangepicker-bs3-rtl.css";
            styleProgressBar.Href = "../css/ProgressBar-rtl.css";
            styleselect2.Href = "../plugins/select2/select2.min-rtl.css";
            styleAutocomp.Href = "../bootstrap/css/autoCompleteNew-rtl.css";
            Session["GridCSS"] = "../bootstrap/css/GridView-rtl.css";
            Session["SiteMapProvider"] = "Arabic";
        }
        else
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome.css";
            stylebuild.Href = "../bootstrap/font-awesome/css/build.css";
            stylejvectormap.Href = "../plugins/jvectormap/jquery-jvectormap-1.2.2.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min.css";
            styleallskins.Href = "../dist/css/_all-skins.min.css";
            stylecustom.Href = "../dist/css/custom.css";
            stylefonts.Href = "../dist/css/fonts.css";
            styledatepicker3.Href = "../plugins/datepicker/datepicker3.css";
            styledaterangepicker.Href = "../plugins/daterangepicker/daterangepicker-bs3.css";
            styleProgressBar.Href = "../css/ProgressBar.css";
            styleselect2.Href = "../plugins/select2/select2.min.css";
            styleAutocomp.Href = "../bootstrap/css/autoCompleteNew.css";
            Session["GridCSS"] = "../bootstrap/css/GridView.css";
            Session["SiteMapProvider"] = "English";
        }
    }
    private void loadMenu()
    {
        //Session["MenuCreate"] = "1";
        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;
        //if Standard Demo
        string strType = string.Empty;
        if (Session["Demo"] != null)
        {
            switch (Convert.ToString(Session["Demo"]))
            {
                case "Standard":
                    strType = "Standard";
                    break;
                case "Express":
                    strType = "Express";
                    break;
                default:
                    break;
            }
        }
        cls_Common getmenu = new cls_Common();
        DataSet ds = new DataSet();
        DataSet dsChildMenus = new DataSet();
        DataSet dsReportsMenu = new DataSet();
        DataSet dsReportsSubMenu = new DataSet();
        int intReportsSubMenu = 0;
        int MenuNo = 1;
        if (Convert.ToInt32(Session["userGroup_Id"]) != 0)
        {
            ds = getmenu.getUserMenu(0, Convert.ToInt32(Session["userGroup_Id"]), strType);
            for (int iloop = 0; iloop <= ds.Tables[0].Rows.Count - 1; iloop++)
            {
                String txt = Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]);
                string title = txt + "_Title";
                string Icon = txt + "_Icon";
                dsChildMenus = getmenu.getUserMenu(Convert.ToInt32(ds.Tables[0].Rows[iloop]["pageid"]), Convert.ToInt32(Session["userGroup_Id"]), strType);
                if (dsChildMenus.Tables[0].Rows.Count > 0)
                {
                    this.MainMenu.InnerHtml += "<li id='" + Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]) + "'><a class='menu-hed' href='" + ds.Tables[0].Rows[iloop]["path"] + "' id='" + Convert.ToString(ds.Tables[0].Rows[iloop]["pageid"]) + "' title='" + rm.GetString(title, ci) + " '><img class='icon' src='dist/img/" + rm.GetString(Icon, ci) + "'><span>" + rm.GetString(txt, ci) + "</span><i class='fa fa-angle-right pull-right'></i></a>";
                    this.MainMenu.InnerHtml += "<ul class='treeview-menu'>";
                }
                else
                {
                    this.MainMenu.InnerHtml += "<li id='" + Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]) + "'><a class='menu-hed' href='" + ds.Tables[0].Rows[iloop]["path"] + "' id='" + Convert.ToString(ds.Tables[0].Rows[iloop]["pageid"]) + "' title='" + rm.GetString(title, ci) + " '><img class='icon' src='dist/img/" + rm.GetString(Icon, ci) + "'><span>" + rm.GetString(txt, ci) + "</span></a>";
                }
                for (int iloopChild = 0; iloopChild <= dsChildMenus.Tables[0].Rows.Count - 1; iloopChild++)
                {
                    string txtChild = Convert.ToString(dsChildMenus.Tables[0].Rows[iloopChild]["displayName"]);
                    string titlechild = txtChild + "_Title";
                    string[] splittedPath = Convert.ToString(ds.Tables[0].Rows[iloop]["path"]).Split("/".ToCharArray());
                    if (dsChildMenus.Tables[0].Rows[iloopChild]["path"].ToString() != string.Empty)
                    {
                        this.MainMenu.InnerHtml += "<li id='" + Convert.ToString(dsChildMenus.Tables[0].Rows[iloopChild]["displayName"]) + "'><a href='" + dsChildMenus.Tables[0].Rows[iloopChild]["path"] + "' id='" + Convert.ToString(dsChildMenus.Tables[0].Rows[iloopChild]["pageid"]) + "' title='" + rm.GetString(titlechild, ci) + " '><i class='fa fa-circle-o'></i>" + rm.GetString(txtChild, ci) + " </a>";
                    }
                    else
                    {
                        this.MainMenu.InnerHtml += "<li id='" + Convert.ToString(dsChildMenus.Tables[0].Rows[iloopChild]["displayName"]) + "'><a id='" + Convert.ToString(dsChildMenus.Tables[0].Rows[iloopChild]["pageid"]) + "' title='" + rm.GetString(titlechild, ci) + " '><i class='fa fa-circle-o'></i>" + rm.GetString(txtChild, ci) + " <i class='fa fa-angle-right pull-right MenuPadding' ></i></a>";
                    }
                    dsReportsSubMenu = getmenu.getUserMenu(Convert.ToInt32(dsChildMenus.Tables[0].Rows[iloopChild]["pageId"]), Convert.ToInt32(Session["userGroup_Id"]), strType);
                    if (dsReportsSubMenu.Tables[0].Rows.Count > 0)
                    {
                        this.MainMenu.InnerHtml += "<ul class='treeview-menu' id='" + dsChildMenus.Tables[0].Rows[iloopChild]["displayname"].ToString() + "' style='padding:10px'>";
                        for (intReportsSubMenu = 0; intReportsSubMenu <= dsReportsSubMenu.Tables[0].Rows.Count - 1; intReportsSubMenu++)
                        {
                            string txtChild1 = Convert.ToString(dsReportsSubMenu.Tables[0].Rows[intReportsSubMenu]["displayName"]);
                            string titlechild1 = txtChild1 + "_Title";
                            this.MainMenu.InnerHtml += "<li class='active MenuPadding1' id='" + Convert.ToString(dsReportsSubMenu.Tables[0].Rows[intReportsSubMenu]["displayName"]) + "'><a href='" + dsReportsSubMenu.Tables[0].Rows[intReportsSubMenu]["path"] + "' id='" + Convert.ToString(dsReportsSubMenu.Tables[0].Rows[intReportsSubMenu]["pageid"]) + "' title='" + rm.GetString(titlechild1, ci) + " '><i class='fa fa-circle-o'></i>" + rm.GetString(txtChild1, ci) + "</a></li>";
                        }
                        if (dsReportsSubMenu.Tables[0].Rows.Count == 2)
                        {
                            this.MainMenu.InnerHtml += "<li class='line-spec'></li>";
                        }
                        this.MainMenu.InnerHtml += "</ul>";
                    }
                    this.MainMenu.InnerHtml += "</li>";
                }
                if (dsChildMenus.Tables[0].Rows.Count > 0)
                {
                    this.MainMenu.InnerHtml += "</ul>";
                }
                this.MainMenu.InnerHtml += "</li>";
                dsChildMenus.Dispose();
                MenuNo = MenuNo + 1;
            }
        }
        ds.Clear();
        ds.Dispose();
        Session["TopMenu"] = this.MainMenu.InnerHtml;
        Session["TopMenuPrevious"] = this.MainMenu.InnerHtml;
        LoadYear();
    }
    private void LoadYear()
    {
        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;
        //Year Setting Display in Main Menu
        string sYearValue = Convert.ToString(Cache["ActiveYearSetting"]);
        if (sYearValue != null && sYearValue != "")
        {
            lblYear.Text = rm.GetString("Year", ci);
            lblYearData.Text = sYearValue;
            if (lblYearData.Text.Contains("January"))
            {
                lblYearData.Text = lblYearData.Text.Replace("January", "Jan");
            }
            if (lblYearData.Text.Contains("February"))
            {
                lblYearData.Text = lblYearData.Text.Replace("February", "Feb");
            }
            if (lblYearData.Text.Contains("March"))
            {
                lblYearData.Text = lblYearData.Text.Replace("March", "Mar");
            }
            if (lblYearData.Text.Contains("April"))
            {
                lblYearData.Text = lblYearData.Text.Replace("April", "Apr");
            }
            if (lblYearData.Text.Contains("May"))
            {
                lblYearData.Text = lblYearData.Text.Replace("May", "May");
            }
            if (lblYearData.Text.Contains("June"))
            {
                lblYearData.Text = lblYearData.Text.Replace("June", "Jun");
            }
            if (lblYearData.Text.Contains("July"))
            {
                lblYearData.Text = lblYearData.Text.Replace("July", "Jul");
            }
            if (lblYearData.Text.Contains("August"))
            {
                lblYearData.Text = lblYearData.Text.Replace("August", "Aug");
            }
            if (lblYearData.Text.Contains("September"))
            {
                lblYearData.Text = lblYearData.Text.Replace("September", "Sep");
            }
            if (lblYearData.Text.Contains("October"))
            {
                lblYearData.Text = lblYearData.Text.Replace("October", "Oct");
            }
            if (lblYearData.Text.Contains("November"))
            {
                lblYearData.Text = lblYearData.Text.Replace("November", "Nov");
            }
            if (lblYearData.Text.Contains("December"))
            {
                lblYearData.Text = lblYearData.Text.Replace("December", "Dec");
            }
        }
        else
        {
            lblYearData.Text = string.Empty;
        }
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Login.Text = rm.GetString("Login", ci);
        lbtn_logout.Text = rm.GetString("btn_Logout", ci);
        lbl_OT_Comp.Text = rm.GetString("lbl_NewsEvents", ci);
    }
    protected void lbtn_logout_Click(object sender, EventArgs e)
    {
        if (Session["EmployeeId"] != null)
        {
            string userId = Convert.ToString(Session["EmployeeId"]);
            string sUser = Convert.ToString(Cache[Session["EmployeeId"].ToString().ToUpper()]);
            if (sUser != null && sUser != String.Empty)
            {
                HttpContext.Current.Cache.Remove(Session["EmployeeId"].ToString().ToUpper());
                Session.Clear();
            }
            ObjEmployeeEventData.InsertUpdateLastLogout(userId);
        }
        string currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;
        RedirectToLoginPage(currentPageFileName);
    }
}
