using System;
using System.Linq;

namespace ZionRobot.Models.Items
{
    public class File : ZionFoundation.Components.File, IDisposable
    {
        private ZionVault.Binder.OCR.Document ocrObject;
        private ZionVault.Binder.Output.Document metaObject;
        private ZionVault.Binder.Verified.Document verifyObject;

        public string OCR { get => string.Format(@"{0}\{1}.ocr", this.Directory, this.Name); }
        public string META { get => string.Format(@"{0}\{1}.ztd", this.Directory, this.Name); }
        public string VERIFY { get => string.Format(@"{0}\{1}.vrf", this.Directory, this.Name); }

        public bool Disposed;

        public File(string path) : base(path)
        {
            if (System.IO.File.Exists(this.META) && (this.METAObject.Status == Persistence.Basic.INTEGRATED || this.METAObject.Status == @"GROUPED"))
                this.Status = Persistence.Basic.INTEGRATED;
            else if (System.IO.File.Exists(this.META))
                this.Status = Persistence.Basic.INTEGRATION;
            else
                this.Status = Persistence.Basic.EXTRACTION;

            this.Disposed = false;
        }

        public void Move(string folder)
        {
            base.Move(folder);
            System.IO.File.Move(META, System.IO.Path.Combine(folder, this.Name + @".ztd"));
        }

        public void Dispose()
        {
            if (OCRObject != null)
                OCRObject.Dispose();

            if (METAObject != null)
                METAObject.Dispose();

            if (VERIFYObject != null)
                VERIFYObject.Dispose();

            GC.SuppressFinalize(this);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            this.Disposed = true;

        }

        public ZionVault.Binder.OCR.Document OCRObject
        {
            get
            {
                if (System.IO.File.Exists(OCR))
                {
                    if (ocrObject != null)
                        return ocrObject;

                    ocrObject = new ZionVault.Binder.OCR.Document(OCR, false);
                    return ocrObject;
                }

                return null;
            }
        }

        public ZionVault.Binder.Output.Document METAObject
        {
            get
            {
                if (System.IO.File.Exists(META))
                {
                    if (metaObject != null)
                        return metaObject;

                    metaObject = new ZionVault.Binder.Output.Document(META, false);
                    return metaObject;
                }

                return null;
            }
        }


        public ZionVault.Binder.Verified.Document VERIFYObject
        {
            get
            {
                if (System.IO.File.Exists(VERIFY))
                {
                    if (verifyObject != null)
                        return verifyObject;

                    verifyObject = new ZionVault.Binder.Verified.Document(VERIFY, false);
                    return verifyObject;
                    
                }

                return null;
            }
        }

        public string LawsuitNumber
        {
            get
            {
                return this.GetField(@"NrProcesso").Value;
            }
        }

        public string PublicationContent
        {
            get
            {
                return this.GetField(@"ConteudoCompleto").Value;
            }

        }

        private ZionVault.Binder.Output.Field GetField(string fieldName)
        {
            return (from field in this.METAObject.AllFields
                    where field.Name == fieldName
                    select field).FirstOrDefault();
        }
    }
}
