using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace Nappat.Testing.WPF
{
    public class LogWriter
    {
        #region Private Fields
        private RichTextBox logTextBox; 
        #endregion

        #region Constructors
        public LogWriter(RichTextBox logTextBox)
        {
            this.logTextBox = logTextBox;
        } 
        #endregion

        #region Public Methods
        public void UpdateBox(DataSender dataSender)
        {
            TextRange tr = new TextRange(logTextBox.Document.ContentEnd, logTextBox.Document.ContentEnd);
            tr.Text = dataSender.LogMessages.ToString();
            if (dataSender.LogMessages.Category == "Program")
            {
                if (dataSender.LogMessages.Severity == "Info")
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
                else if (dataSender.LogMessages.Severity == "Success")
                {
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Green);
                }
                else if (dataSender.LogMessages.Severity == "FatalError")
                {
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
                    //tr.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                }
                else
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Orange);
            }
            else if (dataSender.LogMessages.Category == "Common")
            {
                if (dataSender.LogMessages.Severity == "Info")
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
                else if (dataSender.LogMessages.Severity == "FatalError")
                {
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
                    //tr.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                }
                else
                    tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Orange);
            }
            else
            {
                tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Gray);
            }
            logTextBox.AppendText(Environment.NewLine);
            logTextBox.ScrollToEnd();
        } 
        #endregion
    }
}
