using Infrastructure.DataAccess;
using Infrastructure.Interfaces;
using Infrastructure.Models;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Infrastructure.Services
{
    public class TestCaseService : ITestCaseService, IDisposable
    {
        public TestCaseService() : this(new TestProjectService(), new AppDbContext())
        {
        }

        public TestCaseService(ITestProjectService testProjectService, AppDbContext context)
        {
            _testProjectService = testProjectService;
            _context = context;
        }

        public ITestProjectService _testProjectService { get; private set; }
        public AppDbContext _context { get; private set; }


        public Task AddStepToTestCase(StepResultDefinition step, int testCaseID)
        {
            step.TestCaseID = testCaseID;

            _context.StepResultDefinitions.Add(step);

            return _context.SaveChangesAsync();
        }


        public Task CreateTestCase(TestCase testCase, string creatorID)
        {
            // set creator of test case
            testCase.CreatorID = creatorID;
            
            // get current projectID
            var projectID = _testProjectService.Project.ID;
            var projectName = _testProjectService.Project.ShortName;
            
            // set projectID to test case
            testCase.ProjectID = projectID;

            // update counter of IDs in project
            _testProjectService.UpdateExternalIDCounter(projectID);

            // set the external test case id, e.g. WHD123
            testCase.ExternalID = projectName + _testProjectService.Project.ExternalIDCounter;

            _context.TestCases.Add(testCase);
            return _context.SaveChangesAsync();
        }


        public void CreateTestCase(TestCase testCase, string creatorID, TestProject project)
        {
            // set creator of test case
            testCase.CreatorID = creatorID;

            // get current projectID
            var projectName = project.ShortName;

            // set projectID to test case
            testCase.ProjectID = project.ID;

            // update counter of IDs in project
            _testProjectService.UpdateExternalIDCounter(project.ExternalIDCounter);

            // set the external test case id, e.g. WHD123
            testCase.ExternalID = projectName + _testProjectService.Project.ExternalIDCounter;

            _context.TestCases.Add(testCase);
            _context.SaveChanges();
        }


        public IEnumerable<TestCase> ListTestCases()
        {
            var projectID = _testProjectService.Project.ID;

            return _context.TestCases.Where(t => t.ProjectID == projectID);
        }


        public IEnumerable<TestCase> ListTestCases(int testSuiteID)
        {
            var projectID = _testProjectService.Project.ID;

            return _context.TestCases.Where(t => (t.ProjectID == projectID) && (t.TestSuiteID == testSuiteID));
        }

        
        public TestCase GetTestCase(int testCaseID)
        {
            return _context.TestCases.Find(testCaseID);
        }


        public int ConvertFromExternalID(string externalID)
        {
            return _context.TestCases.Where(t => t.ExternalID == externalID).First().ID;
        }


        public void Dispose()
        {
            _context.Dispose();
        }
    }
}