using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Data.SqlClient;

using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Util;

namespace ms.aq.cview.util {
    public class table_cube_interface{
        public string TableName;
        long wsid;
        CasJobs cj = new CasJobs();
        public table_cube_interface(long wsid) {
            this.wsid = wsid;
        }
        public VHierarchy[] GetHierarchies(VDimension vd) { throw new NotImplementedException(); }
        public VHierarchy GetHierarchy(string s) { throw new NotImplementedException(); }
        public VLevel[] GetLevels(VHierarchy vh) { throw new NotImplementedException(); }
        public VLevel GetLevel(string s) { throw new NotImplementedException(); }
        public VMember[] GetLevelMembers(VLevel vl, int i) { throw new NotImplementedException(); }
        public VMember[] GetMembers(VHierarchy vh, int i) { throw new NotImplementedException(); }
        public string Hierachy2Column(string hid, out string aname, Hashtable ahs) {
            aname = "a";
            return hid;
        }
        public string Hierarchy2Table(string hid, VDimension parentD) {
            return TableName;
            if (parentD.Type == VDType.MEASURE)//the second token is the name of the table; [Measures].[APAR] references APAR
                return "sqldata";
            else//the first token is the name of the table; [Site].[Name] references Site
                return common.debracket(common.UID2Tokens(hid)[0]);
        }
        
        public DataSet ExecuteQuery(string query) {
            try {
                return cj.SubmitShortJob(wsid, query, "MyDB", edu.jhu.Cas.Util.SYSTEM.TASKNAME + " BP QUERY", 0);
            //}catch(Exception e){throw new Exception(e.Message+"\n"+query);}
            }
            catch (Exception e) { throw; }
        }
        public IDataReader ExecuteReader_REMOVED(string query) {
            throw new NotImplementedException();
        }
        public VDimension GetDimension(string dname) {
            string q = "select * from dimensions where name = '"+common.debracket(dname)+"'";
            DataSet ds = ExecuteQuery(q);
            return new VDimension(ds.Tables[0].Rows[0]["name"].ToString(),
               object2vdtype(ds.Tables[0].Rows[0]["type"]),
               ds.Tables[0].Rows[0]["relations"].ToString(), ds.Tables[0].Rows[0]["pkeyh"].ToString());
        }
        public VDimension[] GetDimensions() {
            string q = @"
SELECT relations=null,
	name=syscolumns.name,
	type=0,
	datatype=systypes.name,
	pkeyh=null
FROM sysobjects 
	JOIN syscolumns ON sysobjects.id = syscolumns.id
	JOIN systypes ON syscolumns.xtype=systypes.xtype
WHERE sysobjects.xtype='U' and sysobjects.name = '"+TableName+@"'
ORDER BY sysobjects.name,syscolumns.colid
            ";
            DataSet ds = ExecuteQuery(q);
            VDimension[] rst = new VDimension[ds.Tables[0].Rows.Count];
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                rst[i] = new VDimension(ds.Tables[0].Rows[i]["name"].ToString(),
                    object2vdtype(ds.Tables[0].Rows[i]["type"]),
                    ds.Tables[0].Rows[i]["relations"].ToString(), ds.Tables[0].Rows[i]["pkeyh"].ToString());
            return rst;
        }
        public string[] GetTables() {
            return (new MyDBJobs()).Tables(wsid);
        }
        private VDType object2vdtype(object o) {
            switch (int.Parse(o.ToString())) {
                case 0: return VDType.SHALLOW;
                case 1: return VDType.DEEP;
                case 2: return VDType.MEASURE;
                default: throw new Exception("Unknown VDType " + o);
            }
        }
        private VHType object2vhtype(object o) {
            switch (int.Parse(o.ToString())) {
                case 0: return VHType.SHALLOW;
                case 1: return VHType.DEEP;
                default: throw new Exception("Unknown VHType " + o);
            }
        }
     
    }
}