using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using ms.aq.cview.util;

namespace ms.aq.cview.query {

    public class P1QueryFactory {
        private table_cube_interface tci;
        public P1QueryFactory(table_cube_interface tci) {
            this.tci = tci;
        }
        public StringCollection GetSelections(SelectionSets sSets, Hashtable ftmap, Hashtable ahs,
            object extended) {
            Hashtable series = (Hashtable)extended;
            StringCollection rst = new StringCollection();
            string aname;
            StringCollection sc = sSets.GetCollection(PrefixDecoder.P1_RANGE_PREFIX);
            if (sc.Count > 1) throw new Exception("should only be 1, is " + sc.Count);

            FromTable ft = (FromTable)ftmap[sc[0]];
            string cname = tci.Hierachy2Column(sc[0], out aname, ahs);
            rst.Add(ft.Alias + "." + cname + " as " + Settings.XDIM_PREF + aname);

            ArrayList al = sSets.GetCollectionsByPrefix(PrefixDecoder.P1_SERIES_PREFIX);
          
            StringCollection added = new StringCollection();
            sc = (StringCollection)al[0];
            string matchThis = PrefixDecoder.P1_SERIES_PREFIX + sSets.ActiveSeries;
            foreach(object key in series.Keys){
                string skey = (string)key;
                if (!series.ContainsKey(key)) continue;
                string refdSeries = ((StringCollection)series[key])[0];
                int seriesNum = PrefixDecoder.SeriesNumFromPrefix(skey);
                if(skey == matchThis){
                    ft = (FromTable)ftmap[sc[0]];
                    cname = tci.Hierachy2Column(refdSeries, out aname, ahs);
                    rst.Add(common.SqlAggLookup(sSets.Agg) + "(" + ft.Alias + "." + cname + ") as " + Settings.YDIM_PREF + aname + "_" + seriesNum);
                } else {
                    cname = tci.Hierachy2Column(refdSeries, out aname, ahs);
                    rst.Add(ft.Alias+"."+cname+" as " + Settings.YDIM_PREF + aname + "_" + seriesNum);
                }
            }

            return rst;
        }
        public string GetGroupBy(SelectionSets sSets, Hashtable ftmap, Hashtable ahs) {
            StringCollection rst = new StringCollection();
            string aname;
            StringCollection sc = sSets.GetCollection(PrefixDecoder.P1_RANGE_PREFIX);
            if (sc == null || sc.Count == 0) return "";

            FromTable ft = (FromTable)ftmap[sc[0]];
            string cname = tci.Hierachy2Column(sc[0], out aname, ahs);
            return "group by " + ft.Alias + "." + cname;
        }

        private bool CollectionListContains(ArrayList collectionlist, string token) {
            foreach (object o in collectionlist) {
                StringCollection sc = (StringCollection)o;
                foreach (string s in sc) {
                    if (s == token)
                        return true;
                }
            }
            return false;
        }

     
    }
}