//--------------------------------------------------------------------
//     此代码由T4模板自动生成  byzhouhang 20210918
//	   生成时间 2023-05-08 15:26:52 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------

using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Infrastructure;
using Microsoft.AspNetCore.Mvc;

namespace IRaCIS.Core.Application.Service
{
    /// <summary>
    /// 用户WL模板
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Reading")]
    public class UserWLTemplateService(IRepository<UserWLTemplate> _userWLTemplateRepository,
         IRepository<ReadingUserConfig> _readingUserConfigRepository,
        IRepository<UserRole> _userRoleRepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService
    {



        /// <summary>
        /// 获取模板
        /// </summary>
        /// <param name="inQuery"></param>
        /// <returns></returns>
        public async Task<List<UserWLTemplateView>> GetUserWLTemplateList(UserWLTemplateQuery inQuery)
        {
            var userWLTemplateQueryable = _userWLTemplateRepository
            .Where(x => x.UserId == _userInfo.UserRoleId)
            .ProjectTo<UserWLTemplateView>(_mapper.ConfigurationProvider);
            return await userWLTemplateQueryable.ToListAsync();
        }

        /// <summary>
        /// 新增修改模板
        /// </summary>
        /// <param name="addOrEditUserWLTemplate"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddOrUpdateUserWLTemplate(UserWLTemplateAddOrEdit addOrEditUserWLTemplate)
        {
            addOrEditUserWLTemplate.UserId = _userInfo.UserRoleId;
            if (addOrEditUserWLTemplate.Id == null)
            {
                var count = await _userWLTemplateRepository.Where(x => x.UserId == _userInfo.UserRoleId).CountAsync();
                if (count >= 10)
                {
                    throw new BusinessValidationFailedException(_localizer["UserWLTS_MaxTemplate"]);
                }

            }

            if (await _userWLTemplateRepository.AnyAsync(x => x.UserId == _userInfo.UserRoleId && x.TemplateName == addOrEditUserWLTemplate.TemplateName && x.Id != addOrEditUserWLTemplate.Id))
            {
                throw new BusinessValidationFailedException(_localizer["UserWLTS_NameRepeated"]);
            }

            if (await _userWLTemplateRepository.AnyAsync(x => x.UserId == _userInfo.UserRoleId && x.WW == addOrEditUserWLTemplate.WW && x.WL == addOrEditUserWLTemplate.WL && x.Id != addOrEditUserWLTemplate.Id))
            {
                throw new BusinessValidationFailedException(_localizer["UserWLTS_ContentRepeated"]);
            }

            var entity = await _userWLTemplateRepository.InsertOrUpdateAsync(addOrEditUserWLTemplate, true);
            return ResponseOutput.Ok(entity.Id.ToString());
        }

        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="userWLTemplateId"></param>
        /// <returns></returns>
        [HttpDelete("{userWLTemplateId:guid}")]
        public async Task<IResponseOutput> DeleteUserWLTemplate(Guid userWLTemplateId)
        {
            var success = await _userWLTemplateRepository.DeleteFromQueryAsync(t => t.Id == userWLTemplateId, true);
            return ResponseOutput.Ok();
        }

        /// <summary>
        /// 获取自动切换任务配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> GetAutoCutNextTask(GetAutoCutNextTaskInDto inDto)
        {

            var result = await _readingUserConfigRepository.Where(x => x.UserRoleId == _userInfo.UserRoleId&&x.ImageToolType==inDto.ImageToolType).Select(x => new
            {
                AutoCutNextTask = x.AutoCutNextTask,
                IsDoubleScreen = x.IsDoubleScreen,
            }).FirstOrDefaultAsync();




            if (result == null)
            {
                return new
                {
                    AutoCutNextTask = false,
                    IsDoubleScreen = false,
                };
            }

            return result;
        }


        /// <summary>
        /// 设置自动切换任务
        /// </summary>
        /// <param name="inDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> SetAutoCutNextTask(SetAutoCutNextTaskInDto inDto)
        {

            var userConfig = await _readingUserConfigRepository.FirstOrDefaultNoTrackingAsync(x => x.UserRoleId == _userInfo.UserRoleId && x.ImageToolType == inDto.ImageToolType);

            if (userConfig == null)
            {
                await _readingUserConfigRepository.AddAsync(new ReadingUserConfig
                {
                    UserRoleId = _userInfo.UserRoleId,
                    ImageToolType = inDto.ImageToolType,
                    AutoCutNextTask = inDto.AutoCutNextTask,
                    IsDoubleScreen = inDto.IsDoubleScreen,
                });
            }
            else
            {
                await _readingUserConfigRepository.UpdatePartialFromQueryAsync(x=>x.Id==userConfig.Id, x => new ReadingUserConfig
                {
                    AutoCutNextTask = inDto.AutoCutNextTask,
                    IsDoubleScreen = inDto.IsDoubleScreen,
                });
            }


             await _readingUserConfigRepository.SaveChangesAsync();

            return ResponseOutput.Ok();
        }
    }
}
