using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nappat.CsCreator;

namespace Nappat.Testing
{
    public class Program
    {
        #region InterpretingStatus
        public delegate void InterpretingStatusEventHandler();
        public event InterpretingStatusEventHandler InterpretingStart;
        public event InterpretingStatusEventHandler InterpretingStop;
        public void OnInterpretingStart()
        {
            if (InterpretingStart != null)
                InterpretingStart();
        }
        public void OnInterpretingStop()
        {
            if (InterpretingStop != null)
                InterpretingStop();
        }
        #endregion

        #region Private Fields
        DataSender dataSender;
        ParamSender paramSender;
        #endregion

        #region Constructors
        public Program(DataSender dataSender)
        {
            this.dataSender = dataSender;
            paramSender = new ParamSender();

        }
        #endregion

        public void Play()
        {
            if (dataSender.ProgramChooser == 1)
            {
                Creating();
                Interpreting();
            }
            if (dataSender.ProgramChooser == 2)
                Interpreting();
        }

        #region Private Methods
        void Interpreting()
        {
            dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "Info", "Start interpreting file: " + dataSender.TestNames, null);
            try
            {
                FileExecutor fileExecutor = new FileExecutor(dataSender);
                fileExecutor.ExecutingStart += fileExecutor_ExecutingStart;
                fileExecutor.ExecutingStop += fileExecutor_ExecutingStop;
                fileExecutor.Execution();
            }
            catch (Exception e)
            {
                dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "FatalError", dataSender.TestNames + " was not interpreted.", null);
                dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "FatalError", e.InnerException.Message, null);
                //throw new Exception(e.InnerException.Message);
                string bcDebug = e.InnerException.Message + Environment.NewLine + Environment.NewLine + "Tato chyba byla způsobena chybějícími ovladači." + Environment.NewLine + Environment.NewLine +
                    "Ovladače jsou chráněny obchodním tajemstvím společnoti Honeywell International, Inc. V případě oprávněné potřeby zdrojových kódů, lze ovladače získat v IS VUT Brno anebo kontaktováním vztyčné osoby:" + Environment.NewLine + Environment.NewLine +
                    "pan Radomír Svoboda" + Environment.NewLine +
                    "Honeywell HTS Brno" + Environment.NewLine +
                    "Tuřanka 96, 62700 Brno" + Environment.NewLine +
                    "e-mail: radomir.svoboda@honeywell.com"
                    + Environment.NewLine + Environment.NewLine +
                    "UPOZORNĚNÍ:" + Environment.NewLine +
                    "Tato vyjímka se vyskytuje pouze u speciálně upravené verze pro odevzdání bakalářské práce.";
                throw new Exception(bcDebug);
            }
            if (dataSender.LogMessages.Severity == "Info")
                dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "Success", dataSender.TestNames + " was interpreted.", null);
            else
                dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "FatalError", dataSender.TestNames + " was not interpreted.", null);
        }

        void fileExecutor_ExecutingStart()
        {
            OnInterpretingStart();
        }
        void fileExecutor_ExecutingStop()
        {
            OnInterpretingStop();
        }

        void Creating()
        {
            ParamSenderMethod();
            dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "Info", "Start creating file: " + dataSender.TestNames, null);
            try
            {
                CsCreator.CsCreator csCreator = new CsCreator.CsCreator(paramSender);
                csCreator.CreatCode();
            }
            catch (Exception e)
            {
                dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "FatalError", dataSender.TestNames + " was not created.", null);
                throw new Exception(e.InnerException.Message);
            }
            dataSender.LogMessages = new LogLine(DateTime.Now.ToString(), "Program", "Success", dataSender.TestNames + " was created.", null);
        }
        void ParamSenderMethod()
        {
            this.paramSender.DriverPath = dataSender.DriverPaths;
            this.paramSender.CsFilePath = dataSender.CsFilePaths;
            this.paramSender.ConfigPath = dataSender.ConfigPaths;
            this.paramSender.ScriptPath = dataSender.ScriptPaths;
            this.paramSender.TestName = dataSender.TestNames;
            this.paramSender.OutBoardName = dataSender.OutBoardNames;
            this.paramSender.InBoardName = dataSender.InBoardNames;
            this.paramSender.ChannelName = dataSender.ChannelNames;
            this.paramSender.OutputPath = dataSender.OutputPaths;
        }
        #endregion
    }
}
