using System;
using System.IO;
using System.Xml.Serialization;
using KaltgasTool;

namespace KaltgasExample
{
    internal class Program
    {
        private static readonly Controller _controller = new Controller("COM8", 9600, 8, 0, 1);

        private static void Main(string[] args)
        {
            if (_controller.IsFound)
            {
                // switch on power
                _controller.IsPower = true;

                // switch off alarm
                _controller.IsAlarm = false;

                // switch to heater
                _controller.IsControllingChamber = false;

                // get current heater temp in degrees celsius
                float? temp_heater = _controller.Analogeingang1;
                Console.Write("Heater Temp: ");
                Console.WriteLine(temp_heater);

                // get current chamber temp in degrees celsius
                float? temp_chamber = _controller.Analogeingang2;
                Console.Write("Chamber Temp: ");
                Console.WriteLine(temp_chamber);

                // get current setpoint temp in degrees celsius
                float? temp_setpoint = _controller.Sollwert;
                Console.Write("Current Setpoint: ");
                Console.WriteLine(temp_setpoint);

                // get current jet
                int? jet = _controller.Jet;
                Console.Write("Current Jet: ");
                Console.WriteLine(jet);

                // is heater currently on
                bool? heaterOn = _controller.IsHeaterOn;

                // set new setpoint to 44 degrees celsius
                _controller.Sollwert = 44;

                // has the jet been manually changed on the dial (IsComputerJet = false)? 
                bool is_computerjet = _controller.IsComputerJet;

                // set new jet value to 50%
                _controller.IsComputerJet = true;
                _controller.Jet = 50;


                if (_controller.IsGrenzwertalarmJet)
                    Console.WriteLine("Limit Alarm Jet!");

                if (_controller.IsGrenzwertalarmHeater)
                    Console.WriteLine("Limit Alarm Heater!");

                // update all controller parameter values
                // (all parameters properties are starting with the name prefix "Parameter")
                _controller.UpdateParameterValues();

                // read e.g. the offset
                float temp_offset = _controller.ParameterOffset1;

                // or write new offset1 value 11
                _controller.ParameterOffset1 = 11;

                // reset the controller to its default parameters
                _controller.SendDefaultParameters();

                // run autotune for heater
                autotune(false);

                // run autotune for chamber
                autotune(true);

                string file = "test.xml";
                save_autotune_parameters(file);
                // ...
                load_autotune_parameters(file);
            }
            else
            {
                Console.WriteLine("Kaltgas Controller not found");
            }
        }

        private static void autotune(bool isChamber)
        {
            _controller.IsControllingChamber = isChamber;
            int counter = 0;
            while (!_controller.IsAutotune || counter < 10)
            {
                _controller.IsAutotune = true;
                counter++;
            }
        }

        private static void save_autotune_parameters(string fileName)
        {
            var serializer = new XmlSerializer(typeof (JumoDTron.AutotuneParameters));
            using (var fileStream = new FileStream(fileName, FileMode.Create))
            {
                serializer.Serialize(fileStream, _controller.ReadAutotuneParameterSet());
            }
        }

        private static void load_autotune_parameters(string fileName)
        {
            var serializer = new XmlSerializer(typeof (JumoDTron.AutotuneParameters));
            JumoDTron.AutotuneParameters autotuneValues;
            using (var fileStream = new FileStream(fileName, FileMode.Open))
            {
                autotuneValues = (JumoDTron.AutotuneParameters) serializer.Deserialize(fileStream);
            }
            _controller.WriteAutotuneParameterSet(autotuneValues);
        }
    }
}