

using System;
using System.Collections.Generic;

namespace wojilu.Web.Mvc.Routes {

    internal class RouteParser {

        public static List<RouteSetting> ParseConfig( String configContent ) {

            List<RouteSetting> results = new List<RouteSetting>();
            if (strUtil.IsNullOrEmpty( configContent )) return results;

            String[] arrLines = configContent.Trim().Split( '\n' );
            foreach (String line in arrLines) {

                if (strUtil.IsNullOrEmpty( line )) continue;
                if (line.Trim().StartsWith( "//" )) continue;

                RouteSetting route = getRoute( line );
                results.Add( route );
            }

            return results;
        }

        private static RouteSetting getRoute( String line ) {

            String[] arrItem = line.Trim().Split( new char[] { ';' } );

            String url = strUtil.TrimStart( arrItem[0], "url=>" ).Trim();
            RouteSetting setting = new RouteSetting();
            setting.setPath( url );
            setting.SplitPath();

            if ((arrItem.Length > 1) ) {
                addRouteInfo( arrItem, setting );
            }

            return setting;
        }

        private static void addRouteInfo( String[] arrItem, RouteSetting setting ) {

            Dictionary<String, RouteOtherInfo> dic = new Dictionary<String, RouteOtherInfo>();

            for (int i = 1; i < arrItem.Length; i++) {

                if (strUtil.IsNullOrEmpty( arrItem[i] )) continue;

                RouteOtherInfo info = RouteOtherInfo.Parse( arrItem[i] );
                if (info != null) dic.Add( info.getName(), info );

            }

            setting.setRouteOtherInfo( dic );

            if (dic.ContainsKey( "default" )) {
                setting.setDefaultValue( RouteOtherInfo.getDefaultValue( dic["default"] ) );
            }

        }


    }
}

