using AutoMapper;
using Newtonsoft.Json;
using Ropin.Inspection.Common.Accessor.Interface;
using Ropin.Inspection.Common.Helper;
using Ropin.Inspection.Model;
using Ropin.Inspection.Model.Entities;
using Ropin.Inspection.Model.SearchModel;
using Ropin.Inspection.Model.ViewModel;
using Ropin.Inspection.Repository.Interface;
using Ropin.Inspection.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ropin.Inspection.Service
{
    public class ReportService : IReportService
    {
        private readonly IReportRepository _repository;
        private readonly IMapper _mapper;
        private readonly IClaimsAccessor _claims;
        public ReportService(IClaimsAccessor claims, IReportRepository repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
            _claims = claims;
        }

        public async Task<IEnumerable<ReportViewModel>> GetWithoutReportData(ReportSearchModel searchModel)
        {
            var result = await _repository.GetWithoutReportData(searchModel);
            return result;
        }


        public async Task CreateOneAsync(ReportViewModel viewModel)
        {
            var content = _mapper.Map<TISP_Report>(viewModel);
            content.D_CreateTime = DateTime.Now;
            content.C_Status = "1";
            _repository.Create(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("创建失败");
            }
        }

        public  async Task DeleteAsync(Guid id)
        {
            var content = await _repository.GetByIdAsync(id);
            if (content == null)
            {
                throw new Exception("没有此报表");
            }
            _repository.Delete(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("删除失败");
            }
        }

        public  async Task<ReportViewModel> GetByIdAsync(Guid id)
        {
            var content = await _repository.GetByIdAsync(id);
            if (content == null)
            {
                throw new Exception("没有此报表");
            }
            var contentDto = _mapper.Map<ReportViewModel>(content);
            return contentDto;
        }

        public async Task<object> GetReportDataByIdAsync(Guid id)
        {
            var content = await _repository.GetByIdAsync(id);
            if (content == null)
            {
                throw new Exception("没有此报表");
            }
            object oRecord;
            switch (content.I_Type)
            {
                case 2:
                    oRecord = JsonConvert.DeserializeObject<HiddenDangerRectificationAcceptanceFormViewModel>(content.C_Data.Trim()); // HiddenDangerRectificationAcceptanceFormViewModel firePreventionWeekRecord 
                    break;
                case 1:
                    oRecord = JsonConvert.DeserializeObject<IEnumerable<FirePreventionWeekRecordViewModel>>(content.C_Data.Trim());
                    break;
                case 3:
                    oRecord = JsonConvert.DeserializeObject<IEnumerable<FireFightingFacilitiesWeekRecordViewModel>>(content.C_Data.Trim());
                    break;
                case 4:
                    oRecord = JsonConvert.DeserializeObject<IEnumerable<FireInspectionRecordViewModel>>(content.C_Data.Trim());
                    break;
                default:
                    oRecord = null;
                    break;

            }
            //IEnumerable<FirePreventionWeekRecordViewModel> firePreventionWeekRecord = JsonConvert.DeserializeObject<IEnumerable<FirePreventionWeekRecordViewModel>>(content.C_Data.Trim());
            //if (string.IsNullOrEmpty(content.C_Data))
            //{
             //   firePreventionWeekRecord = JsonHelper.ParseFormByJson<IEnumerable<FirePreventionWeekRecordViewModel>>(content.C_Data);
            //}
            return oRecord;
        }
        public async Task<HiddenDangerRectificationAcceptanceFormViewModel> GetHiddenDangerRectificationAcceptanceFormReportDataByIdAsync(Guid id)
        {
            var content = await _repository.GetByIdAsync(id);
            if (content == null)
            {
                throw new Exception("没有此报表");
            }
            HiddenDangerRectificationAcceptanceFormViewModel firePreventionWeekRecord = JsonConvert.DeserializeObject<HiddenDangerRectificationAcceptanceFormViewModel>(content.C_Data.Trim());
            return firePreventionWeekRecord;
        }

        public Task<bool> IsExistAsync(Guid id)
        {
            throw new NotImplementedException();
        }

        public Task<int> UpdateOneAsync(ReportViewModel viewModel, params string[] fields)
        {
            throw new NotImplementedException();
        }
        public async Task UpdateAsync(Guid id, ReportUpdateViewModel updateModel)
        {
            var content = await _repository.GetByIdAsync(id);
            if (content == null)
            {
                throw new Exception("没有此报表");
            }
            _mapper.Map(updateModel, content, typeof(ReportUpdateViewModel), typeof(TISP_Report));
            _repository.Update(content);
            var result = await _repository.SaveAsync();
            if (!result)
            {
                throw new Exception("更新失败");
            }
        }
    }
}