﻿namespace Beekeeper.Web.Controllers.Tests.HoneycombController
{
    using System.Linq;
    using System.Reflection;
    using Areas.User.Controllers;
    using Areas.User.ViewModels.Honeycomb;
    using Data.Models;
    using Infrastructure.Mapping;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;
    using Services.Data.Common.Contracts;
    using Services.Web;
    using TestStack.FluentMVCTesting;

    [TestClass]
    public class Details_Should
    {
        private Mock<IBaseDataService<Honeycomb>> honeycombServiceMock;
        private Mock<IBaseDataService<HoneycombReport>> honeycombReportServiceMock;
        private IIdentifierProvider identifierProvider;

        [TestInitialize]
        public void Initialize()
        {
            var autoMapperConfig = new AutoMapperConfig();
            autoMapperConfig.Execute(Assembly.GetAssembly(typeof(ApiaryController)));

            this.honeycombServiceMock = new Mock<IBaseDataService<Honeycomb>>();
            this.honeycombReportServiceMock = new Mock<IBaseDataService<HoneycombReport>>();
            this.identifierProvider = new IdentifierProvider();
        }

        [TestCleanup]
        public void Cleanup()
        {
            this.honeycombServiceMock = null;
            this.honeycombReportServiceMock = null;
            this.identifierProvider = null;
        }

        [TestMethod]
        public void ReturnViewWithModelWithCorrectProperties_WhenThereIsAModelWithThePassedId()
        {
            // Arange
            Honeycomb honeycombModel = new Honeycomb()
            {
                Id = 123,
                Number = 1,
                Area = 0.1,
                SuperId = 12,
                Honey = 0.1,
                Pollen = 0.2,
                Brood = 0.5,
                UserId = "temp"
            };

            this.honeycombServiceMock.Setup(m => m.GetById(honeycombModel.Id)).Returns(honeycombModel);

            HoneycombController honeycombController = new HoneycombController(this.honeycombServiceMock.Object, this.honeycombReportServiceMock.Object, this.identifierProvider);
            honeycombController.CurrentUserId = "temp";

            // Act & Assert
            honeycombController
                .WithCallTo(a => a.Details(this.identifierProvider.EncodeId(honeycombModel.Id)))
                .ShouldRenderDefaultView()
                .WithModel<HoneycombDetailsViewModel>(vm =>
                {
                    Assert.AreEqual(honeycombModel.Id, vm.Id);
                    Assert.AreEqual(honeycombModel.Number, vm.Number);
                    Assert.AreEqual(honeycombModel.Area, vm.Area);
                    Assert.AreEqual(honeycombModel.SuperId, vm.SuperId);
                    Assert.AreEqual(honeycombModel.Honey, vm.Honey);
                    Assert.AreEqual(honeycombModel.Pollen, vm.Pollen);
                    Assert.AreEqual(honeycombModel.Brood, vm.Brood);
                    CollectionAssert.AreEqual(honeycombModel.Reports.ToList(), vm.Reports.ToList());
                });
        }

        [TestMethod]
        public void ReturnErrorView_WhenCurrentUserIsNotTheCUser()
        {
            // Arange
            Honeycomb honeycombModel = new Honeycomb()
            {
                Id = 1,
                UserId = "temp"
            };

            this.honeycombServiceMock.Setup(m => m.GetById(honeycombModel.Id)).Returns(honeycombModel);

            HoneycombController honeycombController = new HoneycombController(this.honeycombServiceMock.Object, this.honeycombReportServiceMock.Object, this.identifierProvider);
            honeycombController.CurrentUserId = "temp2";

            // Act & Assert
            honeycombController
                .WithCallTo(a => a.Details(this.identifierProvider.EncodeId(honeycombModel.Id)))
                .ShouldRenderView("NoPermissions");
        }
    }
}
