using System;
using System.IO;
using System.Collections.Generic;
using uri_import.EClasses;
using uri_import.FClasses;
using uri_import.AClasses;
using uri_import.PClasses;
using Acumatica.Default_20_200_001.Model;
using Acumatica.Manufacturing_21_200_001.Model;
using Acumatica.RESTClient.Model;

namespace uri_import
{
    class Program
    {
        static void Main(string[] args)
        {
            FileActions.MakeDirectories();
            Ex365 exch = new Ex365("mcarman@prelub.com", "Gr@nt@cc3$$", "https://outlook.office365.com/ews/Exchange.asmx", Independentsoft.Exchange.StandardFolder.Inbox);
            List<EmailAttachment> pos = exch.GetPOs("United Rentals, Inc:");
            List<SalesOrder> sos = new List<SalesOrder>();
            WritePOsToDirectory(pos);
            try
            {
                var config = AcumaticaREST.Config("https://rpmindustries.acumatica.com", "bryckman", "Washington1", "RPM SANDBOX", null, null);

                foreach (EmailAttachment ea in pos)
                {
                    try
                    {
                        List<string> lines = PDFHelper.PDFLinesToText(ea.FilePath);
                    }
                    catch (Exception lex)
                    {
                        ea.Errors.Add(string.Format("{0} {1} {2}", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString(), lex.Message));
                    }
                    ea.SalesOrder = PDFHelper.PDFBuildSalesOrder(PDFHelper.PDFLinesToText(ea.FilePath));
                    SalesOrder order = ea.SalesOrder;

                    foreach (SalesOrderDetail detail in order.Details)
                    {
                        Dictionary<string, CustomField> inner = new Dictionary<string, CustomField>();
                        CustomBooleanField b = new CustomBooleanField(true, "CustomBooleanField");
                        inner.Add("AMProdCreate", b);
                        detail.Custom = new Dictionary<string, Dictionary<string, CustomField>>();
                        detail.Custom.Add("Transactions", inner);

                    }
                    AcumaticaREST.CreateSalesOrder(ref order, config, ea.FileName, ea.Content);
                    ea.SalesOrder.OrderNbr = order.OrderNbr;
                    ea.SalesOrder.ID = order.ID;
                    //AcumaticaREST.UploadPurchaseOrder(config, ea.SalesOrder, "PurchaseOrder.pdf", ea.Content);
                    int ln = 1;
                    foreach (SalesOrderDetail detail in ea.SalesOrder.Details)
                    {
                        ProductionOrder prod = new ProductionOrder();
                        prod.OrderType = "RO";
                        prod.QtytoProduce = detail.OrderQty;
                        prod.InventoryID = detail.InventoryID;
                        prod.SOOrderNbr = order.OrderNbr;
                        prod.SOOrderType = "SO";
                        prod.SOLineNbr = detail.LineNbr;
                        ln = ln + 1;
                        AcumaticaREST.CreateProductionOrder(config, ref prod, "https://rpmindustries.acumatica.com", "bryckman", "Washington1", "RPM SANDBOX", null, null);
                        ea.ProductionOrders.Add(prod);
                    }
                }
                foreach (EmailAttachment ea in pos)
                {
                    AcumaticaREST.ReleaseAndLinkProductionOrders(config, ea, "https://rpmindustries.acumatica.com", "bryckman", "Washington1", "RPM SANDBOX", null, null);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
            finally
            {
                AcumaticaREST.APILogout("https://rpmindustries.acumatica.com", "bryckman", "Washington1", "RPM SANDBOX", null, null);
            }
            using (RestService service = new RestService("https://rpmindustries.acumatica.com", "bryckman", "Washington1", "RPM SANDBOX", "en_US"))
            {
                foreach (EmailAttachment ea in pos)
                {
                    try
                    {
                        AcumaticaREST.PutFile(service, ea.SalesOrder, "https://rpmindustries.acumatica.com", "SO", ea.SalesOrder.OrderNbr.Value.ToString(), ea.FileName, ea.Content);
                    }
                    catch { }
                }
            }

        }
        static void WritePOsToDirectory(List<EmailAttachment> pos)
        {
            foreach (EmailAttachment obj in pos)
            {
                string path = Path.Combine(FileActions.Attachments, obj.FileName);
                if (!File.Exists(path))
                    File.WriteAllBytes(path, obj.Content);
                Console.WriteLine(obj.FilePath);
            }
        }
    }
}
