using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using Telerik.Web.UI;

using System.Security.Principal;
using System.Web.SessionState;


namespace StaticAppGlobals
{
    /// <summary>
    /// Summary description for BasePage
    /// </summary>
    public class BasePage : System.Web.UI.Page
    {
        protected IIdentity UserId = HttpContext.Current.User.Identity;
        //protected CentralTicket cticketservice = new CentralTicket();
        protected DataRetriever cticketservice = new DataRetriever();

        public CentralUserProfile UserProfile
        {
            get 
            {
                try
                {
                    return (CentralUserProfile)HttpContext.Current.Session["UserProfile"];
                }
                catch
                {
                    HttpContext.Current.Session["UserProfile"] = new CentralUserProfile(UserId);
                    return (CentralUserProfile)HttpContext.Current.Session["UserProfile"];
                }
            }
            set 
            {
                HttpContext.Current.Session["UserProfile"] = value;
            }
        }

        public BasePage()
        {
            DataRetriever.txtCSVFolderPath = Server.MapPath("~/App_Data/CSVFiles");
            cticketservice.FillDataSets();
        }

        protected void Page_Load()
        {
            if (UserProfile == null)
                UserProfile = new CentralUserProfile(UserId);

        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            //register_script_block();
            ScriptReference sr = new ScriptReference("~/javascript/AjaxErrorHandler.js");
            sr.NotifyScriptLoaded = false;
            ScriptManager.GetCurrent(this.Page).CompositeScript.Scripts.Add(sr);
        }

    }

    public class CentralUserProfile
    {
        #region Variable Declaration

        //private Variables Declarations
        private IIdentity UserId;
        private bool _Validated;
        private string _user_login = "";

        //public Variables Declarations
        public bool Validated { get { return _Validated; } }

        public string AD_UserName
        {
            get
            {
                char[] splitter = { '\\' };
                return UserId.Name.Split(splitter)[1].ToString();
            }
        }
        public string AD_UserDomain
        {
            get
            {
                char[] splitter = { '\\' };
                return UserId.Name.Split(splitter)[0].ToString();
            }
        }

        //public string login { get { return _user_login; } }
        //public string user_login { get { return _user_login; } }
        public string login { get { return "ivalente"; } }
        public string user_login { get { return "ivalente"; } }
        public string user_name { get; set; }
        public string user_email { get; set; }
        public string user_phone { get; set; }
        public string user_mobile { get; set; }
        public string user_title { get; set; }

        #endregion

        public CentralUserProfile()
        {
            _Validated = false;
        }

        public CentralUserProfile(IIdentity UserIdentity)
        {
            UserId = UserIdentity;
            char[] splitter = { '\\' };
            InitProfile(UserId.Name.Split(splitter)[1].ToString());
        }

        public CentralUserProfile(string login)
        {
            InitProfile(login);
        }

        public void InitIdentity(IIdentity UserIdentity)
        {
            UserId = UserIdentity;
            char[] splitter = { '\\' };
            InitProfile(UserId.Name.Split(splitter)[1].ToString());
        }

        public void InitProfile(IIdentity UserIdentity)
        {
            UserId = UserIdentity;
            char[] splitter = { '\\' };
            InitProfile(UserId.Name.Split(splitter)[1].ToString());
        }

        public void InitProfile(string login)
        {
            //do other stuff....
            _user_login = login;
            _Validated = true;
        }

        public void InitViewableAreas()
        {
        }

    }

}
