﻿using System;
using GCSUploader.Core;
using Newtonsoft.Json;

namespace GCSUploader.Console
{
    class Program
    {
        static void Main(string[] args)
        {

            /**
             * -x -l -c -t ExtractAndUpload -p DefaultProject -u gs://dwh-exports-gcloud-testing/facts/FactProductInventory/ -s DataWarehouse -q "SELECT * FROM [AdventureWorksDW2014].[dbo].[FactProductInventory]" -n FactProductInventory -f NEWLINE_JSON
             * -x -c -t Extract -s DataWarehouse -q "SELECT * FROM [AdventureWorksDW2014].[dbo].[FactProductInventory]" -n FactProductInventory -f NEWLINE_JSON -m 200000
             * -l -t Upload -p DefaultProject -u gs://dwh-exports-gcloud-testing/facts/FactProductInventory/ -n FactProductInventory
             */

            var options = new Options();
            if (CommandLine.Parser.Default.ParseArguments(args, options))
            {
                var v = options.Validate();

                Logger.Info(v.InfoMessages);
                
                if(!v.IsValid)
                    Logger.Error(v.ErrorMessages);

                if (v.IsValid)
                {
                    try
                    {
                        var uploader = new Uploader(options);

                        uploader.OnUploadsComplete += uploader_UploadsComplete;

                        uploader.Process();
                    }
                    catch (Exception e)
                    {
                        Logger.Error(JsonConvert.SerializeObject(e, Formatting.Indented));
                    }

                }
            }
            System.Console.Read();
        }

        static void uploader_UploadsComplete(string message)
        {

            Logger.Info("");
            Logger.Info(message);

            System.Threading.Thread.Sleep(5000);

            Environment.Exit(0);
        }
    }
}
