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

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

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

        }

        public List<Role> GetListOfRoles()
        {
            List<Roles> roleList = _context.Roles.ToList();
            List<Role> iList = _mapper.MapList<Roles, Role>(roleList);
            return iList;
        }

        public dynamic GetRoleById(int id)
        {
            var role = _common.GetRoleDetailByRoleId(id);
            return role;
        }

        public async Task<Role> AddRole(int user_id, RoleAddModel role)
        {
                try
                {
                    //Step-1: Begin a transaction
                    _context.Database.BeginTransaction();

                    //Step-2: Add Exam details
                    Roles newRoles = new Roles
                    {
                        Code = role.Code,
                        Name = role.Name,
                        Description = role.Description,
                        CreatedBy = user_id,
                        UpdatedBy = user_id,
                        CreatedOn = _common.NowIndianTime(),
                        UpdatedOn = _common.NowIndianTime(),
                        IsActive = true
                    };
                    _context.Roles.Add(newRoles);
                    await _context.SaveChangesAsync();
                    _context.Database.CommitTransaction();

                    return _common.GetRoleDetailByRoleId(newRoles.Id);
                }
                catch
                {
                    _context.Database.RollbackTransaction();
                    return null;
                }
        }

        public Role UpdateRole(int user_id, int id, RoleEditModel theRole)
        {
            try
            {
                _context.Database.BeginTransaction();

                Roles roles = _context.Roles.Where(r => r.Id == id).FirstOrDefault();

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

                int count = 0;

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

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

                _context.Roles.Update(roles);
                _context.SaveChanges();
                _context.Database.CommitTransaction();

                return _common.GetRoleDetailByRoleId(roles.Id);
            }
            catch
            {
                _context.Database.RollbackTransaction();
                return null;
            }
        }


        public Role RestoreRole(int user_id, int id)
        {
            try
            {
                _context.Database.BeginTransaction();

                Roles roles = _context.Roles.Where(r => r.Id == id).FirstOrDefault();

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

                roles.IsActive = true;
                roles.UpdatedOn = _common.NowIndianTime();
                roles.UpdatedBy = user_id;
                
                _context.Roles.Update(roles);
                _context.SaveChanges();
                _context.Database.CommitTransaction();

                return _common.GetRoleDetailByRoleId(roles.Id);
            }
            catch
            {
                _context.Database.RollbackTransaction();
                return null;
            }
        }


    }
}
