﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using OopFactory.X12.Hipaa.Claims.Services;

namespace OopFactory.X12.Hipaa.ClaimParser
{
    class Program
    {
        static void Main(string[] args)
        {
            var opts = new ExecutionOptions(args);

            var service = new ClaimFormTransformationService(
                new ProfessionalClaimToHcfa1500FormTransformation("HCFA1500_Red.gif"),
                new InstitutionalClaimToUB04ClaimFormTransformation("UB04_Red.gif"),
                new DentalClaimToJ400FormTransformation("ADAJ400_Red.gif"));

            foreach (var filename in Directory.GetFiles(opts.Path, opts.SearchPattern, SearchOption.TopDirectoryOnly))
            {
                try
                {
#if DEBUG
                    FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    var parser = new X12.Parsing.X12Parser();
                    var interchange = parser.ParseMultiple(stream).First();
                    File.WriteAllText(filename + ".dat", interchange.SerializeToX12(true));
                    stream.Close();
#endif           
                    DateTime start = DateTime.Now;
                    FileStream inputFilestream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    
                    var claimDoc = service.Transform837ToClaimDocument(inputFilestream);

                    FileInfo fi = new FileInfo(filename);
                    DirectoryInfo di = new DirectoryInfo(opts.OutputPath);

                    if (opts.MakeXml)
                    {
                        string outputFilename = string.Format("{0}\\{1}.xml", di.FullName, fi.Name);

                        string xml = claimDoc.Serialize();
                        xml = xml.Replace("encoding=\"utf-16\"", "encoding=\"utf-8\"");
                        File.WriteAllText(outputFilename, xml);
                    }

                    if (opts.MakePdf)
                    {
                        string outputFilename = string.Format("{0}\\{1}.pdf", di.FullName, fi.Name);
                        using (FileStream pdfOutput = new FileStream(outputFilename, FileMode.Create, FileAccess.Write))
                        {
                            XmlDocument foDoc = new XmlDocument();
                            string foXml = service.TransformClaimDocumentToFoXml(claimDoc);
                            foDoc.LoadXml(foXml);

                            var driver = Fonet.FonetDriver.Make();
                            driver.Render(foDoc, pdfOutput);
                            pdfOutput.Close();
                        }
                    }

                    opts.WriteLine(string.Format("{0} parsed in {1}.", filename, DateTime.Now - start));
                }
                catch (Exception exc)
                {
                    opts.WriteLine(string.Format("Exception occurred: {0}.  {1}.  {2}", exc.GetType().FullName, exc.Message, exc.StackTrace));
                }
            }
        }
    }
}
