using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using ModuleDataLib;

namespace Setup
{
    /// <summary>
    /// Třída pro panel nastavení měření a broadcast vysílání
    /// </summary>
    public partial class CtrlPanelMeas : CtrlPanel
    {
        /// <summary>
        /// Vytvoření instance panelu
        /// </summary>
        public CtrlPanelMeas()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Zajistí stažení dat z modulu
        /// </summary>
        public override void DownloadData()
        {
            base.DownloadData();
        }

        /// <summary>
        /// Provede stažení dat v případě, že dosud nebylo provedeno a jejich zobrazení
        /// </summary>
        public override void ShowData()
        {
            base.ShowData();

            if (DataObject.Retrieved)
            {

                DataMeas meas = (DataObject as DataMeas);

                tbMeasInt.Text = meas.MeasInterval.ToString();
                tbBcIp.Text = meas.BroadcastIP.ToString();
                cbBc.Checked = (meas.BroadcastInterval > 0);
                tbBcInt.Enabled = cbBc.Checked;
                tbBcIp.Enabled = cbBc.Checked;
                if (cbBc.Checked)
                {                        
                    tbBcInt.Text = meas.BroadcastInterval.ToString();
                }
            }
        }

        /// <summary>
        /// Obsluha checkboxu pro povolení broadcast vysílání
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbBc_CheckedChanged(object sender, EventArgs e)
        {
            tbBcInt.Enabled = cbBc.Checked;
            tbBcIp.Enabled = cbBc.Checked;
            if (cbBc.Checked)
                tbBcInt.Text = "15";
            else
                tbBcInt.Text = String.Empty;
        }

        /// <summary>
        /// Validace dat zadaných uživatelem ve formuláři
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public override bool ValidateData(StringBuilder message)
        {
            if (message == null)
                message = new StringBuilder();

            DataMeas meas = (DataObject as DataMeas);

            byte x = 0;
            Byte.TryParse(tbMeasInt.Text, out x);

            if (x < 5)
                message.AppendFormat("{0} není platná hodnota, povolený rozsah je 5..255 {1}", tbMeasInt.Text, Environment.NewLine);
            else
                meas.MeasInterval = x;

            if (cbBc.Checked)
            {
                meas.BroadcastIP = IPAddress.Parse(tbBcIp.Text.Trim());
                if (meas.BroadcastIP == null)
                    message.AppendFormat("{0} není platná IP adresa {1}", tbBcIp.Text, Environment.NewLine);

                x = 0;
                Byte.TryParse(tbBcInt.Text, out x);

                if (x < 5)
                    message.AppendFormat("{0} není platná hodnota, povolený rozsah je 5..255 {1}", tbBcInt.Text, Environment.NewLine);
                else
                    meas.BroadcastInterval = x;
            }
            else
                meas.BroadcastInterval = 0;


            return base.ValidateData(message);
        }

        /// <summary>
        /// Uložení dat do modulu
        /// </summary>
        /// <returns></returns>
        public override bool UploadData()
        {
            return base.UploadData();
        }

    }
}
