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;

using System.IO;
using System.Threading;

namespace Access_Control_Server
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            StartListenThread();
        }

        private void Form_Load(object sender, EventArgs e)
        {
            if (File.Exists(Directory.GetCurrentDirectory() + "/database.xml"))
            {
                dataSetList.ReadXml(Directory.GetCurrentDirectory() + "/database.xml");
            }
        }

        private void Form_Closing(object sender, FormClosingEventArgs e)
        {
            tcpListener.Stop();
            listenThread.Abort();

            bindingSourceUsers.EndEdit();
            bindingSourceTerminals.EndEdit();
            dataSetList.Terminals.Columns.Remove(dataSetList.Terminals.StatusOKColumn);
            dataSetList.WriteXml(Directory.GetCurrentDirectory() + "/database.xml");
        }

        private void Form_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                notifyIcon.Visible = true;
                notifyIcon.ShowBalloonTip(2000, "Access Control Server", "Windows was minimized.", ToolTipIcon.Info);
                this.ShowInTaskbar = false;
            }
        }

        private void showWindow(object sender, MouseEventArgs e)
        {
            notifyIcon.Visible = false;
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
        }

        private void Show_NetworkInfo(object sender, EventArgs e)
        {
            FormNetworkInfo form = new FormNetworkInfo();
            form.Show();
        }

        private void Show_AboutBox(object sender, EventArgs e)
        {
            AboutBox form = new AboutBox();
            form.ShowDialog();
        }

        private void TimerControl_Tick(object sender, EventArgs e)
        {
            String[] strArray;
            Boolean error = false;
            foreach (DataSetTypes.DataSetList.TerminalsRow row in dataSetList.Terminals.Rows)
            {
                strArray = new Comm().SendToTerminal(row.IP, "GETSTATE").Split(' ');
                if (strArray[0] == "OK")
                {
                    row.StatusOK = true;
                    if (strArray.Length > 1) row.Name = strArray[1];
                }
                else
                {
                    row.StatusOK = false;
                    error = true;
                }
            }

            if (error)
            {
                notifyIcon.Icon = Properties.Resources.NotifyIconErr;
                notifyIcon.ShowBalloonTip(10000, "Warning", "Please check terminals", ToolTipIcon.Warning);
            }
            else notifyIcon.Icon = Properties.Resources.NotifyIconOK;
        }

        private void Terminal_PositionChanged(object sender, EventArgs e)
        {
            try
            {
                DataSetTypes.DataSetList.TerminalsRow row = (DataSetTypes.DataSetList.TerminalsRow)((DataRowView)bindingSourceTerminals.Current).Row;
                String filter = "TerminalID=" + row.ID.ToString();
                bindingSourceTerminalLog.Filter = filter;
                bindingSourceTerminalUsers.Filter = filter;
            }
            catch
            {
                bindingSourceTerminalLog.Filter = "null";
                bindingSourceTerminalUsers.Filter = "null";
            }
        }

        private void User_PositionChanged(object sender, EventArgs e)
        {
            try
            {
                DataSetTypes.DataSetList.UsersRow row = (DataSetTypes.DataSetList.UsersRow)((DataRowView)bindingSourceUsers.Current).Row;
                String filter = "UserID=" + row.ID.ToString();
                bindingSourceUserLog.Filter = filter;
                bindingSourceUserTerminals.Filter = filter;
            }
            catch
            {
                bindingSourceUserLog.Filter = "null";
                bindingSourceUserTerminals.Filter = "null";
            }
        }

        private void Terminal_AddUser(object sender, EventArgs e)
        {
            try
            {
                Int32 CurrentID = ((DataSetTypes.DataSetList.TerminalsRow)((DataRowView)bindingSourceTerminals.Current).Row).ID;
                
                foreach (DataSetTypes.DataSetList.UserToTerminalRow row in dataSetList.UserToTerminal.Rows)
                {
                    if (row.TerminalID == CurrentID && row.UserID == (Int32)comboBoxTerminalAddUser.SelectedValue)
                    {
                        MessageBox.Show("User is already registered", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                dataSetList.UserToTerminal.AddUserToTerminalRow((Int32)comboBoxTerminalAddUser.SelectedValue, CurrentID);
            }
            catch { };
        }

        private void User_AddTerminal(object sender, EventArgs e)
        {
            try
            {
                Int32 CurrentID = ((DataSetTypes.DataSetList.UsersRow)((DataRowView)bindingSourceUsers.Current).Row).ID;

                foreach (DataSetTypes.DataSetList.UserToTerminalRow row in dataSetList.UserToTerminal.Rows)
                {
                    if (row.UserID == CurrentID && row.TerminalID == (Int32)comboBoxUserAddTerminal.SelectedValue)
                    {
                        MessageBox.Show("Terminal is already registered", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                dataSetList.UserToTerminal.AddUserToTerminalRow(CurrentID, (Int32)comboBoxUserAddTerminal.SelectedValue);
            }
            catch { };
            
        }

        private void TerminalSetting_Click(object sender, EventArgs e)
        {
            String CurrentName = ((DataSetTypes.DataSetList.TerminalsRow)((DataRowView)bindingSourceTerminals.Current).Row).Name;
            String CurrentIP = ((DataSetTypes.DataSetList.TerminalsRow)((DataRowView)bindingSourceTerminals.Current).Row).IP;

            FormTerminalSetting form = new FormTerminalSetting(CurrentIP, CurrentName);
            form.ShowDialog();
        }

        private void TimerRefresh_Tick(object sender, EventArgs e)
        {
            dataGridViewTerminalLog.Refresh();
            dataGridViewUserLog.Refresh();
        }
    }
}
