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 Telerik.Web.UI;
using System.Security.Principal;

using StaticAppGlobals;

public partial class CentralTicketing_Interfaces_UserAssignedTicketListingsView : BaseUserControl //System.Web.UI.UserControl
{

    protected int SelectedTab
    {
        get {
            return Convert.ToInt32(SelectedTabNumber.Value);
        }
        set {
            SelectedTabNumber.Value = value.ToString();
        }
    }

    protected List<getCTicketsAssignedSystemsResult> UsersTicketListInSystems
    {
        get
        {
            try
            {
                return (List<getCTicketsAssignedSystemsResult>)HttpContext.Current.Session["UsersTicketListInSystems"];
            }
            catch
            {
                HttpContext.Current.Session["UsersTicketListInSystems"] = cticketservice.GetSystemAssignedList(UserProfile.user_login);
                return (List<getCTicketsAssignedSystemsResult>)HttpContext.Current.Session["UsersTicketListInSystems"];
            }
        }
        set
        {
            HttpContext.Current.Session["UsersTicketListInSystems"] = value;
        }
    }

    protected ctickets_systems_configuration ListedSystemConfig
    {
        get
        {
            try
            {
                if ((ctickets_systems_configuration)HttpContext.Current.Session["ListedSystemConfig"] == null )
                    HttpContext.Current.Session["ListedSystemConfig"] = cticketservice.GetCTicketSystemConfiguration((int)UsersTicketListInSystems[TicketsListTabStrip.SelectedIndex].id_system);
                return (ctickets_systems_configuration)HttpContext.Current.Session["ListedSystemConfig"];
            }
            catch
            {
                HttpContext.Current.Session["ListedSystemConfig"] = cticketservice.GetCTicketSystemConfiguration((int)UsersTicketListInSystems[TicketsListTabStrip.SelectedIndex].id_system);
                return (ctickets_systems_configuration)HttpContext.Current.Session["ListedSystemConfig"];
            }
        }
        set
        {
            HttpContext.Current.Session["ListedSystemConfig"] = value;
        }
    }

    CentralTicketing_Interfaces_WorkListViewControl CTicketListControl;
    CentralTicketing_Interfaces_UserAssignedTicketListingsByType CTicketTabListControl;
    CentralTicketing_Interfaces_UserAssignedTicketListingsByStatus CTicketTabListStatusControl;


    protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load();


        if (!Page.IsPostBack)
        {
            DoTabs();
            if (TicketsListTabStrip.Tabs.Count > 0)
            {
                TicketsListTabStrip.SelectedIndex = 0;
                SelectedTab = 0;
                ListedSystemConfig = cticketservice.GetCTicketSystemConfiguration((int)UsersTicketListInSystems[0].id_system);
                AddWorkListViewControl(SelectedTab, (int)UsersTicketListInSystems[SelectedTab].id_system);
                TicketsListTabStrip.SelectedTab.PageViewID = "CTicketPageView0";
                TicketsListTabStrip.SelectedTab.PageView.Selected = true;
            }
        }
        else
        {
            if (TicketsListTabStrip.SelectedIndex != -1)
            {
                DoTabs();
            }
            try
            {
                ListedSystemConfig = cticketservice.GetCTicketSystemConfiguration((int)UsersTicketListInSystems[TicketsListTabStrip.SelectedIndex].id_system);
            }
            catch
            {
            }
        }
    }

    public void Refresh()
    {
        if (TicketsListTabStrip.SelectedIndex != -1)
        {
            DoTabs();
            RefreshList();
        }
    }

    public void RefreshList()
    {
        if (CTicketListControl != null)
            CTicketListControl.Refresh();
        if (CTicketTabListControl != null)
            CTicketTabListControl.Refresh();
    }

    public void RowSelected(object sender, EventArgs e)
    {
    }

    public void setTimer(int msecs)
    {
        TicketListingsTimer.Interval = msecs;
    }

    protected void TicketListingsTimer_Tick(object sender, EventArgs e)
    {
        Refresh();
    }

    protected void DoTabs()
    {
        int TabNumber = 0;
        UsersTicketListInSystems = cticketservice.GetSystemAssignedList(UserProfile.user_login);
        foreach (getCTicketsAssignedSystemsResult CTicketList in UsersTicketListInSystems)
        {
            if (TicketsListTabStrip.Tabs.Count() > TabNumber)
            {
                TicketsListTabStrip.Tabs[TabNumber].Text = CTicketList.system_label;
                if (SelectedTab == TabNumber)
                {
                    AddWorkListViewControl(SelectedTab, (int)UsersTicketListInSystems[TicketsListTabStrip.SelectedIndex].id_system);
                }
            }
            else
                TicketsListTabStrip.Tabs.Add(CreateTab(CTicketList.system_label, TabNumber, (int)CTicketList.id_system));
            TabNumber++;
        }
    }

    private RadTab CreateTab(string Tabtext, int TabNumber, int id_system)
    {
        RadTab tab = new RadTab();
        tab.Text = Tabtext;
        tab.PageViewID = "CTicketPageView" + TabNumber.ToString();
        tab.TabIndex = (short)TabNumber;

        CreatePageview(TabNumber, id_system);

        return tab;
    }

    protected void CreatePageview(int index, int id_system)
    {
        RadPageView pageView = new RadPageView();
        pageView.ID = "CTicketPageView" + index.ToString();
        pageView.Visible = true;
        pageView.Enabled = true;
        pageView.EnableViewState = true;

        TicketsListMultiPage.PageViews.Add(pageView);
    }

    protected void AddWorkListViewControl(int index, int id_system)
    {
        RadPageView pageView = TicketsListMultiPage.PageViews[index];
        switch (ListedSystemConfig.system_list_type)
        {
            case 1:
                //CTicketTabListControl = new CentralTicketing_Interfaces_UserAssignedTicketListingsByType();
                
                CTicketTabListControl = (CentralTicketing_Interfaces_UserAssignedTicketListingsByType)LoadControl("~/Interfaces/UserAssignedTicketListingsByType.ascx");
                CTicketTabListControl.ID = "CTicketListControl" + index.ToString();
                CTicketTabListControl.id_system = id_system;
                CTicketTabListControl.EnableViewState = true;

                pageView.Controls.Add(CTicketTabListControl);
                break;

            case 2:
               // CTicketTabListStatusControl = new CentralTicketing_Interfaces_UserAssignedTicketListingsByStatus();
               
                CTicketTabListStatusControl = (CentralTicketing_Interfaces_UserAssignedTicketListingsByStatus)LoadControl("~/Interfaces/UserAssignedTicketListingsByStatus.ascx");
                CTicketTabListStatusControl.ID = "CTicketListControl" + index.ToString();
                CTicketTabListStatusControl.id_system = id_system;
                CTicketTabListStatusControl.EnableViewState = true;

                pageView.Controls.Add(CTicketTabListStatusControl);
                break;

            case 0:
            default:
              //  CTicketListControl = new CentralTicketing_Interfaces_WorkListViewControl();
               
                CTicketListControl = (CentralTicketing_Interfaces_WorkListViewControl)LoadControl("~/Interfaces/WorkListViewControl.ascx");
                CTicketListControl.ID = "CTicketListControl" + index.ToString();
                CTicketListControl.RowSelected += new EventHandler(RowSelected);

                CTicketListControl.id_system = id_system;
                CTicketListControl.EnableViewState = true;

                pageView.Controls.Add(CTicketListControl);
                break;
        }
    }

    protected void TicketsListTabStrip_TabClick(object sender, RadTabStripEventArgs e)
    {
        SelectedTab = e.Tab.Index;
        AddWorkListViewControl(SelectedTab, (int)UsersTicketListInSystems[TicketsListTabStrip.SelectedIndex].id_system);
        e.Tab.PageViewID = "CTicketPageView" + SelectedTab.ToString(); ;
        e.Tab.PageView.Selected = true;
        (Page.FindControl("BottomPane") as RadPane).Collapsed = true;

    }
}
