﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCommon;
using OpenPrint.BusModel;

namespace OpenPrint
{
    public class CleanData
    {
        public static void GenerateModel()
        {
            CityBus bjBus = new CityBus("北京");
            string folder = Program.RootPath +"\\RawData\\";
            string[] files = Directory.GetFiles(folder);
            bjBus.计算路线表和索引(files);
            Console.WriteLine("Total {0} files", files.Length);
            int successCount = 0;
            int failedCount = 0;
            int skippedCount = 0;

            foreach (var f in files)
            {
                string pureFileName = new FileInfo(f).Name;
                if(! bjBus.路线索引按文件.ContainsKey(pureFileName))
                {
                    skippedCount++;
                    continue;
                }
                BusRouteBase baseRoute = bjBus.路线索引按文件[pureFileName];
                BusRouteDetail bus = new BusRouteDetail(baseRoute);
                if (bus.ParseAndClean(f, baseRoute))
                {
                    successCount++;
                    bjBus.路线.Add(baseRoute, bus);
                }
                else
                    failedCount++;

                if (successCount >= 10)
                {
                    //break;
                }
            }

            bjBus.CountStation();

            Console.WriteLine("Succ {0} + Skip {1} + Failed {2} of {3}", successCount, skippedCount, failedCount, files.Length);



            using (StreamWriter sw = new StreamWriter(Program.RootPath + "\\输出统计\\模型" + bjBus.城市名 + ".xml", false, Encoding.UTF8))
            {
                sw.WriteLine(XmlSerializeUtil.ToXml<CityBus>(bjBus));
            }

            XmlSerializeUtil.WriteToBinaryFile<CityBus>(Program.RootPath + "\\输出统计\\模型" + bjBus.城市名 + ".bin", bjBus);
        }
    }
}
