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

public partial class MyPortalAjax : System.Web.UI.Page
{
    private List<DockState> CurrentDockStates
    {
        get
        {
            List<DockState> _currentDockStates = (List<DockState>)Session["CurrentDockStatesMyPortal"];
            if (Object.Equals(_currentDockStates, null))
            {
                _currentDockStates = new List<DockState>();
                Session["CurrentDockStatesMyPortal"] = _currentDockStates;
            }
            return _currentDockStates;
        }
        set
        {
            Session["CurrentDockStatesMyPortal"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownZone.DataBind();
        }
    }

    public ArrayList GetZones()
    {
        ArrayList zones = new ArrayList();
        zones.Add(RadDockZone1);
        zones.Add(RadDockZone2);

        return zones;
    }

    protected void Page_Init(object sender, EventArgs e)
    {        
        for (int i = 0; i < CurrentDockStates.Count; i++)
        {
            if (CurrentDockStates[i].Closed == false)
            {
                RadDock dock = CreateRadDockFromState(CurrentDockStates[i]);                
                RadDockLayout1.Controls.Add(dock);                
                CreateSaveStateTrigger(dock);                
                LoadWidget(dock);
            }
        }
    }

    protected void RadDockLayout1_LoadDockLayout(object sender, DockLayoutEventArgs e)
    {
        foreach (DockState state in CurrentDockStates)
        {
            e.Positions[state.UniqueName] = state.DockZoneID;
            e.Indices[state.UniqueName] = state.Index;
        }
    }

    protected void RadDockLayout1_SaveDockLayout(object sender, DockLayoutEventArgs e)
    {        
        CurrentDockStates = RadDockLayout1.GetRegisteredDocksState();
    }

    private RadDock CreateRadDockFromState(DockState state)
    {
        RadDock dock = new RadDock();
        dock.ID = string.Format("RadDock{0}", state.UniqueName);
        dock.ApplyState(state);
        dock.Command += new DockCommandEventHandler(dock_Command);
        dock.Commands.Add(new DockCloseCommand());
        dock.Commands.Add(new DockExpandCollapseCommand());

        return dock;
    }

    private RadDock CreateRadDock()
    {
        int docksCount = CurrentDockStates.Count;

        RadDock dock = new RadDock();
        dock.UniqueName = Guid.NewGuid().ToString().Replace("-", "");
        dock.ID = string.Format("RadDock{0}", dock.UniqueName);
        dock.Title = "Dock";
        dock.Text = string.Format("Added at {0}", DateTime.Now);
        dock.Width = Unit.Pixel(300);

        dock.Commands.Add(new DockCloseCommand());
        dock.Commands.Add(new DockExpandCollapseCommand());
        dock.Command += new DockCommandEventHandler(dock_Command);

        return dock;
    }

    void dock_Command(object sender, DockCommandEventArgs e)
    {
        if (e.Command.Name == "Close")
        {
//            ScriptManager.RegisterStartupScript(Page, typeof(Page), "RemoveDock",
//                string.Format(@"function _removeDock() {{ $find('{0}').undock();
//                    $get('{1}').appendChild($get('{0}'));
//                    $find('{0}').doPostBack('DockPositionChanged');
//                }};", ((RadDock)sender).ClientID, RadDockZone1.ClientID), true);
        }
    }

    private void CreateSaveStateTrigger(RadDock dock)
    {
        dock.AutoPostBack = true;
        dock.CommandsAutoPostBack = true;
    }

    private void LoadWidget(RadDock dock)
    {
        if (string.IsNullOrEmpty(dock.Tag))
        {
            return;
        }
        Control widget = LoadControl(dock.Tag);
        dock.ContentContainer.Controls.Add(widget);
    }

    protected void ButtonAddDock_Click(object sender, EventArgs e)
    {
        RadDock dock = CreateRadDock();
        RadDockLayout1.RegisteredZones[DropDownZone.SelectedIndex].Controls.Add(dock);

        CreateSaveStateTrigger(dock);

        dock.Tag = DroptDownWidget.SelectedValue;
        LoadWidget(dock);
    }
}
