// Created with MonoDevelop
//
//    Copyright (C) 2011 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 Hiperscan.Common;
using Hiperscan.Spectroscopy;
using Hiperscan.Unix;

namespace Hiperscan.SGS.Benchtop
{
    public partial class Finder
    {
        public class LightSourceDamagedException : Exception
        {
            public LightSourceDamagedException()
            {
            }

            public LightSourceDamagedException(string msg) : base(msg)
            {
            }

            public LightSourceDamagedException(string msg, Exception inner) : base(msg, inner)
            {
            }
        }

        public void SetLightSourceDamageThreshold()
        {
            string value = Environment.GetEnvironmentVariable(Env.ApoIdent.LIGHT_SOURCE_DAMAGE_THRESHOLD);

            if (String.IsNullOrEmpty(value) == false)
            {
                try
                {
                    this.LightSourceDamageThreshold = Double.Parse(value, System.Globalization.CultureInfo.InvariantCulture);
                    Console.WriteLine("Set light source damaged threshold to: {0}.", this.LightSourceDamageThreshold);
                    return;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Cannot read light source damaged threshold from enviromental variable: {0}", ex.Message);
                    Console.WriteLine("Continue with setting light source threshold depending from product id.");
                }
            }

            if (this.Device != null)
            {
                switch (this.Device.Info.ProductId)
                {
                    case Hiperscan.SGS.ProductId.SGS1900_GROUP:
                        this.LightSourceDamageThreshold = LightSourceDamagedThresholds.APOIDENT_1;
                        break;

                    case Hiperscan.SGS.ProductId.APOIDENT2_SGS:
                        this.LightSourceDamageThreshold = LightSourceDamagedThresholds.APOIDENT_2;
                        break;

                    case Hiperscan.SGS.ProductId.APOIDENT_2X_ES20:
                        this.LightSourceDamageThreshold = LightSourceDamagedThresholds.APOIDENT_2_1;
                        break;

                    default:
                        break;
                }

                Console.WriteLine("Set light source damaged threshold to: {0}.", this.LightSourceDamageThreshold);
            }
        }

        private void UnsetLightSourceDamageThreshold()
        {
            this.LightSourceDamageThreshold = 0;
            Console.WriteLine("Set light source damaged threshold to: {0}.", this.LightSourceDamageThreshold);
        }

        private void CheckLightSource(Spectrum oxid_spectrum)
        {
            if (this.HardwareFeatures.HasFlag(HardwareFeatures.LightSourceMonitor))
            {
                if (this.device.LightSourceReady() < 0)
                    throw new LightSourceDamagedException(Catalog.GetString("Light source is damaged."));
            }
            else
            {
                oxid_spectrum = oxid_spectrum.Clone();
                oxid_spectrum.AdcCorrection = null;

                Console.WriteLine("CheckLightSource; YMax(Oxide): {0}, Threshold; {1}",
                                  oxid_spectrum.Intensity.YMax(), this.LightSourceDamageThreshold);

                if (oxid_spectrum.Intensity.YMax() < this.LightSourceDamageThreshold)
                    throw new LightSourceDamagedException(Catalog.GetString("Light source is damaged."));
            }
        }

        public double LightSourceDamageThreshold { get; private  set; } = 0;
    }
}
