package cn.com.poc.ai_software_copyright.rest.impl;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import cn.com.gsst.dify_client.exception.DifyApiException;
import cn.com.poc.ai_software_copyright.aggregate.SoftwareCopyRightService;
import cn.com.poc.ai_software_copyright.contant.AiSoftWareCopyRightEnum;
import cn.com.poc.ai_software_copyright.convert.BizSoftwareCopyrightConvert;
import cn.com.poc.ai_software_copyright.convert.BizSoftwareCopyrightDocRecordConvert;
import cn.com.poc.ai_software_copyright.domain.TechnicalContent;
import cn.com.poc.ai_software_copyright.dto.CallbackDto;
import cn.com.poc.ai_software_copyright.dto.SoftwareCopyRightAssistantDto;
import cn.com.poc.ai_software_copyright.dto.SoftwareCopyRightDto;
import cn.com.poc.ai_software_copyright.dto.SoftwareCopyRightFilesDto;
import cn.com.poc.ai_software_copyright.entity.BizSoftwareCopyrightEntity;
import cn.com.poc.ai_software_copyright.entity.CallbackEntity;
import cn.com.poc.ai_software_copyright.rest.BizSoftwareCopyrightRest;
import cn.com.poc.ai_software_copyright.service.BizSoftwareCopyrightService;
import cn.com.yict.framemax.data.model.PagingInfo;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

@Component
public class BizSoftwareCopyrightRestImpl implements BizSoftwareCopyrightRest {

    @Resource
    private SoftwareCopyRightService softwareCopyRightService;

    @Resource
    private BizSoftwareCopyrightService bizSoftwareCopyrightService;


    public SoftwareCopyRightDto getById(java.lang.Long id) throws Exception {
        Assert.notNull(id);
        return BizSoftwareCopyrightConvert.entityToDto(bizSoftwareCopyrightService.get(id));
    }

    public SoftwareCopyRightDto save(SoftwareCopyRightDto dto) throws Exception {
        Assert.notNull(dto);
        Assert.notNull(dto.getBaseInfo(), "基础信息不能为空");
        Assert.notNull(dto.getTechnicalContent(), "技术内容不能为空");
        Assert.notNull(dto.getGeneratedDocType(), "文档类型不能为空");
        for (String docType : dto.getGeneratedDocType()) {
            Assert.isTrue(AiSoftWareCopyRightEnum.DOC_TYPE.isValid(docType), "文档类型错误：" + docType + "类型不存在");
        }
        return BizSoftwareCopyrightConvert.entityToDto(
                softwareCopyRightService.save(
                        dto.getBaseInfo(),
                        dto.getTechnicalContent(),
                        dto.getReferenceDocumentFileUrl(),
                        dto.getGeneratedDocType())
        );
    }

    public void deletedById(java.lang.Long id) throws Exception {
        Assert.notNull(id);
        bizSoftwareCopyrightService.deletedById(id);
    }

    @Override
    public List<SoftwareCopyRightDto> querySoftwareCopyRight(String query, String generatedStatus,
                                                             PagingInfo pagingInfo) {
        List<BizSoftwareCopyrightEntity> bizSoftwareCopyrightEntities =
                softwareCopyRightService.querySoftwareCopyRight(query, generatedStatus, pagingInfo);
        return bizSoftwareCopyrightEntities.stream().map(BizSoftwareCopyrightConvert::entityToDto)
                .collect(Collectors.toList());
    }

    @Override
    public void softwareCopyRightAssistant(SoftwareCopyRightAssistantDto dto)
            throws DifyApiException, IOException, InterruptedException {
        softwareCopyRightService.softwareCopyRightAssistant(
                dto.getType(),
                dto.getFileUrls(),
                dto.getQuestion(),
                dto.getData()
        );
    }

    @Override
    public SoftwareCopyRightDto createTechnicalContent(SoftwareCopyRightDto dto) throws IOException {
        Assert.notNull(dto.getBaseInfo());
        TechnicalContent technicalContent =
                softwareCopyRightService.createTechnicalContent(dto.getReferenceDocumentFileUrl(), dto.getBaseInfo());
        SoftwareCopyRightDto result = new SoftwareCopyRightDto();
        result.setTechnicalContent(technicalContent);
        return result;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        Assert.notNull(file);
        return softwareCopyRightService.upload(file);
    }

    @Override
    public List<SoftwareCopyRightFilesDto> listFile(Long id) throws Exception {
        return BizSoftwareCopyrightDocRecordConvert.entityToDto(softwareCopyRightService.listFile(id));
    }

    @Override
    public String getDownloadUrl(Long id) throws Exception {
        return softwareCopyRightService.getDownloadUrl(id);
    }


    @Override
    public void callbackGeneratedBaseDoc(String taskId,
                                         String type,
                                         CallbackDto callbackDto) {
        CallbackEntity callbackEntity = new CallbackEntity();
        if (callbackDto != null && callbackDto.getResult() != null) {
            callbackEntity.setResult(callbackDto.getResult());
        }
        softwareCopyRightService.callbackGeneratedBaseDoc(taskId, type, callbackEntity);
    }
}