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

namespace Access_Control_Server
{
    public partial class FormTerminalSetting : Form
    {
        String TerminalIP;

        public FormTerminalSetting(String IP, String Name)
        {
            Comm comm = new Comm();
            String str;

            InitializeComponent();
            TerminalIP = IP;
            this.Text += Name + " (" + IP + ")";
            this.webBrowser.Navigate(IP);
            textBoxName.Text = Name;

            str = comm.SendToTerminal(IP, "GETDOORTIME");
            if (!String.Equals(str, "ERR") && str.Length > 0)
            {
                numericUpDownDoorControl.Value = Convert.ToDecimal(str);
            }
        }

        private void ChangeName_Click(object sender, EventArgs e)
        {
            new Comm().SendToTerminal(TerminalIP, "NAME=" + textBoxName.Text);
        }

        private void ChangeDoorControl_Click(object sender, EventArgs e)
        {
            new Comm().SendToTerminal(TerminalIP, "DOORTIME=" + numericUpDownDoorControl.Value.ToString());
        }

        private void Save_Click(object sender, EventArgs e)
        {
            Comm comm = new Comm();

            if (!String.Equals(comm.SendToTerminal(TerminalIP, "NAME=" + textBoxName.Text), "OK")) {
                MessageBox.Show("Communication error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!String.Equals(comm.SendToTerminal(TerminalIP, "DOORTIME=" + numericUpDownDoorControl.Value.ToString()), "OK"))
            {
                MessageBox.Show("Communication error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!String.Equals(comm.SendToTerminal(TerminalIP, "SAVESETTING"), "OK"))
            {
                MessageBox.Show("Communication error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.Close();
        }

        private void Calibration_Click(object sender, EventArgs e)
        {
            Comm comm = new Comm();

            if (!String.Equals(comm.SendToTerminal(TerminalIP, "CALIBRATION"), "OK"))
            {
                MessageBox.Show("Communication error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!String.Equals(comm.SendToTerminal(TerminalIP, "SAVESETTING"), "OK"))
            {
                MessageBox.Show("Communication error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
    }
}
