using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Mercury.Mobile.Logging;
using Mercury.Mobile.Logging.Loggers;

namespace Mercury.Mobile.Logging.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class Logger_basic_specs
    {
        public Logger_basic_specs()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void Can_create_an_in_memory_logger()
        {
            MemoryLogger memLog = new MemoryLogger(60);
            Assert.IsNotNull(memLog);
            Assert.IsNotNull(memLog.Formatter);
            Assert.IsNotNull(memLog.Filter);
            Assert.IsTrue(memLog.Name == "");

            memLog.Debug("Testing");
            memLog.Debug(() => { return "A test statement."; });
            memLog.Debug(22, "Testing message code 22.");
            memLog.Debug(22, () => { return "Another test statement."; });
            memLog.Debug("This message has a '{0}' severity level.", LogSeverity.Debug);
            memLog.Debug(22, "Another message with event ID {0}.", 22);

            Assert.IsTrue(memLog.Head == 6);
            var log = memLog.GetLogAsEnumerable();
            var logList = new List<string>();
            foreach (string msg in log)
            {
                if (msg == null)
                    break;
                logList.Add(msg);
            }
            Assert.IsTrue(logList.Count == 6);
        }

        [TestMethod]
        public void Can_log_with_options()
        {
            MemoryLogger memLog = new MemoryLogger(60);
            memLog.Formatter.Options = LogOptions.DateTime | LogOptions.ProcessId | LogOptions.ThreadId;

            memLog.Info("Test message with callstack.");
            var testLog = memLog.CopyLog();
            Assert.IsNotNull(testLog);
            Assert.IsTrue(testLog[0].Length > 100);

            memLog.Formatter.Options = LogOptions.ProcessId | LogOptions.ThreadId | LogOptions.DateTime | LogOptions.Timestamp;
            memLog.Error(70, () => { return "An error message with formatter options."; });
            testLog = memLog.CopyLog();
            Assert.IsTrue(testLog[1].Length > 100);
        }

        [TestMethod]
        public void Can_use_severity_and_category_filters()
        {
            MemoryLogger memLog = new MemoryLogger(60);
            memLog.SeverityThreshold = LogSeverity.Critical;

            // Severity threshold filters...
            memLog.Debug("Will not work!");
            memLog.Info("Will not work!");
            memLog.Warn("Will not work!");
            memLog.Error("Will not work!");
            var testLog = memLog.CopyLog();
            Assert.IsTrue(testLog[0] == null);

            memLog.Critical("This will work!");
            testLog = memLog.CopyLog();
            Assert.IsFalse(testLog[0] == null);

            // Category filters...
            memLog.Clear();
            var fltr = new CategoryFilter(true);
            fltr.AddCategory("DataCore");
            fltr.AddCategory("CoreLogic");
            memLog.Filter = fltr;

            memLog.WithFilter("Data").Critical(9000, "Critical data error occurred.");
            testLog = memLog.CopyLog();
            Assert.IsTrue(testLog[0] == null);

            memLog.WithFilter("DataCore").Critical(9000, "Critical data error occurred.  This will work!");
            testLog = memLog.CopyLog();
            Assert.IsFalse(testLog[0] == null);

            var exFltr = new CategoryFilter(false);
            exFltr.AddCategory("Entity");
            exFltr.AddCategory("UI");
            memLog.Filter = exFltr;

            memLog.Clear();
            memLog.WithFilter("Entity").Critical(9000, "Cannot write critical message when category is blacklisted.");
            testLog = memLog.CopyLog();
            Assert.IsTrue(testLog[0] == null);

            memLog.WithFilter("").Critical(9000, "Can write on any non-blacklisted category.");
            testLog = memLog.CopyLog();
            Assert.IsFalse(testLog[0] == null);
        }
    }
}
