using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using System.Web;
using System.Net;

namespace ms.aq.cview.util {
    public enum AxisTypeB {
        Independent,
        Dependent,
        Condition
    }
    /* 
     * Plot is defined in terms of independent and dependent.
     * only dimensions containing multi-level hierachies are allowed on the independent axis.
     * only dimensions not containing multi-level hierarchies are allowed on the dependent axis
     */
    public abstract class html_factory {
        public const int MEMBER_CAP = 500;

        public static string GetP1HTML(table_cube_interface ci) {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div id=\""+PrefixDecoder.P1_RANGE_PREFIX+"\" style=\"width:400;\">");//3
            sb.AppendLine("<h5 onclick=\"sm.SetSelected('" + PrefixDecoder.P1_RANGE_PREFIX +
                "');\" style=\"width:100%;\"><table cellpadding=\"0\" cellspacing=\"0\" " +
                "style=\"width:100%;\"><tr><td>Range</td>" +
                "<td style=\"width:100%;text-align:right;\" id=\"" 
                + PrefixDecoder.P1_RANGE_PREFIX + "_title\">No selection</td></tr></table></h5>");
            sb.AppendLine("<div id=\""+PrefixDecoder.P1_RANGE_PREFIX+"_pane\" sselect=\"1\" style=\"overflow:auto;\" " +
                ">");//4
            sb.AppendLine("<table class=\"cList\" cellpadding=\"0\" cellspacing = \"0\">");
            foreach (VDimension vd in ci.GetDimensions())
                DumpItem(ci, sb, vd.Name, vd.UID, PrefixDecoder.P1_RANGE_PREFIX, true, true);
            sb.AppendLine("</table>");
            
            sb.AppendLine("</ul>");
            sb.AppendLine("</div>");//c4
            sb.AppendLine("<script language=\"javascript\">window.parent.sm.Reset('p1_axis_ddl','p1Sub');</script>");
            sb.AppendLine("</div>");//c0
            return sb.ToString();
        }
        public static string GetP1SeriesHTML(table_cube_interface ci, int seriesNum) {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div id=\""+PrefixDecoder.P1_SERIES_PREFIX+seriesNum+"\">");//0

            sb.AppendLine("<h5 onclick=\"sm.SetSelected('" + PrefixDecoder.P1_SERIES_PREFIX + seriesNum +
                "');\"><table cellpadding=\"0\" cellspacing=\"0\" style=\"width:100%;\">" +
                "<tr><td style=\"white-space:nowrap\">Series "+seriesNum+
                "</td><td style=\"width:100%;text-align:right;\" id=\""
                +PrefixDecoder.P1_SERIES_PREFIX+seriesNum+"_title\">No selection</td></tr></table></h5>");
            sb.AppendLine("<div sselect=\"1\" id=\""+PrefixDecoder.P1_SERIES_PREFIX+seriesNum+"_pane\" style=\"overflow:auto\">");//2
            sb.AppendLine("<table class=\"cList\" cellpadding=\"0\" cellspacing = \"0\">");
            foreach (VDimension vd in ci.GetDimensions()) {
                DumpItem(ci, sb, vd.Name, vd.UID, PrefixDecoder.P1_SERIES_PREFIX + seriesNum, true, true);
            }
            sb.AppendLine("</table>");
            sb.AppendLine("</div>");//2
            sb.AppendLine("</div>");//0
            return sb.ToString();
        }
        

        private static bool dcontainsh(VDimension vd, StringCollection xc) {
            foreach (string s in xc) {
                StringCollection sc = common.UID2Tokens(s);
                string d = sc[0];
                if (d.CompareTo(vd.UID) == 0) return true;
            }
            return false;
        }
        public static string GetDimensionHTML(table_cube_interface ci, out string deepHList, string prefix, VDimension vd,
            bool noleaves,bool selectableHierarchies) {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<ul style=\"display:none\">");
            deepHList = null;
            VHierarchy[] vhs = ci.GetHierarchies(vd);
            if (noleaves) {
                foreach (VHierarchy h in vhs) {
                    DumpItem(ci, sb, h.Name, h.UID, prefix, true, selectableHierarchies);
                }
            } else {
                if (vd.Type == VDType.DEEP) {
                    foreach (VHierarchy h in vhs) {
                        DumpItem(ci, sb, h.Name, h.UID, prefix, true, selectableHierarchies,false);
                        deepHList = deepHList == null ? h.UID : deepHList + "|" + h.UID;
                    }
                } else {
                    foreach (VHierarchy h in vhs) {
                        DumpItem(ci, sb, h.Name, h.UID, prefix, false, selectableHierarchies,false );
                    }
                }
            }
            sb.AppendLine("</ul>");
            return sb.ToString();
        }
        public static string GetHierarchyHTML(table_cube_interface ci, string prefix, VHierarchy vh) {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<ul style=\"display:none\">");

            if (vh.Type == VHType.SHALLOW) {
                //this must be a shallow hierarchy.  skip 'all', and go straight to listing members
                foreach (VMember m in ci.GetMembers(vh, MEMBER_CAP)) {                  
                    DumpItem(ci, sb, m.Name, m.UID, prefix, true, true);
                }
            } else {
                DumpDeepHierarchy(ci, sb, vh, prefix);
            }
            sb.AppendLine("</ul>");
            return sb.ToString();
        }
        public static string GetDependentLevelHTML(table_cube_interface ci, string prefix, VLevel l, string parentUID) {
            StringBuilder sb = new StringBuilder();
            DumpDependentMeasures(prefix, ci, sb, l, parentUID, false);
            return sb.ToString();
        }
        private static void DumpDependentLevels(string prefix, table_cube_interface ci, StringBuilder sb, VHierarchy h) {
            sb.Append("<table style=\"\">");
            string display = "";
            string nextParent = null;
            VLevel[] levels = ci.GetLevels(h);
            for (int i = 0; i < levels.Length; i++) {
                display = i == 1 ? "block" : "none";
                sb.Append("<tr style=\"display:" + display + "\">");
                sb.Append("<th style=\"text-align:left;\">" + levels[i].Name + "</th>");
                sb.Append("<td id=\"" + prefix + levels[i].UID + "\" style=\"text-align:right\">");
                nextParent = DumpDependentMeasures(prefix, ci, sb, levels[i], nextParent, i != 1);
                sb.Append("</td>");
                sb.Append("</tr>");
            }
            sb.Append("</table>");
        }
        //returns current selected object
        private static string DumpDependentMeasures(string prefix, table_cube_interface ci, StringBuilder sb, VLevel l,
            string parentUID, bool shallow) {
            VMember[] members = ci.GetLevelMembers(l, MEMBER_CAP);
            string nextParent = null;
            sb.AppendLine("<select>");
            for (int i = 0; i < members.Length; i++) {
                if (i == 0) nextParent = members[i].Name;
                sb.Append("<option value=\"" + members[i].Name +
                    "\" onclick=\"setHierarchyLevel('" + prefix + "','" + l.UID + "','"
                    + members[i].Name + "');setDH('"
                    + prefix + "','" + l.ParentHierarchy.UID + "');\">");
                sb.Append(members[i].Name);
                sb.Append("</option>");
            }
            sb.Append("</select>");
            return nextParent;
        }
        private static string RemoveParentString(string parent, string child) {
            if (parent == null) throw new Exception("Parent cannot be null.");
            if (child == null) throw new Exception("Child cannot be null.");
            child = child.Trim(']').Trim('[');
            if (parent == null) return child;
            parent = parent.Trim(']').Trim('[');
            return child.Replace(parent, "").Trim().Trim('-').Trim(':');//trimming the special char's is kinda nasty...
        }
        private static void DumpDeepHierarchy(table_cube_interface ci, StringBuilder sb, VHierarchy h, string prefix) {
            sb.AppendLine("<div>Depth");
            sb.AppendLine("<select id=\"" + prefix + "R" + h.UID + "\">");
            VLevel[] levels = ci.GetLevels(h);
            foreach (VLevel l in levels) {
                if (l.Name == "(All)") continue;
                sb.Append("<option onclick=\"setDH('" + prefix + "','" + h.UID + "');\" value=\""
                    + l.UID + "\">" + l.Name + "</option>");
            }
            sb.AppendLine("</select>");
            sb.AppendLine("</div>");
            sb.AppendLine("<div id=\"" + prefix + "F" + h.UID + "\">");
            sb.AppendLine("<fieldset style=\"margin:5 5 5 5;\">");
            sb.AppendLine("<legend>From</legend>");
            DumpDependentLevels(prefix + "F", ci, sb, h);

            sb.AppendLine("</fieldset>");
            sb.AppendLine("<div id=\"" + prefix + "T" + h.UID + "\">");
            sb.AppendLine("<fieldset style=\"margin:5 5 5 5;\">");
            sb.AppendLine("<legend>To</legend>");
            DumpDependentLevels(prefix + "T", ci, sb, h);
            sb.AppendLine("</fieldset>");
            sb.AppendLine("</div>");
        }
    
        private static void DumpStickyButton(StringBuilder sb, string name, string uid, string prefix,
            bool buttonExpands) {
            string onclick = "ceb.Toggle('" + prefix + "','" + uid + "');";
            if (buttonExpands)
                onclick = "ceb.Explode('" + uid + "','" + prefix + "');" + onclick;
            string shortname = name;
            if (shortname.Length > 26) shortname = shortname.Substring(0, 26) + "...";
            //originally used an <a> instead of <input> here.  
            //but firefox would not find it when calling getElementById...
            sb.AppendLine("<input title=\"" + name + "\" id=\"" + prefix + "s_" + uid
                + "\" sel=\"0\" type=\"button\" onclick=\"" + onclick + "\" value=\"" + shortname + "\"/>");
        }
        private static void DumpItem(table_cube_interface ci, StringBuilder sb, string name, string uid, string prefix,
            bool isLeaf, bool selectable) {
            DumpItem(ci, sb, name, uid, prefix, isLeaf, selectable, false);
        }
        private static void DumpItem(table_cube_interface ci, StringBuilder sb, string name, string uid, string prefix,
            bool isLeaf, bool selectable, bool buttonExpands) {
            sb.AppendLine("\t<tr><td id=\"" + prefix + "p_" + uid + "\">");
            if (selectable) DumpStickyButton(sb, name, uid, prefix, buttonExpands);
            sb.AppendLine("</td></tr>");
        }
    }
}