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 iSTP_Tools
{
    public partial class PowerSupplyWindow : Form
    {
        PowerSupplyController _Controller;

        public PowerSupplyWindow()
        {
            InitializeComponent();
        }

        public void SetController(PowerSupplyController controller)
        {
            _Controller = controller;
        }

        public string ComPort {
            get { return (comboBoxPort.Text);}
        }

        private void PowerSupplyWindow_Load(object sender, EventArgs e)
        {
            comboBoxPort.Items.Clear();
            comboBoxPort.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
        }

        private void buttonOff_Click(object sender, EventArgs e)
        {
            _Controller.EnableOutput();
        }

        private void buttonOn_Click(object sender, EventArgs e)
        {
            _Controller.DisableOutput();
        }

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

        public void ChangeConnectionLabel(string lbl)
        {
            buttonCon.Text = lbl;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _Controller.SetOutput((float)VoltUpDown.Value, (float)AmpUpDown.Value);
        }

        public float Voltage { set{ VoltUpDown.Value = (decimal)value;} }

        public float Current { set { AmpUpDown.Value = (decimal)(value > (float)AmpUpDown.Maximum? 0 : value); } }

        public void SetOutputIndication(bool outputEnbabled)
        {
            if (outputEnbabled)
            {
                buttonOn.Visible = true;
                buttonOff.Visible = false;
                OnOffPanel.BackColor = Color.LightGreen;
            }
            else
            {
                buttonOff.Visible = true;
                buttonOn.Visible = false;
                OnOffPanel.BackColor = Color.LightGray;
            }
        }

        public void EnableControls(bool enabled)
        {
            PwrGroupBox.Enabled = enabled;
            OutGroupBox.Enabled = enabled;
        }
    }
}
