using System;
using System.Net;
using Winbooks.Apis.Services;
using Winbooks.Apis.Services.v2;
using System.IO;

namespace Winbooks.Apis.Demo
{
    public class Program
    {
        static void Main(string[] args)
        {
            string url, email, exchangeToken, folderCode;

            // url
            //
            // Staging environment
            // https://stg.winbooksonweb.be
            // https://stg.winbooksapis.be/wow/v1 (2.4)
            // https://stg.winbooksapis.be/wow/v2 (2.5)
            //
            // Production environment
            // https://www.winbooksonweb.be
            // https://prd.winbooksapis.be/wow/v1 (2.4)
            // https://prd.winbooksapis.be/wow/v2 (2.5)

            url = "https://stg.winbooksapis.be/wow/v2";
            email = "jfc@winbooks.be";
            exchangeToken = "MuwdDAFNDKTqRwK...c5YJx8t7RVcRMas06PxT2g";
            folderCode = "JF_TEST_RESTAPI";

            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                   | SecurityProtocolType.Tls11
                   | SecurityProtocolType.Tls12
                   | SecurityProtocolType.Ssl3;

            UserCredential userCredential = new UserCredential(url, email, exchangeToken);
            
            // put a log file name to generate a log with the json example
            userCredential.LogFile = "c:\\temp\\_mytestrapi.log";
            if (File.Exists(userCredential.LogFile))
                File.Delete(userCredential.LogFile);

            bool noEntitiesServices = false;
            if (noEntitiesServices)
            {
                // In this case, we just use the UserCredential class of the Winbooks.Apis.Services which allows to Authenticate and Get/Post/Put/Delelete the http request
                // We do not use the OM and the (de)serialization of the Services, so the application has to serialize and deserialize by itself

                if (!userCredential.Authentication())
                {
                    Console.WriteLine("Cannot authenticate");
                }
                else
                {
                    string demo = "GetDocumentsHttp";
                    string resultMsg = "";
                    switch (demo)
                    {
                        case "GetDocumentsHttp":
                            resultMsg = GetDocumentsHttp.MainExecute(url, userCredential);
                            break;
                    }
                    Console.WriteLine("");
                    Console.WriteLine(resultMsg);
                }
                Console.WriteLine("Type any key ...");
                Console.Read();
            }
            else
            {
                // In this case, we use all facilities offered by the Winbooks.Apis.Services including the (de)serialization allowing to work directly with the OM of Wow

                Tools.LogFile = userCredential.LogFile;

                Console.WriteLine("-------------------------BEGIN------------------------");
                Console.WriteLine();
                Console.WriteLine("********* CREATE ACCESS TO FOLDER *********");
                Console.WriteLine();
                //THIS CREATE AN ACCESS TO FOLDER PARFIWEB
                Console.WriteLine("Getting the access to folder " + folderCode);
                EntitiesServices folder = new EntitiesServices(userCredential, folderCode);

                bool isCredentialOK = folder.CheckingAccess();
                if (isCredentialOK)
                {
                    Console.WriteLine("Getting the access to folder " + folderCode + " : OK");
                }
                else
                {
                    Console.WriteLine("Getting the access to folder " + folderCode + " : FAIL");
                    Console.ReadLine();
                    return;
                }

                string demo = "linker";
                string resultMsg = "";
                switch (demo)
                {
                    case "main":
                        resultMsg = MainExample.MainExecute(folder);
                        break;
                    case "bank":
                        resultMsg = PostBankCash.MainExecute(folder);
                        break;
                    case "linker":
                        resultMsg = PostViaLinker.MainExecute(folder, userCredential);
                        break;
                    case "invoice":
                        resultMsg = PostInvoice.MainExecute(folder);
                        break;
                    case "test":
                        resultMsg = Test.MainExecute(folder);
                        break;
                    case "http":
                        resultMsg = PostSalesBasic.MainExecute(folder, userCredential);
                        break;
                }

                Console.WriteLine("");
                Console.WriteLine(resultMsg);
                Console.WriteLine("Type any key ...");
                Console.Read();
            }
        }


    }
}
