using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using Helper;
using Telerik.Web.UI;

namespace NikaiDSR
{
    public class PageBase : Page
    {
        protected int UserID = 0;
        protected int RoleID = 0;
        protected string LoginName = "";
        protected string UserName = "";
        protected string Password = "";
        protected int LocationId = 0;
        protected int LanguageId = 0;
        protected string Email = "";

        /// <summary>
        /// Sets the page title.
        /// </summary>
        /// <value>
        /// The page title.
        /// </value>
        protected string PageTitle
        {
            set
            {
                Master.Page.Title = value;
            }
        }

        protected void ClearSearchCriteriaSession()
        {
            //Clear Page Session
        }

        protected void ShowMessage(Label topLabel, string message)
        {
            topLabel.Text = "";
            topLabel.CssClass = "errorMsg";
            // Set the message to the label.
            if (topLabel.Text.Length == 0)
            {
                topLabel.Text = message;
            }
            else
            {
                topLabel.Text += "<br>" + message;
            }
        }

        protected void ShowMessage(Label topLabel, string errorMessage, Common.ERROR_MESSAGE_TYPE errorMessageType)
        {
            topLabel.Text = "";
            if (topLabel == null || errorMessage == null)
                return;

            topLabel.Text = errorMessage;
            switch (errorMessageType)
            {
                case Common.ERROR_MESSAGE_TYPE.SUCCESS:
                    topLabel.CssClass = "sucessMsg";
                    break;
                case Common.ERROR_MESSAGE_TYPE.ERROR:
                    topLabel.CssClass = "errorMsg";
                    break;
                case Common.ERROR_MESSAGE_TYPE.WARNING:
                    topLabel.CssClass = "warningMsg";
                    break;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            try
            {
                base.OnInit(e);

                //UserDTO userDetail = (UserDTO)Session["UserInfo"];
                //this.UserID = userDetail.Site_User_Id;
                //this.RoleID = userDetail.Role_Id;
                //this.LoginName = userDetail.Login_Name;
                //this.UserName = userDetail.Name;
                //this.Password = userDetail.Password;
                //this.LocationId = userDetail.Location_Id;
                CheckPageAccess();
            }
            catch (Exception exp)
            {
                //ErrorLoging.LogErrorInLogFile(exp);
            }
        }

        protected override void InitializeCulture()
        {
            base.InitializeCulture();

            UserDTO userDetail = (UserDTO)Session["UserInfo"];
            if (userDetail != null)
            {
                this.UserID = userDetail.Site_User_Id;
                this.RoleID = userDetail.Role_Id;
                this.LoginName = userDetail.Login_Name;
                this.UserName = userDetail.Name;
                this.Password = userDetail.Password;
                this.LocationId = userDetail.Location_Id;
                this.LanguageId = userDetail.Language_Id ?? 0;
                this.Email = userDetail.Email;

                if (LanguageId == 27)
                {
                    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-ae");
                    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-ae");
                }
            }
        }

        private void CheckPageAccess()
        {
            string path = Request.AppRelativeCurrentExecutionFilePath.ToLower();
            if (RoleID == 5)
            {
                if (!(path.Contains("login.aspx") || path.Contains("unauthorised.aspx") || path.Contains("landingpageexecutive.aspx")
                      || path.Contains("changepassword.aspx") || path.Contains("forgotpassword.aspx")
                      || path.Contains("dsrreport.aspx") || path.Contains("editcontact.aspx") || path.Contains("editcustomer.aspx")
                      || path.Contains("addvisit.aspx") || path.Contains("changeattendance.aspx") || path.Contains("dsrentry.aspx")
                    ))
                {
                    Server.Transfer("~/Unauthorised.aspx", true);
                }
            }
            else if (RoleID != 1)
            {
                if (!(path.Contains("login.aspx") || path.Contains("unauthorised.aspx") || path.Contains("landingpageadmin.aspx")
                      || path.Contains("changepassword.aspx") || path.Contains("forgotpassword.aspx")
                      || path.Contains("dsrreport.aspx") || path.Contains("editcontact.aspx") || path.Contains("editcustomer.aspx")
                      || path.Contains("addvisit.aspx") || path.Contains("changeattendance.aspx") || path.Contains("dsrentry.aspx")
                      || path.Contains("changereportees.aspx") || path.Contains("salespersonreport.aspx") || path.Contains("summarydayreport.aspx")
                      || path.Contains("teamreport.aspx") || path.Contains("viewcontacts.aspx")
                    ))
                {
                    Server.Transfer("~/Unauthorised.aspx", true);
                }
            }
        }

        /// <summary>
        ///----------------------------------------------------------------
        /// void Method ShowErrorMessage:
        /// Purpose: This method sets the error message for the errormsg div passed
        public static void ShowErrorMessage(string Message, System.Web.UI.HtmlControls.HtmlGenericControl errorMsg, Common.ERROR_MESSAGE_TYPE errorMessageType)
        {
            errorMsg.Visible = true;
            errorMsg.InnerText = Message;
            errorMsg.Style["display"] = "block";
            if (errorMessageType != Common.ERROR_MESSAGE_TYPE.SUCCESS)
                errorMsg.Attributes["style"] = "background-color:#fe4a4b";
            else
            {
                errorMsg.Attributes["style"] = "background-color:#1BA84A;border-width: 0";
            }
        }

        protected string GetDateFormat(DateTime date)
        {
            if (date == null)
                return "";
            string strDate = date.ToString("MM/dd/yyyy");
            if (strDate == "01/01/0001")
                strDate = "";
            return strDate;
        }

        protected void ExcelHeader(GridExportingArgs e, string customText)
        {
            e.ExportOutput = e.ExportOutput.Replace("<body>", "<body>" + "<b>" + customText + "</b>");
        }

    }
}
