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 PostQueryFactory{
        public static string BuildInitQuery(ClotTable ct,float xmin, float xmax, float ymin, float ymax){
            return null;
        }
        public static string BuildMinMax(ClotTable ct, float xmin, float xmax, float ymin, float ymax) {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("SELECT");
            for (int i = 0; i < ct.XCols.Count; i++) {
                sb.AppendLine("\t"+xmax+" as " + ct.XCols[i] + "_MAX,");
                sb.AppendLine("\t" + xmin + " as " + ct.XCols[i] + "_MIN,");
            }
            for (int i = 0; i < ct.YCols.Count; i++) {
                sb.AppendLine("\t" + ymax + " as " + ct.YCols[i] + "_MAX,");
                sb.AppendLine("\t" + ymin + " as " + ct.YCols[i] + "_MIN,");
            }
            sb.Remove(sb.Length - 3, 1);//die comma
            return sb.ToString();
        }
        public static string BuildDataQuery(ClotTable ct, float xmin, float xmax, float ymin, float ymax) {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("SELECT * FROM "+ct.TableName);
            sb.AppendLine("WHERE");
            sb.Append(BuildConstraints(ct, xmin, xmax, ymin, ymax));
            return sb.ToString();
        }
        public static string BuildFinalQuery(ClotTable ct, float xmin, float xmax, float ymin, float ymax) {
            return null;
        }
        private static string BuildConstraints(ClotTable ct, float xmin, float xmax, float ymin, float ymax) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < ct.XCols.Count; i++) {
                sb.AppendLine(ct.XCols[i] + " between " + xmin + " and " + xmax + " AND");
            }
            for (int i = 0; i < ct.YCols.Count; i++) {
                sb.Append(ct.YCols[i] + " between " + ymin + " and " + ymax);
                if (i < ct.YCols.Count - 1)
                    sb.Append(" AND");
                sb.AppendLine();
            }
            return sb.ToString();
        }
    }
}