using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using ResourcesManager.Controllers;
using ResourcesManager.Interfaces;
using ResourcesManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace ResourcesManager.Controllers.Tests
{
    [TestClass()]
    public class NotificationsControllerTests
    {
        List<Notification> expectedNotifications;
        Mock<INotificationRepository> mockNotifRepo;
        NotificationsController notifController;

        [TestInitialize]
        public void InitializeTestData()
        {
            expectedNotifications = new List<Notification> {
                new Notification() { Id=1, Title = "Cím", Description = "text text" },
                new Notification() { Id=2, Title = "Cím", Description = "text text", Readed = false },
                new Notification() { Id=3, Title = "Cím", Description = "text text" }
            };
            mockNotifRepo = new Mock<INotificationRepository>() { CallBase = true };

            mockNotifRepo.Setup(m => m.GetNotifications()).Returns(expectedNotifications);

            mockNotifRepo.Setup(m => m.GetNotificationByID(It.IsAny<int>())).Returns((int id) =>
            {
                return expectedNotifications.FirstOrDefault(x => x.Id == id);
            });

            mockNotifRepo.Setup(m => m.InsertNotification(It.IsAny<Notification>())).Returns((Notification notif) =>
            {
                expectedNotifications.Add(notif);
                return true;
            });

            mockNotifRepo.Setup(m => m.UpdateNotification(It.IsAny<Notification>())).Returns((Notification target) =>
            {
                var notif = expectedNotifications.FirstOrDefault(x => x.Id == target.Id);
                if (notif == null)
                {
                    return false;
                }
                notif.Title = target.Title;
                notif.Description = target.Description;
                notif.Readed = target.Readed;
                notif.Logo = target.Logo;
                notif.Link = target.Link;
                notif.User = target.User;
                notif.CreateDate = target.CreateDate;
                return true;
            });

            mockNotifRepo.Setup(m => m.DeleteNotification(It.IsAny<int>())).Returns((int id) =>
            {
                var notif = expectedNotifications.FirstOrDefault(x => x.Id == id);
                if (notif == null)
                {
                    return false;
                }
                expectedNotifications.Remove(notif);
                return true;
            });
        }

        [TestMethod()]
        public void Notification_Unread_Test()
        {
            var result = notifController.GetUnread();
            Assert.IsNotNull((result as ViewResult).Model);
        }

        [TestMethod()]
        public void Notification_Readed_Test()
        {
            var result = notifController.Readed(1);

            Assert.Fail();
        }

        [TestMethod()]
        public void Notification_MakeReadAll_Test()
        {
            var result = notifController.MakeReadAll();

            Assert.Fail();
        }

        [TestCleanup]
        public void Notifications_CleanUpTestData()
        {
            expectedNotifications = null;
            mockNotifRepo = null;
        }
    }
}