using System;
using System.Web.UI;
using Telerik.Web.UI;

namespace B4Restore.Common.Control.BaseDS
{
    public abstract class DSBaseControl : UserControl
    {
        //#region Private members

        //protected IRadGridViewStrategy radGridViewStrategy;
        //private bool _IsRedirectAllowed = true;
        //private readonly Hashtable TabControlAttributeSet = new Hashtable();

        //#endregion

        //#region Public members

        //public SerializableDictionary<string, SerializableDictionary<string, string>> GetTabControlAttributes(string key)
        //{
        //    return TabControlAttributeSet.ContainsKey(key) ? (SerializableDictionary<string, SerializableDictionary<string, string>>)TabControlAttributeSet[key] : new SerializableDictionary<string, SerializableDictionary<string, string>>();
        //}

        //public void AddTabControlAttribute(string key, object value)
        //{
        //    if (!TabControlAttributeSet.ContainsKey(key))
        //    {
        //        TabControlAttributeSet.Add(key, value);
        //    }
        //}

        //public virtual SerializableDictionary<string, SerializableDictionary<string, string>> GetDictionaryOfSiteMapFrom(string PageSource, string tabUrl, params string[] tabNames)
        //{
        //    return baseTabHelper.GetDictionaryOfSiteMapFrom(PageSource, tabUrl, tabNames);
        //}

        //public IRadGridViewStrategy RadGridViewStrategy
        //{
        //    get { return radGridViewStrategy; }
        //    set { radGridViewStrategy = value; }
        //}

        //private string pageSource = string.Empty;
        //public string PageSource
        //{
        //    get { return pageSource; }
        //    set { pageSource = value; }
        //}

        //public string ControlSource { get; set; }

        //protected bool IsMyPostBack
        //{
        //    get
        //    {
        //        if (IsPostBack)
        //        {
        //            //Control c = GetPostBackControl(Page);
        //            //return Controls.Contains(c);
        //            var eventTarget = Page.Request.Params.Get("__EVENTTARGET");
        //            return (eventTarget != null && eventTarget.IndexOf(ID) >= 0);
        //        }
        //        return false;
        //    }
        //}

        //public string RedirectURL
        //{
        //    get
        //    {
        //        return GetRedirectURL();
        //    }
        //    set
        //    {
        //        SetRedirectURL(value);
        //    }
        //}

        //public string BackURL
        //{
        //    get
        //    {
        //        return GetBackURL();
        //    }
        //    set
        //    {
        //        SetBackURL(value);
        //    }
        //}

        //public ClientInfo CurrentClient
        //{
        //    get
        //    {
        //        return ClientController.CurrentClient;
        //    }
        //}

        //public virtual bool IsRedirectAllowed
        //{
        //    get
        //    {
        //        return _IsRedirectAllowed;
        //    }
        //    set
        //    {
        //        _IsRedirectAllowed = value;
        //    }
        //}

        //protected virtual string GetRedirectURL()
        //{
        //    var result = (string)ViewState["RedirectURL"];

        //    /*if (string.IsNullOrEmpty(result))
        //    {
        //        result = Parser.GetPathWithoutShortWebRoot(Request.Url.LocalPath);
        //    }*/

        //    return result;
        //}

        //protected virtual void SetRedirectURL(string URL)
        //{
        //    ViewState["RedirectURL"] = URL;
        //}

        //protected virtual string GetBackURL()
        //{
        //    var result = (string)ViewState["BackURL"];

        //    if (string.IsNullOrEmpty(result))
        //    {
        //        result = NavigationHistory.Previous;
        //    }

        //    return result;
        //}

        //protected virtual void SetBackURL(string URL)
        //{
        //    ViewState["BackURL"] = URL;
        //}

        //protected virtual string NoteImageName(int? note_id)
        //{
        //    return Parser.GetWebRoot() + "/images/note_" + (note_id == null ? "new" : "existing") + ".gif";
        //}

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

        //    if (String.IsNullOrEmpty(PageSource) && NamingContainer is BaseControl) PageSource = ((BaseControl)NamingContainer).PageSource;
        //    if (String.IsNullOrEmpty(ControlSource) && NamingContainer is BaseControl)
        //        ControlSource = ((BaseControl)NamingContainer).ControlSource;

        //    Page.LoadComplete += SetupHelpers;
        //}

        //protected override void OnLoad(EventArgs e)
        //{
        //    base.OnLoad(e);

        //    if (IsRedirectAllowed && !String.IsNullOrEmpty(RedirectURL))
        //    {
        //        var _RedirectURL = RedirectURL;
        //        RedirectURL = null;
        //        Response.Redirect(_RedirectURL);
        //    }
        //}

        //protected virtual void OnSelectingData(object sender, ObjectDataSourceSelectingEventArgs e)
        //{
        //    if (e.Arguments.MaximumRows == int.MaxValue)
        //    {
        //        e.Arguments.MaximumRows = int.MaxValue - 1;
        //    }
        //    e.Cancel = !Visible;
        //}

        //protected virtual void SetupHelpers(object sender, EventArgs e)
        //{
        //    foreach (var src in Controls.OfType<ObjectDataSource>())
        //    {
        //        src.Selecting += OnSelectingData;
        //    }
        //}

        //protected virtual void rgView_Init(object sender, EventArgs e)
        //{
        //    ((RadGrid)sender).ImagesPath = "~/App_Themes/" + Page.Theme + "/Grid";
        //    ((RadGrid)sender).EnableViewState = true;
        //    ((RadGrid)sender).ItemEvent += rgView_ItemEvent;

        //    imgToExel.CommandArgument = ((RadGrid)sender).ID;
        //    imgToExel.Click += imgToExel_ToExelClick;
        //    imgToExel.ID = "imgtoexel_" + ((RadGrid)sender).ID;
        //    imgToExel.CssClass = "exel";
        //    imgToExel.Text = "&nbsp;";
        //    imgToExel.ToolTip = "Export this view to Excel";
        //    ((RadGrid)sender).ShowFooter = true;
        //}

        //protected virtual void rgView_ItemEvent(object sender, GridItemEventArgs e)
        //{
        //    if (!(e.EventInfo is GridInitializePagerItem))
        //        return;

        //    var info = e.EventInfo as GridInitializePagerItem;

        //    e.Canceled = true;

        //    var item = e.Item as GridPagerItem;
        //    if (item == null)
        //        return;

        //    item.PagerContentCell.Controls.Add(new RadGridB4RestorePager(info.PagingManager, e.Item.OwnerTableView, Page, 10));
        //}

        //protected virtual void imgToExel_ToExelClick(object sender, EventArgs e)
        //{
        //    var lb = sender as LinkButton;
        //    if (lb == null) return;

        //    var rg = WorkWithControlInstance.FindControlRecursive(this, lb.CommandArgument, false) as RadGrid;
        //    if (rg == null) return;

        //    rg.PageSize = rg.MasterTableView.VirtualItemCount;
        //    rg.MasterTableView.PageSize = rg.MasterTableView.VirtualItemCount;
        //    rg.CurrentPageIndex = 0;
        //    rg.ExportSettings.ExportOnlyData = true;
        //    rg.ExportSettings.IgnorePaging = false;
        //    rg.ExportSettings.OpenInNewWindow = true;
        //    rg.Rebind();
        //    Response.Expires = 10;

        //    // to fix ssl and IE issue
        //    rg.Page.Response.ClearHeaders();
        //    rg.Page.Response.Cache.SetCacheability(HttpCacheability.Private);

        //    rg.MasterTableView.ExportToExcel();
        //}

        //private LinkButton imgToExel = new LinkButton();
        //internal readonly BaseTabHelper baseTabHelper = new BaseTabHelper();
        //private readonly WorkWithControl workWithControl;
        //private bool isDetailView;

        //public DSBaseControl()
        //{
        //    workWithControl = new WorkWithControl();
        //}

		protected void rdtpView_Init(object sender, EventArgs e)
		{
			if (sender is RadTimePicker)
			{
				((RadTimePicker)sender).ImagesPath = "~/App_Themes/" + Page.Theme + "/Calendar";
			}
			else if (sender is RadDateTimePicker)
			{
				((RadDateTimePicker)sender).ImagesPath = "~/App_Themes/" + Page.Theme + "/Calendar";
			}
			else if (sender is RadDatePicker)
			{
				((RadDatePicker)sender).ImagesPath = "~/App_Themes/" + Page.Theme + "/Calendar";
			}
		}

        //protected virtual void rgView_ItemCreated(object sender, GridItemEventArgs e)
        //{
        //    var rgView = (RadGrid)sender;

        //    if (radGridViewStrategy != null)
        //        radGridViewStrategy.rgView_ItemCreated(sender, e);


        //    if (e.Item.ItemType == GridItemType.Header)
        //    {
        //        foreach (var c in e.Item.Controls)
        //        {
        //            var cc = c as GridTableHeaderCell;
        //            if (cc != null && cc.Controls.Count > 2)
        //            {
        //                var gridImageButton = cc.Controls[2] as GridImageButton;
        //                if (gridImageButton != null)
        //                {
        //                    gridImageButton.OnClientClick = "return false;";
        //                }
        //            }
        //        }
        //    }
        //    else if (e.Item is GridDataItem)
        //    {
        //        var _lblRowNum = e.Item.FindControl("lblRowNum") as Label;
        //        if (_lblRowNum != null)
        //        {
        //            _lblRowNum.Text = (rgView.MasterTableView.CurrentPageIndex * rgView.MasterTableView.PageSize + e.Item.ItemIndex + 1).ToString();
        //        }
        //    }

        //    if (e.Item.ItemType == GridItemType.Pager)
        //    {
        //        foreach (var cell in workWithControl.GetControlList(e.Item.Controls[0], typeof(HtmlTableCell)))
        //        {
        //            var htmlTableCell = cell as HtmlTableCell;
        //            if (htmlTableCell != null && rgView.PageCount > 1 && string.Equals(htmlTableCell.Width, "70%", StringComparison.OrdinalIgnoreCase))
        //            {
        //                var htmlTableRow = htmlTableCell.Parent as HtmlTableRow;
        //                if (htmlTableRow != null)
        //                {
        //                    var tableCell = new HtmlTableCell();
        //                    tableCell.Controls.Add(imgToExel);
        //                    tableCell.Attributes.Add("class", "ra");
        //                    htmlTableRow.Cells.Add(tableCell);
        //                    rgView.ShowFooter = false;
        //                }
        //            }
        //        }
        //    }
        //    if (e.Item is GridHeaderItem)
        //    {
        //        foreach (var cell in e.Item.Cells.Cast<TableCell>())
        //        {
        //            foreach (var ctrl in cell.Controls.Cast<System.Web.UI.Control>())
        //            {
        //                if (ctrl is LinkButton)
        //                {
        //                    ctrl.ID = "btn" + (ctrl as LinkButton).Text;
        //                }
        //            }
        //        }
        //    }
        //    if (e.Item.ItemType == GridItemType.Footer && rgView.PageCount == 1 && imgToExel != null)
        //    {
        //        var gcell = e.Item.Controls[e.Item.Controls.Count - 1] as GridTableCell;
        //        if (gcell == null)
        //        {
        //            return;
        //        }

        //        gcell.Controls.Add(imgToExel);
        //        gcell.CssClass = "ra";
        //    }
        //}

        //public static string ConvertNumberToGB(decimal? num, bool bPrefix)
        //{
        //    var result = String.Empty;

        //    if (num.HasValue)
        //    {
        //        var numValue = num.Value / 1024 / 1024 / 1024;

        //        result = String.Format("{0:#,0.0}", numValue);

        //        if (bPrefix)
        //        {
        //            result = String.Format("{0} GB", result);
        //        }
        //    }

        //    return result;
        //}

        //protected void rgView_Load(object sender, EventArgs e)
        //{
        //    base.OnLoad(e);

        //    var rgView = (RadGrid)sender;

        //    if (rgView.Visible)
        //    {
        //        int size;
        //        var userSetting = UserSettingController.CurrentUserSettingByType(CustomUserSettingType.GridPageSize);

        //        if (userSetting != null && int.TryParse(userSetting.Value, out size))
        //        {
        //            if (size > 0)
        //            {
        //                rgView.PageSize = size;
        //                rgView.MasterTableView.PageSize = size;
        //            }
        //        }
        //    }
        //}
        //public virtual string TabID
        //{
        //    get { return baseTabHelper.TabID; }
        //}

        //public virtual string UrlToNotePopup
        //{
        //    get { return "~/Pages/NotePopup.aspx".ApplyAppPathModifier(); }
        //}

        //public LinkButton ImgToExel
        //{
        //    get { return imgToExel; }
        //    protected set { imgToExel = value; }
        //}

        //public WorkWithControl WorkWithControlInstance
        //{
        //    get { return workWithControl; }
        //}

        //protected virtual bool IsDetailView
        //{
        //    get { return isDetailView; }
        //    set { isDetailView = value; }
        //}

        //#endregion

        //private readonly WorkWithControl wc = new WorkWithControl();
        //private bool isImgToExelInserted;

        //public virtual string MenuPassiveClickRelUrl(string pageURL, string queryParams)
        //{
        //    return NavigateParams.MenuPassiveClickRelUrl(pageURL, queryParams);
        //}

        //public virtual string MenuPassiveClickRelUrl(string pageURL, Dictionary<string, string> param)
        //{
        //    return NavigateParams.MenuPassiveClickRelUrl(pageURL, param);
        //}

        //protected virtual string ApplyQueryParamsTo(string pageURL, Dictionary<string, string> param)
        //{
        //    return NavigateParams.ApplyQueryParamsTo(pageURL, param);
        //}


        //protected virtual void NavigateToPage(string where)
        //{
        //    NavigateParams.NavigateToPage(where);
        //}

        //protected virtual void btnCancel_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect(Response.ApplyAppPathModifier(BackURL));
        //}

        //public string FormatNoteText(string sNoteText)
        //{
        //    return sNoteText == null ? String.Empty : HttpUtility.HtmlDecode(sNoteText).Replace('"', '\'');
        //}

        //protected static bool IsRowInGrid(GridItemEventArgs e)
        //{
        //    return e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem;
        //}
    }
}