using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data; 

public partial class _Default : System.Web.UI.Page 
{
    public DataTable treeDataTable;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            //create datatable to fill tree 
            DataRow row;
            treeDataTable = new DataTable();

            treeDataTable.Columns.Add("Selected");
            treeDataTable.Columns.Add("Market");
            treeDataTable.Columns.Add("Site");
            treeDataTable.Columns.Add("Sector");

            row = treeDataTable.NewRow();
            row["Selected"] = "0"; row["Market"] = "Cincinnati"; row["Site"] = "1"; row["Sector"] = "1";
            treeDataTable.Rows.Add(row);

            row = treeDataTable.NewRow();
            row["Selected"] = "0"; row["Market"] = "Cincinnati"; row["Site"] = "1"; row["Sector"] = "2";
            treeDataTable.Rows.Add(row);

            row = treeDataTable.NewRow();
            row["Selected"] = "0"; row["Market"] = "Cincinnati"; row["Site"] = "1"; row["Sector"] = "3";
            treeDataTable.Rows.Add(row);

            row = treeDataTable.NewRow();
            row["Selected"] = "1"; row["Market"] = "Cincinnati"; row["Site"] = "2"; row["Sector"] = "1";
            treeDataTable.Rows.Add(row);

            row = treeDataTable.NewRow();
            row["Selected"] = "1"; row["Market"] = "Cincinnati"; row["Site"] = "2"; row["Sector"] = "2";
            treeDataTable.Rows.Add(row);

            row = treeDataTable.NewRow();
            row["Selected"] = "1"; row["Market"] = "Cincinnati"; row["Site"] = "2"; row["Sector"] = "3";
            treeDataTable.Rows.Add(row);

            row = treeDataTable.NewRow();
            row["Selected"] = "0"; row["Market"] = "Cincinnati"; row["Site"] = "3"; row["Sector"] = "1";
            treeDataTable.Rows.Add(row);

            row = treeDataTable.NewRow();
            row["Selected"] = "1"; row["Market"] = "Cincinnati"; row["Site"] = "3"; row["Sector"] = "2";
            treeDataTable.Rows.Add(row);

            row = treeDataTable.NewRow();
            row["Selected"] = "0"; row["Market"] = "Cincinnati"; row["Site"] = "3"; row["Sector"] = "3";
            treeDataTable.Rows.Add(row);

            //populate tree with the data 
            populateClusterTree();

            treeUpdatePanel.Update();

        }

    }

    protected void populateClusterTree()
    {

        //clear any existing values 
        RadTreeView1.Nodes.Clear();

        //init node building vars 
        string lastMarket = treeDataTable.Rows[0]["Market"].ToString();
        string lastMarketID = "4391";
        string lastSite = "-1";
        RadTreeNode siteNode = null;
        RadTreeNode sectorNode = null;

        //add root market node 
        RadTreeNode rootNode = new RadTreeNode(lastMarket, lastMarketID);
        rootNode.Expanded = true;
        RadTreeView1.Nodes.Add(rootNode);

        //go through each element returned from query 
        foreach (DataRow clusterElementRow in treeDataTable.Rows)
        {

            //see if this row has a new site 
            if (lastSite != clusterElementRow["Site"].ToString())
            {

                //add last site to root node, if not null 
                if (siteNode != null)
                {

                    rootNode.Nodes.Add(siteNode);

                }

                //new site 
                lastSite = clusterElementRow["Site"].ToString();

                //create site node for newly encountered site 
                siteNode = new RadTreeNode(lastSite, lastMarketID + "-" + lastSite);
                siteNode.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;

            }

            //create new sector node 
            sectorNode = new RadTreeNode(clusterElementRow["Sector"].ToString(), lastMarketID + "-" + lastSite + "-" + clusterElementRow["Sector"].ToString());

            //check sector node, if appropriate 
            if (clusterElementRow["Selected"].ToString() == "1")
                sectorNode.Checked = true;

            //add sector node to last site node 
            siteNode.Nodes.Add(sectorNode);

        }

        //add last site to root node, if not null 
        if (siteNode != null)
        {

            rootNode.Nodes.Add(siteNode);

        }

    }

    protected void treeNodeExpand(object sender, RadTreeNodeEventArgs e)
    {

        //DEBUG THIS LINE 
        bool nodeChecked = e.Node.Checked;

    }

    protected void Button2_Click(object sender, EventArgs e)
    {

        foreach (ClientOperation<RadTreeNode> operation in RadTreeView1.ClientChanges)
        {

            RadTreeNode node = operation.Item;

            switch (operation.Type)
            {
                case ClientOperationType.Insert:
                    break;
                case ClientOperationType.Remove:
                    break;
                case ClientOperationType.Update:
                    UpdateClientOperation<RadTreeNode> update = (UpdateClientOperation<RadTreeNode>)operation;
                    break;
            }

        }

    } 
}
