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

using ms.aq.cview.query;
using ms.aq.cview.util;
using ms.aq.cview.draw;

namespace ms.aq.cview.query {
    /*
     * for generating html sections that describe what has been selected
     */
    public abstract class PInfoFactory {
        private static void DumpRequiredSingleSelection(StringBuilder sb, string id, SelectionSets ss) {
            if (ss.GetCollection(id).Count > 0)
                sb.Append(common.debracket((ss.GetCollection(id)[0])));
            else
                sb.Append("<font style=\"color:Red\">Selection Required</font>");
        }
        /* for converting uids that specify values ie [Measures].[Apar].[234] */
        private static string UIDValue2Text(string uid) {
            StringCollection tmp =  common.UID2Tokens(uid);
            if (tmp.Count != 3) throw new Exception("uh oh, dunno how to handle this");
            return common.debracket(tmp[1]) + " = " + common.debracket(tmp[2]);            
        }
        private static void DumpP1HTML(table_cube_interface ci, SelectionSets ss, StringBuilder sb, Serie[] legend, int rows,
            double eSeconds,double rSeconds,out bool canPlot) {
            bool hasRange = false, hasSeries = false;
            sb.Append("<div id=\"plotSummary\" style=\"margin:2 0 0 2;\">");
            sb.Append("<table cellpadding=\"5\" cellspacing=\"0\">");
            ICollection ic = ss.GetPrefixes(PrefixDecoder.P1_SERIES_PREFIX);
            
            ArrayList al = new ArrayList();
            foreach (object o in ic)
                al.Add(o);
            al.Sort();//hooRAY lazy sort

            StringCollection series = new StringCollection();
            foreach (object o in al)
                series.Add((string)o);

            Hashtable ssKey2Legend = null;
            if (legend != null) {
                ssKey2Legend = new Hashtable();
                foreach (Serie ser in legend)
                    ssKey2Legend.Add(common.sNumFromCAlias(ser.Name), ser);
            }
            
            
            sb.Append("<tr><th colspan=\"" + 2 + series.Count + 
                "\">Plot Summary</th></tr>");
            if (rows > -1) {
                sb.Append("<tr><th colspan=\"" + 2 + series.Count + "\"><h5>"+
                    rows+" points</h5></th></tr>");
            }
            sb.Append("<tr><td></td><td>Range</td>");
            if (series.Count == 0) sb.Append("<td>Series</td>");
            foreach(string s in series){
                int seriesNum = PrefixDecoder.SeriesNumFromPrefix(s);
                if(ss.GetCollection(s).Count > 0)
                    if (legend != null) {
                        sb.Append("<td style=\"color:#eeeeff;background-color:" + ((Serie)ssKey2Legend[seriesNum]).WColor.ToString() + "\">Series" + seriesNum + "</td>");
                    }else sb.Append("<th>Series" + seriesNum + "</th>");
            }

            if (rows > -1) {
                sb.Append("<tr><th>Points</th><th></th>");
                foreach(string s in series){
                    int seriesNum = PrefixDecoder.SeriesNumFromPrefix(s);
                    if (ss.GetCollection(s).Count > 0)
                        if (legend != null) {
                            sb.Append("<td>" + ((Serie)ssKey2Legend[seriesNum]).SCount + "</td>");
                        } else sb.Append("<th>Series" + seriesNum + "</th>");
                }
                sb.Append("</tr>");
            }
  
            sb.Append("</tr><tr><th></th>");
            sb.Append("<td>");
            hasRange = ss.GetCollection(PrefixDecoder.P1_RANGE_PREFIX).Count > 0;
            DumpRequiredSingleSelection(sb, PrefixDecoder.P1_RANGE_PREFIX, ss);
            sb.Append("</td>");
            if (series.Count == 0)
                sb.Append("<td><font style=\"color:Red\">Selection Required</font></td>");
            foreach (string s in series) {
                int seriesNum = PrefixDecoder.SeriesNumFromPrefix(s);
                StringCollection sc = ss.GetCollection(s);
                if (sc.Count > 0) {
                    sb.Append("<td>" + common.debracket(sc[0]) + "</td>");
                    hasSeries = true;
                }
            }
            canPlot = hasRange && hasSeries;
            sb.Append("</tr>");
            VDimension[] vds = ci.GetDimensions();
            foreach (VDimension vd in vds) {
                if (vd.Type == VDType.MEASURE) continue;
                sb.Append("<tr><th>");
                sb.Append(vd.Name);
                sb.Append("</th>");
                sb.Append("<td>&nbsp;</td>");//skipping range
                foreach (string s in series) {
                    int seriesNum = PrefixDecoder.SeriesNumFromPrefix(s);
                    StringCollection sc = ss.GetCollection(PrefixDecoder.P1_CONSTRAINT_PREFIX+ seriesNum);
                    StringCollection scTMP = ss.GetCollection(s);
                    string dimensionSelections = "";
                    if (scTMP.Count == 0) continue;
                    sb.Append("<td>");
                    foreach (string s1 in sc) {
                        if (s1.StartsWith(vd.UID))
                            dimensionSelections += "<div>" + UIDValue2Text(s1) + "</div>";
                    }
                    if (dimensionSelections == "") dimensionSelections = "All";
                    sb.Append(dimensionSelections);
                    sb.Append("</td>");
                }
                sb.Append("</tr>");
            }
            if (eSeconds != -1) {
                sb.Append("<tr><th colspan=\"" + 2 + series.Count + "\">Query Time: " + eSeconds
                    + " seconds</th></tr>");
                sb.Append("<tr><th colspan=\"" + 2 + series.Count + "\"><h5>Render Time: " + rSeconds
                    + " seconds</h5></th></tr>");
            }
            
            sb.Append("</table>");
        }
        public static string SS2HTML(table_cube_interface ci, SelectionSets ss, Serie[] legend, int rows,
            double eSeconds, double rSeconds, out bool canPlot) {
            canPlot = false;
            if (ss == null) return "<div>Nothing selected.</div>";
            StringBuilder sb = new StringBuilder();
            sb.Append("<div id=\"plotSummary\">");            
            switch (ss.PlotType) {
                case PrefixDecoder.P1:
                    DumpP1HTML(ci, ss, sb, legend, rows, eSeconds, rSeconds, out canPlot);
                    break;
                default:
                    throw new Exception("UNKNOWN PLOT TYPE");
            }
            sb.Append("</div>");
            return sb.ToString();
        }
    }
}
