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í sítě
    /// </summary>
    public partial class CtrlPanelNet : CtrlPanel
    {
        /// <summary>
        /// Vytvoření instance panelu
        /// </summary>
        public CtrlPanelNet()
        {
            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)
            {
                tbIP.Text = (DataObject as DataNet).IP.ToString();
                tbGate.Text = (DataObject as DataNet).Gate.ToString();
                tbMask.Text = (DataObject as DataNet).Mask.ToString();
            }
        }

        /// <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();

            (DataObject as DataNet).IP = IPAddress.Parse(tbIP.Text.Trim());
            if ((DataObject as DataNet).IP == null)
                message.AppendFormat("{0} není platná IP adresa {1}", tbIP.Text, Environment.NewLine);

            (DataObject as DataNet).Mask = IPAddress.Parse(tbMask.Text.Trim());
            if ((DataObject as DataNet).Mask == null)
                message.AppendFormat("{0} není platná IP adresa {1}", tbMask.Text, Environment.NewLine);

            (DataObject as DataNet).Gate = IPAddress.Parse(tbGate.Text.Trim());
            if ((DataObject as DataNet).Gate == null)
                message.AppendFormat("{0} není platná IP adresa {1}", tbGate.Text, Environment.NewLine);

            return base.ValidateData(message);
        }

        /// <summary>
        /// Uložení dat do modulu
        /// </summary>
        /// <returns></returns>
        public override bool UploadData()
        {
            if (base.UploadData())
            {
                if (MessageBox.Show(
                    "Toto nastavení vyžaduje restart modulu, chcete provést restart nyní?",
                    "Potvrzení",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    DataObject.Module.Restart();
                }
                return true;
            }
            return false;
        }
    }
}
