using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Specialized;

using ms.aq.cview.util;
using ms.aq.cview.query;
using edu.jhu.Cas.Web;

namespace ms.aq.cview {
    /// <summary>
    /// Summary description for Admin.
    /// </summary>
    public class load_members : System.Web.UI.Page {
        protected string members = null;
        protected string uid;
        protected string prefix;
        protected string sel;
        protected string onload;
        protected string newseries, atab;
        table_cube_interface tci;
        bool loggedin;
        long wsid;

        override protected void OnInit(EventArgs e) {
            InitializeComponent();
            base.OnInit(e);
        }
        private void InitializeComponent() {
            WebUtils.CheckLoggedIn(Session, Response, Request);

            loggedin = Session["USER"] != null;
            wsid = (long)Session["USER"];
            
            prefix = Request.Params["prefix"];
            atab = Request.Params["atab"];
            tci = new table_cube_interface(wsid);
            if (atab != null) {
                Session[preview.BASETABLE] = atab;
                SelectionSets ss = Session[Settings.SESSION_SS_KEY] as SelectionSets;
                if (ss != null) Session[Settings.SESSION_SS_KEY] = new SelectionSets();
            }
            else atab = Session[preview.BASETABLE] as string;
            tci.TableName = atab;
            PrefixDecoder pd = new PrefixDecoder(prefix);
            if (Request.Params["loadptype"] != null) {
                switch (Request.Params["type"]) {
                    case PrefixDecoder.P1:                       
                        members = html_factory.GetP1HTML(tci);
                        break;
                    default:
                        throw new Exception("Unknown plot type '" + Request.Params["loadptype"] + "'");
                }
                onload = "window.parent.ptm.SetPlotType();window.parent.ceb.ReToggleAll();";
                return;
            }
            if (Request.Params["loadseries"] != null) {
                members = html_factory.GetP1SeriesHTML(tci,pd.SeriesNum);
                onload = "window.parent.sm.SetNewSeries('"+prefix+"');";
                return;
            }
            uid = Request.Params["uid"];
            sel = Request.Params["sel"];
            newseries = Request.Params["loadconditions"];

            StringCollection tokens = common.UID2Tokens(uid);
            switch (tokens.Count) {
                case 0:
                    throw new Exception("not enough tokens");
                case 1:// this is a dimension                   
                    VDimension vd = tci.GetDimension(uid);
                    string deepHList;

                    members = html_factory.GetDimensionHTML(tci, out deepHList, prefix,
                        vd, !pd.ShouldExpandHierarchy, pd.HierarchiesAreSelectable);
                    onload+= "window.parent.ceb.SetMembers('" + prefix + "p_" + uid +
                        "');";
                    break;
                case 2:// this is a hierarchy
                    VHierarchy h = tci.GetHierarchy(uid);
                    members = html_factory.GetHierarchyHTML(tci, prefix, h);
                    onload+= "window.parent.ceb.SetMembers('" + prefix + "p_" + uid + "');";
                    break;
                default:
                    throw new Exception(tokens.Count + " tokens nyi");
            }
        }
    }
}
