using System.IO;
using System.Net;
using System;
using System.Collections.Generic;
using Independentsoft.Exchange;
using uri_import.FClasses;
using Acumatica.Default_20_200_001.Model;
using Acumatica.Manufacturing_21_200_001.Model;

namespace uri_import.EClasses
{
    public class Ex365
    {
        private NetworkCredential _credential;
        private Service _service;
        private StandardFolder _folder;
        public NetworkCredential Credential { get { return _credential; } set { _credential = value; } }
        public Service service { get { return _service; } set { _service = value; } }
        public StandardFolder Folder { get { return _folder; } set { _folder = value; } }
        public Ex365() { }

        public Ex365(string username, string password)
        {
            _credential = new NetworkCredential(username, password);
            _service = new Service("https://outlook.office365.com/ews/Exchange.asmx", _credential);

        }
        public Ex365(string username, string password, string url)
        {
            _credential = new NetworkCredential(username, password);
            _service = new Service(url, _credential);

        }
        public Ex365(string username, string password, string url, StandardFolder folder)
        {
            _credential = new NetworkCredential(username, password);
            _service = new Service(url, _credential);
            Folder inbox = _service.GetFolder(folder);
        }
        public List<EmailAttachment> GetPOs(string filter, bool hasAttachment = true)
        {
            int index = 1;
            List<EmailAttachment> list = new List<EmailAttachment>();
            IsEqualTo restriction = new IsEqualTo(MessagePropertyPath.HasAttachments, hasAttachment);
            Contains subFilter = new Contains(MessagePropertyPath.Subject, "United Rentals, Inc:", ContainmentMode.Prefixed, ContainmentComparison.IgnoreCase);
            ItemShape itemShape = new ItemShape(ShapeType.Id);
            FindItemResponse response = _service.FindItem(StandardFolder.Inbox, itemShape, subFilter);
            for (int i = 0; i < response.Items.Count; i++)
            {
                if (response.Items[i] is Message)
                {
                    ItemId itemId = response.Items[i].ItemId;
                    Message message = service.GetMessage(itemId);
                    Console.WriteLine(message.Subject);
                    IList<AttachmentInfo> attachmentsInfo = message.Attachments;
                    for (int j = 0; j < attachmentsInfo.Count; j++)
                    {
                        Attachment attachment = _service.GetAttachment(attachmentsInfo[j].Id);
                        if (attachment is FileAttachment && attachment.Name.ToUpper().EndsWith(".PDF"))
                        {
                            EmailAttachment ea = new EmailAttachment();
                            FileAttachment fileAttachment = (FileAttachment)attachment;
                            ea.Content = fileAttachment.Content;
                            ea.FileName = fileAttachment.Name.ToUpper().Replace(".PDF", string.Format("{0}.PDF", index));
                            index = index + 1;
                            ea.Sender = message.From.EmailAddress;
                            list.Add(ea);
                        }
                    }
                }
            }
            return list;
        }
    }
    public class EmailSettings
    {
        public string Username { get; set; }
        public string Password { get; set; }
        public string Url { get; set; }
        public StandardFolder Folder { get; set; }
    }
    public class EmailAttachment
    {
        private List<ProductionOrder> _productionOrders;
        private List<string> _errors;
        private bool _released = false;
        private bool _linked = false;
        public EmailAttachment()
        {
            _productionOrders = new List<ProductionOrder>();
            _errors = new List<string>();
        }
        public byte[] Content { get; set; }
        public string Sender { get; set; }
        public string FileName { get; set; }
        public string FilePath
        {
            get
            {
                string path = System.IO.Path.Combine(
                    FileActions.ExecFile,
                    "attachments",
                    this.FileName);
                return path;
            }
        }
        public SalesOrder SalesOrder { get; set; }
        public string SalesOrderNumber { get; set; }
        public List<ProductionOrder> ProductionOrders { get { return _productionOrders; } set { _productionOrders = value; } }
        public bool Released { get{return _released;} set{_released = value;}}
        public bool Linked { get{return _linked;} set{_linked = value;}}
        public List<string> Errors { get { return _errors; } set { _errors = value; } }
    }
}