// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2024 Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.IO;
using Hiperscan.AnalysisModule.Authentification;
using Hiperscan.ApoIdentCore.Reporting.ReportData;
using Hiperscan.PdfLib;
using Hiperscan.Spectroscopy;
using Hiperscan.Unix;

namespace Hiperscan.ApoIdentCore.Reporting.Protocol.Method
{
    public class CommonMethod : IDraw
    {
        public bool Draw(PdfArgs args)
        {
            this.Args = args;

            if (this.DrawDeviceProperties() == false)
                return false;

            if (this.DrawModelProperties() == false)
                return false;

            if (this.DrawProtocolFile() == false)
                return false;

            this.Layout.Feed(this.Layout.ParagraphBoundingBox.Height + this.Args.ReferenceLineDistance);
            return true;
        }

        protected bool DrawDeviceProperties()
        {
            this.Layout.Feed(this.Args.ReferenceParagraphFeed);
            this.Layout.SetFont(PdfFontTypes.HELVETICA, this.Args.ReferenceFontSize, PdfColors.Black);

            this.Layout.XPosition = 0;
            if (this.Layout.AddParagraph(Catalog.GetString("Type of measurement:")) == false)
                return false;

            string text = Catalog.GetString("NIR spectroscopy (Ph. Eur 11.0/2.2.40)") + " ";
            if (this.Info.SpectrumType == SpectrumType.ProcessedFluidSpecimen)
            {
                text += Catalog.GetString("with Transflectance Insert");
            }
            else if (this.Info.SpectrumType == SpectrumType.ProcessedSolidSpecimen)
            {
                if (this.Info.ProbeType == ProbeType.FinderInsert ||
                    this.Info.ProbeType == ProbeType.FinderSDInsert)
                    text += Catalog.GetString("with Sample Insert");
                else
                    text += Catalog.GetString("using diffuse reflectance");
            }

            this.Layout.XPosition = this.Args.ReferenceTabIndent;
            if (this.Layout.AddParagraph(text) == false)
                return false;

            this.Layout.Feed(this.Layout.ParagraphBoundingBox.Height + this.Args.ReferenceLineDistance);
            this.Layout.XPosition = 0;
            if (this.Layout.AddParagraph("Instrument:") == false)
                return false;

            text = String.Format(Catalog.GetString("Name: {0} / Serial Number: {1}"), this.Info.DeviceName,
                                 this.Info.OemSerialNumber);

            this.Layout.XPosition = this.Args.ReferenceTabIndent;
            return this.Layout.AddParagraph(text);
        }

        protected virtual bool DrawModelProperties()
        {
            this.Layout.Feed(this.Layout.ParagraphBoundingBox.Height + this.Args.ReferenceLineDistance);
            this.Layout.XPosition = 0;
            if (this.Layout.AddParagraph(Catalog.GetString("Substance class:")) == false)
                return false;

            string text = String.Format("{0}", this.Info.SubstanceClass);
            this.Layout.XPosition = this.Args.ReferenceTabIndent;
            if (this.Layout.AddParagraph(text) == false)
                return false;

            this.Layout.Feed(this.Layout.ParagraphBoundingBox.Height + this.Args.ReferenceLineDistance);
            this.Layout.XPosition = 0;
            if (this.Layout.AddParagraph(Catalog.GetString("Model:")) == false)
                return false;

            text = this.Info.Version;
            if (this.Info.AccessDatabaseType == AccessDatabaseType.Extension)
                text += Catalog.GetString("(added)");
            this.Layout.XPosition = this.Args.ReferenceTabIndent;
            if (this.Layout.AddParagraph(text) == false)
                return false;

            this.Layout.Feed(this.Layout.ParagraphBoundingBox.Height + this.Args.ReferenceLineDistance);
            this.Layout.XPosition = 0;
            if (this.Layout.AddParagraph(Catalog.GetString("Sample/Substance ID:")) == false)
                return false;

            text = this.Info.MatchId + "/" + this.Info.SelectedSubstanceId;
            this.Layout.XPosition = this.Args.ReferenceTabIndent;
            if (this.Layout.AddParagraph(text) == false)
                return false;

            this.Layout.Feed(this.Layout.ParagraphBoundingBox.Height + this.Args.ReferenceLineDistance);
            this.Layout.XPosition = 0;
            if (this.Layout.AddParagraph(Catalog.GetString("Report:")) == false)
                return false;

            return true;
        }

        protected bool DrawProtocolFile()
        {
            this.Layout.XPosition = this.Args.ReferenceTabIndent;
            string text = Path.GetFileName(this.Info.PdfFileFullName);
            if (String.IsNullOrEmpty(text) == true)
                text = " ";
            return this.Layout.AddParagraph(text);
        }

        protected Layout Layout
        {
            get
            {
                return this.Args.Layout;
            }
        }

        protected ReportInfo Info
        {
            get
            {
                return this.Args.Info;
            }
        }

        protected PdfArgs Args { get; set; }
    }
}
