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 EFCoreQuestionTypeRepository : EfCoreRepository<QuestionTypes, OnlineAssessmentContext>
    {
        public readonly OnlineAssessmentContext _context;
        private readonly IMapper _mapper;
        public readonly EfCoreCommonRepository _common;

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

        public List<QuestionType> GetListOfQuestionTypes()
        {
            List<QuestionTypes> questionTypeList = _context.QuestionTypes.Where(q => q.IsActive == true).ToList();
            List<QuestionType> iList = _mapper.MapList<QuestionTypes, QuestionType>(questionTypeList);
            return iList;
        }

        public QuestionType GetQuestionTypeById(int id)
        {
            QuestionType questionType = _common.GetQuestionTypeById(id);
            return questionType;
        }

        public QuestionType AddQuestionType(int user_id, QuestionTypeAddModel questionType)
        {
            try
            {
                //Step-1: Begin a transaction
                _context.Database.BeginTransaction();

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

                return _common.GetQuestionTypeById(newQuestionTypes.Id);
            }
            catch
            {
                _context.Database.RollbackTransaction();
                return null;
            }
        }

        public QuestionType UpdateQuestionType(int user_id, int id, QuestionTypeEditModel theQnsType)
        {
            try
            {
                _context.Database.BeginTransaction();

                QuestionTypes questionTypes = _context.QuestionTypes.Where(q => q.Id == id).FirstOrDefault();

                //Check : Deleted questiontype cant be updated until made activated
                if (questionTypes == null || questionTypes.IsActive == false || theQnsType == null)
                {
                    return null;
                }

                int count = 0;

                if (theQnsType.Code != null && theQnsType.Code != questionTypes.Code) { questionTypes.Code = theQnsType.Code; count++; }
                if (theQnsType.Name != null && theQnsType.Name != questionTypes.Name) { questionTypes.Name = theQnsType.Name; count++; }

                if (count > 0)
                {
                    questionTypes.UpdatedOn = _common.NowIndianTime();
                    questionTypes.UpdatedBy = user_id;
                }

                _context.QuestionTypes.Update(questionTypes);
                _context.SaveChanges();
                _context.Database.CommitTransaction();

                return _common.GetQuestionTypeById(questionTypes.Id);
            }
            catch
            {
                _context.Database.RollbackTransaction();
                return null;
            }
        }

        public QuestionType RestoreQuestionType(int user_id, int id)
        {
            try
            {
                _context.Database.BeginTransaction();

                QuestionTypes qnstypes = _context.QuestionTypes.Where(r => r.Id == id).FirstOrDefault();

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

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

                _context.QuestionTypes.Update(qnstypes);
                _context.SaveChanges();
                _context.Database.CommitTransaction();

                return _common.GetQuestionTypeById(qnstypes.Id);
            }
            catch
            {
                _context.Database.RollbackTransaction();
                return null;
            }
        }

    }
}
