using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace PressureGenerator
{
    interface IPressureGenerator
    {
        /// <summary>
        /// Opens port
        /// </summary>
        /// <param name="portName">Port name</param>
        /// <param name="baudRate">Selected Baudrate</param>
        /// <param name="dataBits">Selected dataBits.</param>
        /// <param name="stopBits">Selected stopBits.</param>
        /// <param name="parity">Selected parity.</param>
        /// <returns>TRUE - port is open</returns>
        bool Connect(string portName,
             string baudRate,
             string dataBits,
             string stopBits,
             string parity);

        /// <summary>
        /// Open Ethernet connection
        /// </summary>
        /// <param name="iIP">Target IP</param>
        /// <param name="port">Target Port</param>
        /// <returns>TRUE - port is open</returns>
        bool Connect(IPAddress iIP, int port);

        void Disconnect();

        /// <summary>
        /// Reads actual value
        /// </summary>
        /// <returns>TRUE - reading was successful</returns>
        bool ManualUpdate();

        /// <summary>
        /// Returns last actual Static pressure [mbar]
        /// </summary>
        /// <returns>last actual Static pressure [mbar]</returns>
        float GetActStatic();

        /// <summary>
        /// Returns last actual Pitot pressure [mbar]
        /// </summary>
        /// <returns>last actual Pitot pressure [mbar]</returns>
        float GetActPitot();

        /// <summary>
        /// Commands new setpoint for Static pressure [mbar]
        /// </summary>
        /// <param name="iStatic">new setpoint for Static pressure [mbar]</param>
        /// <returns>TRUE - success</returns>
        bool SetStatic(float iStatic);

        /// <summary>
        /// Commands new setpoint for Pitot pressure [mbar]
        /// </summary>
        /// <param name="iPitot">new setpoint for Pitot pressure [mbar]</param>
        /// <returns>TRUE - success</returns>
        bool SetPitot(float iPitot);

        /// <summary>
        /// Start pressure generator
        /// </summary>
        /// <returns>TRUE - success</returns>
        bool Run();

        /// <summary>
        /// Stop pressure generator
        /// </summary>
        /// <returns>TRUE - success</returns>
        bool Stop();

        /// <summary>
        /// Returns device name;
        /// </summary>
        string PressureName { get; }

        void SendCommand(string command);
    }

    public static class PressError
    {
        public static bool isActive;

        public static string Text
        {
            get;
            private set;
        }
        public static string Source
        {
            get;
            private set;
        }

        public static string TextFull
        {
            get;
            private set;
        }

        public static string VerboseText
        {
            get
            {
                return string.Format("Pressure generator issue:\n\t[Source: {0}] {1} -> {2}", Source, Text, TextFull);
            }
        }

        public static void Clean()
        {
            Text = string.Empty;
            Source = string.Empty;
            TextFull = string.Empty;
            isActive = false;
        }

        public static void FillErrorStruct(string iSource, string iText, string iFulltext)
        {
            isActive = true;
            Source = iSource;
            Text = iText;
            TextFull = iFulltext;
            iSTP_DebugConsole.DebugConsole.WriteLine(VerboseText);
        }
    }
}
