namespace mfs {
    internal class tlg_table {
        public enum extrap_type {
            EXTRAP_LINEAR,
            EXTRAP_NONE,
            EXTRAP_NEAREST
        }
        public string name = "";
        public extrap_type extrap_method = extrap_type.EXTRAP_LINEAR;
        public string[] indeps;
        public string[] deps;
        public string[] multiply_output_by;
        public double[] multiply_output_by_num;
        public bool scalar_multiplier;
        public bool regular;

        public tlg_table(StreamReader fs) {
            while (!fs.EndOfStream) {
                string line = fs.ReadLine();
                line = line.Trim();

                if (string.IsNullOrWhiteSpace(line) || (line[0] == '%')) {
                    // skip blank or whitespace lines
                    continue;
                }

                if (line == "END BLOCK") {
                    // end block processing on end of block
                    // turn our data points into a grid first
                    grid_data();
                    return;
                }
                string[] splitline = line.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

                if (splitline.Length < 2) {
                    throw new Exception("Syntax error at keyword " + splitline[0] + " in file " + fs.ToString());
                }

                switch (splitline[0].ToLowerInvariant()) {
                    // switch on first keyword of line
                    case "name":
                        name = splitline[1];
                        break;

                    case "extrapolation":
                        switch (splitline[1].ToLowerInvariant()) {
                            case "linear":
                                extrap_method = extrap_type.EXTRAP_LINEAR;
                                break;

                            case "nearest":
                                extrap_method = extrap_type.EXTRAP_NEAREST;
                                break;

                            case "none":
                                extrap_method = extrap_type.EXTRAP_NONE;
                                break;

                            default:
                                throw new Exception("Unknown extrapolation method " + splitline[1]);
                        }
                        break;

                    case "independents":
                        indeps = splitline[1..];
                        break;

                    case "dependents":
                        deps = splitline[1..];
                        break;

                    case "multiplyoutputby":
                        double mult;
                        multiply_output_by_num = new double[splitline.Length - 1];
                        multiply_output_by = new string[splitline.Length - 1];

                        for (int i = 1; i < splitline.Length; i++) {
                            if (double.TryParse(splitline[i], out mult)) {
                                // MultiplyOutputBy is a scalar
                                multiply_output_by_num[i - 1] = mult;
                                scalar_multiplier = true;
                            } else {
                                multiply_output_by[i - 1] = splitline[i];
                                scalar_multiplier = false;
                            }
                        }
                        break;

                    default:
                        break;
                }
            }
            
        }

        private void grid_data() {

        }
    }
}


