using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qlik.Engine;
using System.IO;

namespace QVD2QVX
{
    class Program
    {
        private static execConfig config = new execConfig();
        public class execConfig
        {
            public string sourceFile { get; set; }
        }
        static int ReadParameters(string[] args)
        {
            int result = 0;

            config.sourceFile = "";            

            if (args.Count() < 1)
            {
                Console.WriteLine(String.Format("FINISH STATUS=ERROR. " + "FINISH STATUS=ERROR. " + "Parameters missing"));
                return 10;
            }

            config.sourceFile = args[0];

            return result;
        }

        static void Usage()
        {
            Console.WriteLine(@"");
            Console.WriteLine(@"===QVX2QVX===");
            Console.WriteLine(@"");
            Console.WriteLine(@"QVD2QVX");
            Console.WriteLine(@"");
            Console.WriteLine(@"QVX2QVX file.qvd");
            Console.WriteLine(@"");
        }


        static void Main(string[] args)
        {
            int result = ReadParameters(args);
            if (result != 0)
            {
                Usage();
                return;
            }

            ILocation location = Qlik.Engine.Location.FromUri(new Uri("ws://127.0.0.1:4848"));
            string AppName = "QVD2QVX";
            string ConnName = AppName;
            string ConnName2 = AppName + "2";

            Console.WriteLine("SOURCE:" + config.sourceFile);

            string filename = Path.GetFileNameWithoutExtension(config.sourceFile);
            string path = Path.GetDirectoryName(config.sourceFile);


            // Defining the location as a direct connection to Qlik Sense Personal
            location.AsDirectConnectionToPersonalEdition();
            IAppIdentifier newAppIdentifier = location.AppWithNameOrCreate(AppName);
            //var Identifiers = location.GetAppIdentifiers();
            //IAppIdentifier newAppIdentifier = Identifiers.FirstOrDefault(c => c.AppName == AppName + ".qvf");
            //if (newAppIdentifier == null)
            //{
            //    newAppIdentifier = location.CreateAppWithName(AppName);
            //}
            IApp application = location.App(newAppIdentifier);

            Connection Conn = application.GetConnections().FirstOrDefault(c => c.Name == ConnName);

            if (Conn == null)
            {

                Conn = new Qlik.Engine.Connection();

                Conn.Name = ConnName;
                Conn.Type = "folder";
                Conn.ConnectionString = path;

                application.CreateConnection(Conn);
            }

            Connection Conn2 = application.GetConnections().FirstOrDefault(c => c.Name == ConnName2);

            if (Conn2 == null)
            {

                Conn2 = new Qlik.Engine.Connection();

                Conn2.Name = ConnName2;
                Conn2.Type = "folder";
                Conn2.ConnectionString = Directory.GetCurrentDirectory();

                application.CreateConnection(Conn2);
            }

            string currentScript = application.GetScript();

            string newscript = "";

            newscript = "SET ThousandSep=',';";
            newscript = newscript + "SET DecimalSep='.';";
            newscript = newscript + "SET MoneyThousandSep=',';";
            newscript = newscript + "SET MoneyDecimalSep='.';";
            newscript = newscript + "SET MoneyFormat='XDR#,##0.00;-XDR#,##0.00';";
            newscript = newscript + "SET TimeFormat='hh:mm:ss';";
            newscript = newscript + "SET DateFormat='D/M/YYYY';";
            newscript = newscript + "SET TimestampFormat='D/M/YYYY hh:mm:ss[.fff]';";
            newscript = newscript + "SET FirstWeekDay=0;";
            newscript = newscript + "SET BrokenWeeks=1;";
            newscript = newscript + "SET ReferenceDay=0;";
            newscript = newscript + "SET FirstMonthOfYear=1;";
            newscript = newscript + "SET CollationLocale='es-419';";
            newscript = newscript + "SET MonthNames='Ene.;Feb.;Mar.;Abr.;May.;Jun.;Jul.;Ago.;Sept.;Oct.;Nov.;Dic.';";
            newscript = newscript + "SET LongMonthNames='Enero;Febrero;Marzo;Abril;Mayo;Junio;Julio;Agosto;Septiembre;Octubre;Noviembre;Diciembre';";
            newscript = newscript + "SET DayNames='lun.;mar.;mié.;jue.;vie.;sáb.;dom.';";
            newscript = newscript + "SET LongDayNames='lunes;martes;miércoles;jueves;viernes;sábado;domingo';";

            newscript = newscript + "TABLA:LOAD * FROM [lib://" + ConnName + "/" + filename + ".QVD](qvd);STORE TABLA INTO [lib://" + ConnName2 + "/" + filename + ".QVX] (QVX)";

            application.SetScript(newscript);

            var didReload = application.DoReload();

            application.DoSave();
            bool didDeleteApp = location.Delete(newAppIdentifier);

        }
    }
}
