using System;
using System.Windows.Forms;
using log4net;
using CitiLogReport;

namespace CitiLogReportDemo
{
    public partial class CitiLogReportDemo : Form
    {
        public static readonly ILog Logger = LogManager.GetLogger("CitiLogReportDemo");
        private bool connected;

        private CitiLogReport.CitiLogReport citiLogReport;
        public CitiLogReportDemo()
        {
            Logger.Info(string.Format("CitiLogReportDemo: Start..."));
            InitializeComponent();
            DatabaseInfo databaseInfo = new DatabaseInfo()
            {                
                IP  = textBoxDBServer.Text,
                Port = int.Parse(textBoxDBPort.Text),
                User = textBoxDBUser.Text,
                Password = textBoxDBPassowrd.Text,
                Database = textBoxDatabase.Text,
                LocationTable = textBoxTable.Text
            };
            citiLogReport = new CitiLogReport.CitiLogReport((int)numericUpDownID.Value, databaseInfo);
        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (!connected)
            {
                int port = 44000;
                int.TryParse(textBoxPort.Text, out port);
                int interval = 30;
                int.TryParse(textBoxPeriod.Text, out interval);

                if (citiLogReport.ReadLocationTable())
                {
                    if (connected = citiLogReport.Start(textBoxServer.Text, port, interval))
                    {
                        buttonConnect.Text = "Disconnect";
                    }
                }
            }
            else
            {
                citiLogReport.Stop();
                buttonConnect.Text = "Connect";
                connected = false;
            }
        }

        private void CitiLogReportDemo_FormClosing(object sender, FormClosingEventArgs e)
        {
            citiLogReport.Stop();
        }
    }

    /// <summary>
    /// Location information
    /// </summary>
    public class Location
    {
        public uint id { get; set; }

        public string name { get; set; }

        public string desc { get; set; }
    }
}
