/*
 * VibratoConfig.cs
 * Copyright (c) 2009 kbinani
 *
 * This file is part of Boare.Lib.Vsq.
 *
 * Boare.Lib.Vsq is free software; you can redistribute it and/or
 * modify it under the terms of the BSD License.
 *
 * Boare.Lib.Vsq is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
using System;
using System.IO;

namespace Boare.Lib.Vsq {

    public class VibratoConfig {
        public int number;
        public String file;
        public String author;
        public String vendor;
        public VibratoHandle contents;

        public VibratoConfig() {
            contents = new VibratoHandle();
        }

        public void parseAic( String aic_file ) {
            using ( StreamReader sr = new StreamReader( aic_file ) ) {
                String line;
                String current_entry = "";
                String articulation = "";
                String depth_bpx = "";
                String depth_bpy = "";
                String rate_bpx = "";
                String rate_bpy = "";
                int depth_bpnum = 0;
                int rate_bpnum = 0;
                while ( (line = sr.ReadLine()) != null ) {
                    if ( line.StartsWith( "[" ) ) {
                        current_entry = line;
                        continue;
                    } else if ( line == "" || line.StartsWith( ";" ) ) {
                        continue;
                    }

                    String[] spl = line.Split( new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries );
                    if ( spl.Length < 2 ) {
                        continue;
                    }
                    spl[0] = spl[0].Trim();
                    spl[1] = spl[1].Trim();
                    if ( current_entry == "[Common]" ) {
                        if ( spl[0] == "Articulation" ) {
                            articulation = spl[1];
                        }
                    } else if ( current_entry == "[Parameter]" ) {
                        if ( spl[0] == "Length" ) {
                            try {
                                this.contents.Length = int.Parse( spl[1] );
                            } catch { }
                        } else if ( spl[0] == "StartDepth" ) {
                            try {
                                this.contents.StartDepth = int.Parse( spl[1] );
                            } catch { }
                        } else if ( spl[0] == "DepthBPNum" ) {
                            try {
                                depth_bpnum = int.Parse( spl[1] );
                            } catch { }
                        } else if ( spl[0] == "DepthBPX" ) {
                            depth_bpx = spl[1];
                        } else if ( spl[0] == "DepthBPY" ) {
                            depth_bpy = spl[1];
                        } else if ( spl[0] == "StartRate" ) {
                            try {
                                this.contents.StartRate = int.Parse( spl[1] );
                            } catch { }
                        } else if ( spl[0] == "RateBPNum" ) {
                            try {
                                rate_bpnum = int.Parse( spl[1] );
                            } catch { }
                        } else if ( spl[0] == "RateBPX" ) {
                            rate_bpx = spl[1];
                        } else if ( spl[0] == "RateBPY" ) {
                            rate_bpy = spl[1];
                        }
                    }
                }
                if ( articulation != "Vibrato" ) {
                    return;
                }

                // depth bp
                if ( depth_bpnum > 0 && depth_bpx != "" && depth_bpy != "" ) {
                    String[] bpx = depth_bpx.Split( ',' );
                    String[] bpy = depth_bpy.Split( ',' );
                    if ( depth_bpnum == bpx.Length && depth_bpnum == bpy.Length ) {
                        float[] x = new float[depth_bpnum];
                        int[] y = new int[depth_bpnum];
                        try {
                            for ( int i = 0; i < depth_bpnum; i++ ) {
                                x[i] = float.Parse( bpx[i] );
                                y[i] = int.Parse( bpy[i] );
                            }
                            this.contents.DepthBP = new VibratoBPList( x, y );
                        } catch { }
                    }
                }

                // rate bp
                if ( rate_bpnum > 0 && rate_bpx != "" && rate_bpy != "" ) {
                    String[] bpx = rate_bpx.Split( ',' );
                    String[] bpy = rate_bpy.Split( ',' );
                    if ( rate_bpnum == bpx.Length && rate_bpnum == bpy.Length ) {
                        float[] x = new float[rate_bpnum];
                        int[] y = new int[rate_bpnum];
                        try {
                            for ( int i = 0; i < rate_bpnum; i++ ) {
                                x[i] = float.Parse( bpx[i] );
                                y[i] = int.Parse( bpy[i] );
                            }
                            this.contents.RateBP = new VibratoBPList( x, y );
                        } catch { }
                    }
                }
            }
        }
    }

}
