using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PressureGenerator
{
    public partial class PressureGeneratorWnd : Form
    {
        private PressureGeneratorController _Controller;

        public PressureGeneratorWnd()
        {
            InitializeComponent();
            this.PressGeneratorSelectBox.SelectedIndex = 0;
            this.BaudrateList.SelectedItem = "9600";
            this.DatabitsList.SelectedItem = "8";
            this.StopbitsList.SelectedItem = "1";
            this.ParityList.SelectedItem = "None";
        }

        public void setController(PressureGeneratorController controller)
        {
            this._Controller = controller;
        }

        public int SelectedGenerator { get { return (this.PressGeneratorSelectBox.SelectedIndex); } }
        public bool IsEthernetSelected { get { return (this.EthernetButton.Checked); } }
        public string IpAddress { get { return (this.IPaddressBox.Text); } }

        public int TcpPort
        {
            get
            {
                int portNum;
                if (Int32.TryParse(this.IPportBox.Text, out portNum))
                    return portNum;
                else
                    return -1;
            }
        }

        public String ComPort
        {
            get { return (this.COMportList.SelectedItem == null ? "None" : this.COMportList.SelectedItem.ToString()); }
        }

        public String ComBaudrate
        {
            get { return (this.BaudrateList.SelectedItem.ToString()); }
        }

        public String ComDatabits
        {
            get { return (this.DatabitsList.SelectedItem.ToString()); }
        }

        public String ComStopbits
        {
            get { return (this.StopbitsList.SelectedItem.ToString()); }
        }

        public String ComParity
        {
            get { return (this.ParityList.SelectedItem.ToString()); }
        }

        private void close_btn_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        public void UpdateConnectionControls()
        {
            bool enabled = _Controller.Status == PressureGeneratorController.DeviceStatus.NotConnected;
            this.EthernetButton.Enabled = enabled;
            this.IPaddressBox.Enabled = enabled;
            this.IPportBox.Enabled = enabled;

            this.SerialButton.Enabled = enabled;
            this.COMportList.Enabled = enabled;
            this.BaudrateList.Enabled = enabled;
            this.DatabitsList.Enabled = enabled;
            this.StopbitsList.Enabled = enabled;
            this.ParityList.Enabled = enabled;
            switch (_Controller.Status)
            {
                case PressureGeneratorController.DeviceStatus.NotConnected:
                    this.PressureConnectButton.Enabled = true;
                    this.PressureConnectButton.Text = "Connect";
                    this.toolStripStatusLabel1.Text = "Disconnected";
                    EnableConfigAndPressure(false);
                    break;
                case PressureGeneratorController.DeviceStatus.Connecting:
                    this.PressureConnectButton.Enabled = false;
                    this.PressureConnectButton.Text = "Connecting";
                    this.toolStripStatusLabel1.Text = "Connecting...";
                    EnableConfigAndPressure(false);
                    break;
                case PressureGeneratorController.DeviceStatus.Connected:
                    this.PressureConnectButton.Enabled = true;
                    this.PressureConnectButton.Text = "Disconnect";
                    this.toolStripStatusLabel1.Text = "Connected";
                    EnableConfigAndPressure(true);
                    break;
            }
        }

        private void EnableConfigAndPressure(bool enabled)
        {
            this.PressureConfGroupBox.Enabled = enabled;
            this.PressureGroupBox.Enabled = enabled;
        }

        public float Static
        {
            get
            {
                float rate;
                if (Single.TryParse(this.StaticTxt.Text, out rate))
                    return rate;
                else
                    return 0.0f;
            }
            set
            {
                StaticTxt.Text = String.Format("{0}", value);
            }
        }

        public float Pitot
        {
            get
            {
                float rate;
                if (Single.TryParse(this.PitotTxt.Text, out rate))
                    return rate;
                else
                    return 0.0f;
            }
            set
            {
                PitotTxt.Text = String.Format("{0}", value);
            }
        }

        private void ClimaConnectButton_Click(object sender, EventArgs e)
        {
            this._Controller.Connect();
        }

        private void SetConditionsBtn_Click(object sender, EventArgs e)
        {
            iSTP_DebugConsole.DebugConsole.WriteLine("Static set point {0} Pitot set point {1}", Static, Pitot);
            this._Controller.SetPressure(Static, Pitot);
        }

        private void RunBtn_Click(object sender, EventArgs e)
        {
            this._Controller.Run();
        }

        private void StopBtn_Click(object sender, EventArgs e)
        {
            this._Controller.Stop();
        }

        private void EthernetButton_CheckedChanged(object sender, EventArgs e)
        {
            if (this.EthernetButton.Checked == true)
            {
                this.IPaddressBox.Enabled = true;
                this.IPportBox.Enabled = true;

                this.SerialButton.Checked = false;
                this.COMportList.Enabled = false;
                this.BaudrateList.Enabled = false;
                this.DatabitsList.Enabled = false;
                this.StopbitsList.Enabled = false;
                this.ParityList.Enabled = false;
            }
        }

        private void SerialButton_CheckedChanged(object sender, EventArgs e)
        {
            if (this.SerialButton.Checked == true)
            {
                this.EthernetButton.Checked = false;
                this.IPaddressBox.Enabled = false;
                this.IPportBox.Enabled = false;

                this.COMportList.Enabled = true;
                this.BaudrateList.Enabled = true;
                this.DatabitsList.Enabled = true;
                this.StopbitsList.Enabled = true;
                this.ParityList.Enabled = true;
            }
        }

        private void PressureGeneratorWnd_Load(object sender, EventArgs e)
        {
            this.COMportList.Items.Clear();
            this.COMportList.Items.AddRange(this._Controller.getAvailableCOMs());
        }

        public void UpdateValues(float Static, float Pitot)
        {
            if (CurStaticLbl.InvokeRequired)
            {
                CurStaticLbl.Invoke(new MethodInvoker(delegate { CurStaticLbl.Text = Static.ToString("00.00"); }));
                CurPitotLbl.Invoke(new MethodInvoker(delegate { CurPitotLbl.Text = Pitot.ToString("00.00"); }));
            }
            else
            {
                CurStaticLbl.Text = Static.ToString();
                CurPitotLbl.Text = Pitot.ToString();
            }
        }

        private void buttonCommand_Click(object sender, EventArgs e)
        {
            this._Controller.SendCommand(Commandtb.Text);
        }
    }
}
