using System;
using System.Text.RegularExpressions;

namespace ms.aq.cview.util {
    public enum P2PrefixType{
        Container,
        Selection,
        Constraint,
        Relationship
    }
    public class PrefixDecoder {
        public const string P1 = "p1";//p1 is the range/series plot
        public const string P1_RANGE_PREFIX = P1+"r";
        public const string P1_SERIES_PREFIX = P1 + "s";//this prefix is append with an int specifying which series it represents
        public const string P1_CONSTRAINT_PREFIX = P1 + "c";               
        
        public const string P2 = "p2";//p2 is the scatter plot
        public const string P2_SELECTION= "s";
        public const string P2_CONSTRAINT= "c";
        public const string P2_RELATION= "r";

        //numeric modifier used to specify access.
        public const string P2_SELECTION1_PREFIX = P2 + "_1_" + P2_SELECTION;
        public const string P2_CONSTRAINT1_PREFIX = P2 + "_1_" + P2_CONSTRAINT;
        public const string P2_SELECTION2_PREFIX = P2 + "_2_" + P2_SELECTION;
        public const string P2_CONSTRAINT2_PREFIX = P2 + "_2_" + P2_CONSTRAINT;
        public const string P2_RELATION_PREFIX = P2 + "_3_" + P2_RELATION;

        public string Prefix;

        public PrefixDecoder(string prefix) { Prefix = prefix; }

        public static int SeriesNumFromPrefix(string prefix) {
            return int.Parse(prefix.Split('s')[1]);
        }
        public static string BuildP2Prefix(P2PrefixType type, int axisNum) {
            string rst = P2+"_"+axisNum+"_";
            switch (type) {
                case P2PrefixType.Constraint:rst+=(P2_CONSTRAINT);break;
                case P2PrefixType.Relationship:rst+=(P2_RELATION);break;
                case P2PrefixType.Selection:rst+=(P2_SELECTION);break;
                case P2PrefixType.Container:
                default:break;
            }
            return rst;
        }
        public bool ShouldExpandHierarchy {
            get {
                switch (PType) {
                    case P1:
                        string tmp = Prefix.Substring(0, 3);
                        switch (tmp) {
                            case P1_CONSTRAINT_PREFIX:
                                return true;
                            default:
                                return false;
                        }
                    case P2:
                        if (Prefix.EndsWith(P2_CONSTRAINT)) return true;
                        else return false;
                    default:
                        throw new Exception("unknown plot type");
                }
            }
        }
        public bool HierarchiesAreSelectable {
            get {
                switch (PType) {
                    case P1:
                        string tmp = Prefix.Substring(0, 3);
                        switch (tmp) {
                            case P1_CONSTRAINT_PREFIX:
                                return false;
                            default:
                                return true;
                        }
                    case P2:
                        if (Prefix.EndsWith(P2_CONSTRAINT)) return false;
                        else return true;
                    default:
                        throw new Exception("unknown plot type");
                }
            }
        }
        public string PType {
            get {
                return ptypeRegex.Match(Prefix).Result("$1");
            }
        }
        public int SeriesNum {
            get {
                if (Prefix.StartsWith(P1_SERIES_PREFIX))
                    return int.Parse(snumRegex.Match(Prefix).Result("$1"));
                else throw new Exception("This is not a series.");
            }
        }

        private static Regex snumRegex = new Regex("^"+P1_SERIES_PREFIX+"(?<1>\\d+)", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
        private static Regex ptypeRegex= new Regex("^(?<1>p\\d+)", RegexOptions.Compiled|RegexOptions.ExplicitCapture);
        

    }
}