// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2017 Thomas Klose & Torsten Schache, 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.Identification;
using Hiperscan.ApoIdentCore.Reporting.ReportData;
using Hiperscan.Common.I18n;
using Hiperscan.Unix;

namespace Hiperscan.ApoIdentCore.Reporting.LabelPrint
{
    public class LabelInfo
    {
        public LabelInfo(ReportInfo info)
        {
            this.Substance = this.GetSubstanceName(info);
            this.Batch = info.Batch;
            this.Producer = this.GetProducer(info);
            this.UseBakMarking = info.BAKMarkingActivated;
            this.BAKMarking = this.GetBakMarkingState(info);
            this.PPN = info.Ppn;
            this.ReferenceQuantity = info.ReferenceQuantity;
            this.CorrectionFactor = this.GetWeighingCorrectionFactor(info);
            this.TestId = info.TestId;
            this.Date = info.Date.ToString("d");
            this.Identified = this.GetIdentificationResult(info);
            this.ExpiryDate = info.MinExpiryDate.ToString("d");
            this.Comment = info.Comment;
            this.Address = this.GetAddress(info);
            this.PrintName = this.GetPrintName(info);
            this.THCContent = this.GetTHCContent(info);
            this.CBDContent = this.GetCBDContent(info);
            this.Variety = info.Variety;
            this.CannabisResultIsTrustable = info.CannabisResultIsTrustable;
        }

        private string GetSubstanceName(ReportInfo info)
        {
                if (String.IsNullOrEmpty(info.SelectedName) == false)
                    return info.SelectedName;

                if (String.IsNullOrEmpty(info.DefaultName) == false)
                    return info.DefaultName;

                if (String.IsNullOrEmpty(info.LatinName) == false)
                    return info.LatinName;

                if (String.IsNullOrEmpty(info.SubstanceTradeName) == false)
                    return info.SubstanceTradeName;

            throw new Exception(Catalog.GetString("Could not determine substance name."));
        }

        private string GetProducer(ReportInfo info)
        {
            if (String.IsNullOrEmpty(info.Producer) == false)
                return info.Producer;

            return info.Distributor;
        }

        private string GetBakMarkingState(ReportInfo info)
        {
            if (info.BAKMarking.HasFlag(BAKMarkingState.Red))
                return BAKMarkingState.Red.xGetI18nString();

            string value = String.Empty;

            if (info.BAKMarking.HasFlag(BAKMarkingState.Blue))
                value += BAKMarkingState.Blue.xGetI18nString();

            if (info.BAKMarking.HasFlag(BAKMarkingState.Yellow))
            {
                if (string.IsNullOrEmpty(value) == false)
                    value += ", ";

                value += BAKMarkingState.Yellow.xGetI18nString();
            }

            if (info.BAKMarking.HasFlag(BAKMarkingState.Orange))
            {
                if (string.IsNullOrEmpty(value) == false)
                    value += ", ";

                value += BAKMarkingState.Orange.xGetI18nString();
            }

            return value;
        }

        private string GetWeighingCorrectionFactor(ReportInfo info)
        {
            try
            {
                double tmp = Double.Parse(info.CorrectionFactor);
                if (tmp > 10 || tmp <= 0)
                    return String.Empty;

                return tmp.ToString("f3");
            }
            catch(Exception)
            {
                return String.Empty;
            }
        }

        private string GetIdentificationResult(ReportInfo info)
        {
            if (info.AnalysisType == AnalysisModule.AnalysisType.Quantification)
                return info.CannabisResultIsTrustable ? Catalog.GetString("Yes") : Catalog.GetString("No");

            bool has_result = info.CustomerResult;
            Version version = null;
            try
            {
                version = new Version(info.ReportInfoVersion);
            }
            catch (Exception)
            {
                version = new Version(0, 0);
            }

            if (version >= new Version(3, 0))
            {
                if (info.ClassificationType == ClassificationType.Single)
                {
                    if (info.HasCustomerResult)
                    {
                        return info.CustomerResult ? Catalog.GetString("Yes") : Catalog.GetString("No");
                    }
                    else
                    {
                        return info.IsMatch ? Catalog.GetString("Yes") : Catalog.GetString("No");
                    }
                }
                else
                {
                    if (info.HasCustomerResult)
                    {
                        return info.CustomerResult ? Catalog.GetString("Yes") : Catalog.GetString("No");
                    }
                    else
                    {
                        return Catalog.GetString("Yes / No");
                    }
                }
            }
            else
            {
                return info.CustomerResult ? Catalog.GetString("Yes") : Catalog.GetString("No");
            }
        }

        private string GetAddress(ReportInfo info)
        {
            return String.Format("{0}, {1}, {2}",
                                 info.Company, 
                                 info.Street, 
                                 info.Town);
        }

        private string GetPrintName(ReportInfo info)
        {
            string print_name = "label test print";
            if (String.IsNullOrEmpty(info.PdfFileFullName) == false)
                print_name = Path.GetFileNameWithoutExtension(info.PdfFileFullName);

            return print_name;
        }

        private string GetTHCContent(ReportInfo info)
        {
            string value = String.Empty;

            if (String.IsNullOrEmpty(info.THCInput) == false)
                value = info.THCInput + "%";

            return value;
        }

        private string GetCBDContent(ReportInfo info)
        {
            string value = String.Empty;

            if (String.IsNullOrEmpty(info.CBDInput) == false)
                value = info.CBDInput + "%";

            return value;
        }

        public string Substance { get; private set; }
        public string Batch { get; private set; }
        public string Producer { get; private set; }
        public string BAKMarking { get; private set; }
        public bool UseBakMarking { get; private set; }
        public string PPN { get; private set; }
        public string ReferenceQuantity { get; private set; }
        public string CorrectionFactor { get; private set; }

        public string TestId { get; private set; }
        public string Date { get; private set; }
        public string Identified { get; private set; }
        public string ExpiryDate { get; private set; }
        public string Comment { get; private set; }
        public string Address { get; private set; }

        public string THCContent { get; private set; }
        public string CBDContent { get; private set; }
        public string Variety { get; private set; }
        public bool CannabisResultIsTrustable { get; set; }

        public string PrintName { get; private set; }
    }
}