using IRaCIS.Core.Domain.Models;
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
using Panda.DynamicWebApi.Attributes;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infra.EFCore.Common;
using Microsoft.Extensions.Caching.Memory;
using IRaCIS.Core.Application.Service.Reading.Dto;
using IRaCIS.Core.Infrastructure;

namespace IRaCIS.Core.Application.Service
{
	/// <summary>
	/// 阅片验证
	/// </summary>
	[NonDynamicWebApi]
    public partial class ReadingCalculateService : BaseService, IReadingCalculateService
	{

		/// <summary>
		/// 标准验证
		/// </summary>
		/// <returns></returns>
		public async Task CriterionVerify(Guid visitTaskId)
		{
			var taskinfo = await _visitTaskRepository.Where(x => x.Id == visitTaskId).Include(x=>x.TrialReadingCriterion).FirstNotNullAsync();
			ReadingCalculateDto readingData = await GetReadingCalculateDto(visitTaskId);

			

		}


		/// <summary>
		/// RECIST 1.1验证
		/// </summary>
		/// <param name="inDto"></param>
		/// <returns></returns>
		public async Task RECIST1Point1Verify(ReadingCalculateDto inDto)
		{
			var tableAnswerList = inDto.QuestionInfo.SelectMany(x => x.TableRowInfoList).SelectMany(x=>x.TableQuestionList).ToList();

			List<string> errorList = new List<string>();

			// 判断是否为基线
			if (inDto.IsBaseLine)
			{
				// 不能有状态为 无法评估 消失 太小 显著增大 疑似的病灶
				List<string> targetStates = new List<string>() {
				TargetState.TooSmall.GetEnumInt(),
				TargetState.Loss.GetEnumInt(),
				TargetState.UnableEvaluate.GetEnumInt(),
				};

				if (tableAnswerList.Any(x => x.QuestionMark == QuestionMark.State && targetStates.Contains(x.Answer)))
				{
					errorList.Add("基线不能有状态为 无法评估 消失 太小 显著增大 疑似的病灶！");
					
				}

				if (inDto.QuestionInfo.Where(x => x.LesionType == LesionType.NewLesions).SelectMany(x => x.TableRowInfoList).Count() > 0)
				{
					errorList.Add($"基线不能有新病灶！");
				}

			}
			else 
			{
				
			}

			if (errorList.Count > 0)
			{
				throw new BusinessValidationFailedException(string.Join(',',errorList));
			}
		}


	}
}
