
using Moq;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Mvc;
using User.Insight.Tracker.Controllers;
using User.Insight.Tracker.Services;
using User.Insight.Tracker.DataContracts;
using Microsoft.AspNetCore.Http;
using User.Insight.Tracker.DataContracts.Response;

namespace Insight.Tracker.UnitTest
{
    public class UserInsightTrackerControllerTests
    {
        private readonly Mock<ILogger<UserInsightTrackerController>> _loggerMock;
        private readonly Mock<IUserInsightTrackerService> _userTrackerServiceMock;
        private readonly UserInsightTrackerController _controller;
        public UserInsightTrackerControllerTests()
        {
            _loggerMock = new Mock<ILogger<UserInsightTrackerController>>();
            _userTrackerServiceMock = new Mock<IUserInsightTrackerService>();
            var contextAccessor = SetHttpContext();
            _controller = new UserInsightTrackerController(_loggerMock.Object, _userTrackerServiceMock.Object, contextAccessor.Object);
        }
        private Mock<IHttpContextAccessor> SetHttpContext()
        {
            var mockHttpContextAccessor = new Mock<IHttpContextAccessor>();
           var context = new DefaultHttpContext();          
            mockHttpContextAccessor.Setup(_ => _.HttpContext).Returns(context);
            return mockHttpContextAccessor;
        }

        [Fact]     
        public void GetCallDetails()
        {
            SearchRequest<CallDetailFilterRequest> req = new SearchRequest<CallDetailFilterRequest>() { PageSize=1,PageNumber=10,IgnorePaging=false, Filter = new CallDetailFilterRequest { CallStartedOn =null,CallEndedOn=null} };

            PageResponse<CallDetailsResponse> data = new PageResponse<CallDetailsResponse>();

            _userTrackerServiceMock.Setup(p => p.GetCallDetails(It.IsAny<SearchRequest<CallDetailFilterRequest>>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response =  _controller.GetCallDetails(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<CallDetailsResponse>>;
            Assert.True(responseData?.IsSuccess);
        }
        [Fact]
        public void GetSystemSleepDetails()
        {
            SearchRequest<SystemSleepRequest> req = new SearchRequest<SystemSleepRequest>() { PageSize = 1, PageNumber = 10, IgnorePaging = false, Filter = new SystemSleepRequest { Description = "", TriggeredOn = null } };

            PageResponse<SystemSleepDetailResponse> data = new PageResponse<SystemSleepDetailResponse>();

            _userTrackerServiceMock.Setup(p => p.GetSystemSleepDetails(It.IsAny<SearchRequest<SystemSleepRequest>>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetSystemSleepDetails(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<SystemSleepDetailResponse>>;
            Assert.True(responseData?.IsSuccess);
        }
        [Fact]
        public void GetRunningApplicationsDetailed()
        {
            SearchRequest<ApplicationTabDetailsRequest> req = new SearchRequest<ApplicationTabDetailsRequest>() { PageSize = 1, PageNumber = 10, IgnorePaging = false, Filter = new ApplicationTabDetailsRequest { ApplicationName = "", StartedOn = null,EndedOn=null, ResultsFor = null, EmpCode =  null } };

            PageResponse<ApplicationTabDetailsResponse> data = new PageResponse<ApplicationTabDetailsResponse>();

            _userTrackerServiceMock.Setup(p => p.GetRunningApplicationsDetailed(It.IsAny<SearchRequest<ApplicationTabDetailsRequest>>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetRunningApplicationsDetailed(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<ApplicationTabDetailsResponse>>;
            Assert.True(responseData?.IsSuccess);
        }
        
        [Fact]
        public void GetEmployeeWorkingTime()
        {
            SearchRequest<EmployeeWorkingTimeRequest> req = new SearchRequest<EmployeeWorkingTimeRequest>() { PageSize = 1, PageNumber = 10, IgnorePaging = false, Filter = new EmployeeWorkingTimeRequest { WorkLoggedOn = null } };

            PageResponse<EmployeeWorkingTimeResponse> data = new PageResponse<EmployeeWorkingTimeResponse>();

            _userTrackerServiceMock.Setup(p => p.GetEmployeeWorkingTime(It.IsAny<SearchRequest<EmployeeWorkingTimeRequest>>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetEmployeeWorkingTime(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<EmployeeWorkingTimeResponse>>;
            Assert.True(responseData?.IsSuccess);
        }
        [Fact]
        public void GetSystemLoginDetails()
        {
            SearchRequest<SystemLoginDetailsRequest> req = new SearchRequest<SystemLoginDetailsRequest>() { PageSize = 1, PageNumber = 10, IgnorePaging = false, Filter = new SystemLoginDetailsRequest { LoginTime= null, LogoutTime= null} };
            PageResponse<SystemLoginDetailsResponse> data = new PageResponse<SystemLoginDetailsResponse>();
            _userTrackerServiceMock.Setup(p => p.GetSystemLoginDetails(It.IsAny<SearchRequest<SystemLoginDetailsRequest>>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetSystemLoginDetails(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<SystemLoginDetailsResponse>>;
            Assert.True(responseData?.IsSuccess);
        }
        [Fact]
        public async Task GetRunningApplications()
        {
            SearchRequest<RunningApplicationRequest> req = new SearchRequest<RunningApplicationRequest>() { PageSize = 1, PageNumber = 10, IgnorePaging = false, Filter = new RunningApplicationRequest { StartedOn = null } };

            PageResponse<RunningApplicationResponse> data = new PageResponse<RunningApplicationResponse>();

            _userTrackerServiceMock.Setup(p => p.GetRunningApplications(It.IsAny<SearchRequest<RunningApplicationRequest>>(), It.IsAny<JwtTokenDetail>())).Returns(Task.FromResult(data));
            var response = await _controller.GetRunningApplications(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<RunningApplicationResponse>>;
            Assert.True(responseData?.IsSuccess);
        }

        [Fact]
        public void GetGraphicData()
        {
            SearchRequest<GraphDetailsRequest> req = new SearchRequest<GraphDetailsRequest>() { PageSize = 1, PageNumber = 10, IgnorePaging = false, Filter = new GraphDetailsRequest { RequestTime = DateTime.Now } };

            PageResponse<GraphDetailsResponse> data = new PageResponse<GraphDetailsResponse>();

            _userTrackerServiceMock.Setup(p => p.GetGraphicData(It.IsAny<SearchRequest<GraphDetailsRequest>>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetGraphicData(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<GraphDetailsResponse>>;
            Assert.True(responseData?.IsSuccess);
        }

        [Fact]
        public void GetDashboardData()
        {
            SearchRequest<DashboardDetailsRequest> req = new SearchRequest<DashboardDetailsRequest>() { PageSize = 1, PageNumber = 10, IgnorePaging = false, Filter = new DashboardDetailsRequest { RequestStartDate = DateTime.Now, RequestEndDate = DateTime.Now } };

            PageResponse<DashboardDetailsResponse> data = new PageResponse<DashboardDetailsResponse>();

            _userTrackerServiceMock.Setup(p => p.GetDashboardData(It.IsAny<SearchRequest<DashboardDetailsRequest>>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetDashboardData(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<DashboardDetailsResponse>>;
            Assert.True(responseData?.IsSuccess);
        }

        [Fact]
        public void GetUserProfileData()
        {
            SearchRequest<UserProfileRequest> req = new SearchRequest<UserProfileRequest>() { PageSize = 1, PageNumber = 10, IgnorePaging = false, Filter = new UserProfileRequest { RequestDateStart = DateTime.Now } };

            PageResponse<UserProfileResponse> data = new PageResponse<UserProfileResponse>();

            _userTrackerServiceMock.Setup(p => p.GetUserProfileData(It.IsAny<SearchRequest<UserProfileRequest>>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetUserProfileData(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<UserProfileResponse>>;
            Assert.True(responseData?.IsSuccess);
        }

        [Fact]
        public void GetAllActivity()
        {
            int Type = 1;
            List<ActivityDetailsResponse> data = new List<ActivityDetailsResponse>();
            _userTrackerServiceMock.Setup(p => p.GetAllActivity(Type, It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetAllActivity(Type);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<List<ActivityDetailsResponse>>;
            Assert.True(responseData?.IsSuccess);
        }

        [Fact]
        public void GetUserActivityDetails()
        {
            SearchRequest<UserActivityDetailsRequest> req = new SearchRequest<UserActivityDetailsRequest>() { PageSize = 1, PageNumber = 10, IgnorePaging = false, Filter = new UserActivityDetailsRequest { Activity = "Sleep" , LoginTime = null, LogoutTime = null } };

            PageResponse<UserActivityDetailsResponse> data = new PageResponse<UserActivityDetailsResponse>();

            _userTrackerServiceMock.Setup(p => p.GetUserActivityDetails(It.IsAny<SearchRequest<UserActivityDetailsRequest>>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetUserActivityDetails(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<UserActivityDetailsResponse>>;
            Assert.True(responseData?.IsSuccess);
        }

        [Fact]
        public void GetApplicationActivityReport()
        {
            ApplicationActivityReportRequest req = new ApplicationActivityReportRequest();   

            List<ApplicationActivityReportResponseResult> data = new List<ApplicationActivityReportResponseResult>();

            _userTrackerServiceMock.Setup(p => p.GetApplicationActivityReport(It.IsAny<ApplicationActivityReportRequest>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetApplicationActivityReport(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<ApplicationActivityReportRequest>>;
            Assert.True(true);
        }


        [Fact]
        public void GetCallsActivityReport()
        {
            CallsActivityReportRequest req = new CallsActivityReportRequest();

            List<CallsActivityReportResponseResult> data = new List<CallsActivityReportResponseResult>();

            _userTrackerServiceMock.Setup(p => p.GetCallsActivityReport(It.IsAny<CallsActivityReportRequest>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetCallsActivityReport(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<ApplicationActivityReportRequest>>;
            Assert.True(true);
        }

        [Fact]
        public void GetSessionsActivityReport()
        {
            SessionActivityReportRequest req = new SessionActivityReportRequest();

            List<SessionActivityReportResponseResult> data = new List<SessionActivityReportResponseResult>();

            _userTrackerServiceMock.Setup(p => p.GetSessionsActivityReport(It.IsAny<SessionActivityReportRequest>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetSessionsActivityReport(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<SessionActivityReportRequest>>;
            Assert.True(true);
        }

        [Fact]
        public void GetLogInActivityReport()
        {
            LogInActivityReportRequest req = new LogInActivityReportRequest();

            List<LogInActivityReportResponseResult> data = new List<LogInActivityReportResponseResult>();

            _userTrackerServiceMock.Setup(p => p.GetLogInActivityReport(It.IsAny<LogInActivityReportRequest>(), It.IsAny<JwtTokenDetail>())).Returns(data);
            var response = _controller.GetLogInActivityReport(req);
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponse<LogInActivityReportRequest>>;
            Assert.True(true);
        }

        [Fact]
        public async Task GetUserExists()
        {
            
            PageResponseRequest<AllUserDetailsUser> data = new PageResponseRequest<AllUserDetailsUser>();
             _userTrackerServiceMock.Setup(p => p.GetUserExists(It.IsAny<JwtTokenDetail>())).ReturnsAsync(data);
            var response = await _controller.GetUserExists();
            var objectResult = response.Result as ObjectResult;
            var responseData = objectResult?.Value as Payload<PageResponseRequest<AllUserDetailsUser>>;
            if(responseData!=null)
            {
                if(responseData.IsSuccess)
                    Assert.True(responseData?.IsSuccess);
                else
                    Assert.False(responseData?.IsSuccess);
            }            
        }        
    }
}
