using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Microsoft.Extensions.Options;
using OnlineAssessment.Common;
using OnlineAssessment.Domain.Models;
using OnlineAssessment.Domain.ViewModels;

namespace OnlineAssessment.Data.EFCore
{
    public class EFCoreExamTypeRepository : EfCoreRepository<ExamTypes, OnlineAssessmentContext>
    {
        public readonly OnlineAssessmentContext _context;
        private readonly IMapper _mapper;
        public readonly EfCoreCommonRepository _common;

        public EFCoreExamTypeRepository(OnlineAssessmentContext context, IMapper mapper, IOptionsSnapshot<ResponseMessage> responseMessage) : base(context, responseMessage)
        {
            _context = context;
            _mapper = mapper;
            _common = new EfCoreCommonRepository(_context);
        }

        public List<ExamType> GetListOfExamTypes()
        {
            List<ExamTypes> examTypeList = _context.ExamTypes.Where(e => e.IsActive == true).ToList();
            List<ExamType> iList = _mapper.MapList<ExamTypes, ExamType>(examTypeList);
            return iList;
        }

        public ExamType GetExamTypeById(int id)
        {
            var examType = _common.GetExamTypeById(id);
            return examType;
        }

        public ExamType AddExamType(int user_id, ExamTypeAddModel examtype)
        {
            try
            {
                //Step-1: Begin a transaction
                _context.Database.BeginTransaction();

                //Step-2: Add Exam details
                ExamTypes newExamTypes = new ExamTypes
                {
                    Code = examtype.Code,
                    Name = examtype.Name,
                    Description = examtype.Description,
                    CreatedBy = user_id,
                    UpdatedBy = user_id,
                    CreatedOn = _common.NowIndianTime(),
                    UpdatedOn = _common.NowIndianTime(),
                    IsActive = true
                };
                _context.ExamTypes.Add(newExamTypes);
                _context.SaveChanges();
                _context.Database.CommitTransaction();

                return _common.GetExamTypeById(newExamTypes.Id);
            }
            catch
            {
                _context.Database.RollbackTransaction();
                return null;
            }

        }

        public ExamType UpdateExamType(int user_id, int id, ExamTypeEditModel theExamType)
        {
            try
            {
                _context.Database.BeginTransaction();

                ExamTypes examTypes = _context.ExamTypes.Where(e => e.Id == id).FirstOrDefault();

                //Check : Deleted examtype cant be updated until made activated
                if(examTypes == null || examTypes.IsActive == false || theExamType == null)
                {
                    return null;
                }

                int count = 0;

                if (theExamType.Code != null && theExamType.Code != examTypes.Code) { examTypes.Code = theExamType.Code; count++; }
                if (theExamType.Name != null && theExamType.Name != examTypes.Name) { examTypes.Name = theExamType.Name; count++; }
                if (theExamType.Description != null && theExamType.Description != examTypes.Description) { examTypes.Description = theExamType.Description; count++; }

                if(count > 0)
                {
                    examTypes.UpdatedOn = _common.NowIndianTime();
                    examTypes.UpdatedBy = user_id;
                }
                
                _context.ExamTypes.Update(examTypes);
                _context.SaveChanges();
                _context.Database.CommitTransaction();

                return _common.GetExamTypeById(examTypes.Id);
            }
            catch
            {
                _context.Database.RollbackTransaction();
                return null;
            }
        }

        public ExamType RestoreExamType(int user_id, int id)
        {
            try
            {
                _context.Database.BeginTransaction();

                ExamTypes examtypes = _context.ExamTypes.Where(e => e.Id == id).FirstOrDefault();

                //Check : cant restore active items
                if (examtypes == null || examtypes.IsActive == true)
                {
                    return null;
                }

                examtypes.IsActive = true;
                examtypes.UpdatedOn = _common.NowIndianTime();
                examtypes.UpdatedBy = user_id;

                _context.ExamTypes.Update(examtypes);
                _context.SaveChanges();
                _context.Database.CommitTransaction();

                return _common.GetExamTypeById(examtypes.Id);
            }
            catch
            {
                _context.Database.RollbackTransaction();
                return null;
            }
        }

    }
}
