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

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

        }

        public List<Language> GetListOfLanguages()
        {
            List<Languages> languageList = _context.Languages.Where(l => l.IsActive == true).ToList();
            List<Language> iList = _mapper.MapList<Languages, Language>(languageList);
            return iList;
        }

        public Language GetLanguageById(int id)
        {
            Language language = _common.GetLanguageById(id);
            return language;
        }

        public Language GetLanguageByCode(string code)
        {
            Language language = _common.GetLanguageByCode(code);
            return language;
        }

        public Language AddLanguage(int user_id, LanguageAddModel language)
        {
            try
            {
                //Step-1: Begin a transaction
                _context.Database.BeginTransaction();

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

                return _common.GetLanguageById(newLanguages.Id);
            }
            catch
            {
                _context.Database.RollbackTransaction();
                return null;
            }

        }

        public Language UpdateLanguage(int user_id, int id, LanguageEditModel theLanguage)
        {
            try
            {
                _context.Database.BeginTransaction();

                Languages languages = _context.Languages.Where(l => l.Id == id).FirstOrDefault();

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

                int count = 0;

                if (theLanguage.Code != null && theLanguage.Code != languages.Code) { languages.Code = theLanguage.Code; count++; }
                if (theLanguage.Name != null && theLanguage.Name != languages.Name) { languages.Name = theLanguage.Name; count++; }

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

                _context.Languages.Update(languages);
                _context.SaveChanges();
                _context.Database.CommitTransaction();

                return _common.GetLanguageById(languages.Id);
            }
            catch
            {
                _context.Database.RollbackTransaction();
                return null;
            }
        }

        public Language RestoreLanguage(int user_id, int id)
        {
            try
            {
                _context.Database.BeginTransaction();

                Languages languages = _context.Languages.Where(l => l.Id == id).FirstOrDefault();

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

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

                _context.Languages.Update(languages);
                _context.SaveChanges();
                _context.Database.CommitTransaction();

                return _common.GetLanguageById(languages.Id);
            }
            catch
            {
                _context.Database.RollbackTransaction();
                return null;
            }
        }

    }
}
