﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DaysTest
{
    using System.Linq;

    using AdventCommon;

    using global::Day6;

    using InputReader;

    [TestClass]
    public class Day6
    {
        private System.IO.Stream CreateResource()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Day6Test.Input.txt");
        }

        [TestMethod]
        public void TestResourceHas300Commands()
        {
            var show = new LightShow();
            using (var stream = CreateResource())
            {
                var textStream = InputReader.ReadResource(stream);
                var commands = show.CreateCommands(new Tokenizer().Tokenize(textStream));
                Assert.AreEqual(300, commands.ToList().Count);
            }
        }

        [TestMethod]
        public void DefaultGridHasNoDiodsLit()
        {
            var grid = new LightGrid<LightDiod>(10, 10, () => new LightDiod(false));
            Assert.AreEqual(0, grid.GetDiods().Count(x => x.Item2.On));
        }

        [TestMethod]
        public void ToggleEntireAreaTogglesAll()
        {
            var grid = new LightGrid<LightDiod>(10, 10, () => new LightDiod(false));
            grid.Execute(new LightCommand(new Rectangle<int>(new Point2D<int>(0,0), new Point2D<int>(9,9)),LightCommandType.Toggle ));
            Assert.AreEqual(100, grid.GetDiods().Count(x => x.Item2.On));
        }

        [TestMethod]
        public void LitDiodsAre400410()
        {
            var show = new LightShow();
            using (var stream = CreateResource())
            {
                var textStream = InputReader.ReadResource(stream);
                var commands = show.CreateCommands(new Tokenizer().Tokenize(textStream));

                var grid = new LightGrid<LightDiod>(1000, 1000, () => new LightDiod(false));
                foreach (var command in commands)
                {
                    grid.Execute(command);
                }

                Assert.AreEqual(400410, grid.GetDiods().Count(x => x.Item2.On));
            }
        }

        [TestMethod]
        public void BrightnessIs15343601()
        {
            var show = new LightShow();
            using (var stream = CreateResource())
            {
                var textStream = InputReader.ReadResource(stream);
                var commands = show.CreateCommands(new Tokenizer().Tokenize(textStream));

                var grid = new LightGrid<DimmedLightDiod>(1000, 1000, () => new DimmedLightDiod(0));

                foreach (var command in commands)
                {
                    grid.Execute(command);
                }

                Assert.AreEqual(15343601, grid.GetDiods().Sum(x => x.Item2.Brightness));
            }
        }
    }
}
