//StealthNet
//Copyright (C) 2010 T.Norad

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

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

namespace Regensburger.RShare
{
    internal sealed partial class LogControl : UserControl
    {
        private MainForm m_MainForm;

        public LogControl(MainForm mainForm)
        {
            if (mainForm == null)
                throw new ArgumentNullException("mainForm");

            m_MainForm = mainForm;
            InitializeComponent();

            // set log entries count to the last value
            logEntriesCountComboBox.SelectedIndex = Properties.Settings.Default.LogEntriesCountIndex;

            // attach this form as a LogObserver.
            LogObserver.Instance.AttachLogObserver();

            // set init display text
            updateLogDisplayText();
        }

        private void updateLogDisplayText()
        {
            logLabel.Text = string.Format(Properties.Resources.LogEntries, this.logListBox.Items.Count);
        }

        private void clearListButton_Click(object sender, EventArgs e)
        {
            logListBox.Items.Clear();
            updateLogDisplayText();
        }

        private void logEntriesCountComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.LogEntriesCountIndex = logEntriesCountComboBox.SelectedIndex;
        }

        private void updateTimer_Tick(object sender, EventArgs e)
        {
            try {
                LogObserver.Instance.LogEntryList.Lock();
                
                // remove the old entries
                while (logListBox.Items.Count > int.Parse(logEntriesCountComboBox.SelectedItem.ToString()))
                {
                    logListBox.Items.RemoveAt(logListBox.Items.Count - 1);
                }

                while (LogObserver.Instance.LogEntryList.Count > 0)
                {
                    // add entry to the list box
                    logListBox.Items.Insert(0, string.Format("{0:t} {1}", LogObserver.Instance.LogEntryList[0].TimeStamp, LogObserver.Instance.LogEntryList[0].Text));
                    LogObserver.Instance.LogEntryList.RemoveAt(0);
                }

                // update count text
                updateLogDisplayText();

            } finally {
                LogObserver.Instance.LogEntryList.Unlock();
            }
        }

        public void DetachLogObserver()
        {
            LogObserver.Instance.DetachLogObserver();
        }
    }
}
