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.domian.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.query.SoftwareCopyrightQueryCondition;
import cn.com.poc.ai_software_copyright.query.SoftwareCopyrightQueryItem;
import cn.com.poc.ai_software_copyright.rest.BizSoftwareCopyrightRest;
import cn.com.poc.ai_software_copyright.service.BizSoftwareCopyrightService;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.JsonUtils;
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);
        BizSoftwareCopyrightEntity entity = BizSoftwareCopyrightConvert.dtoToEntity(dto);
        entity.setGeneratedStatus(AiSoftWareCopyRightEnum.GENERATED_STATUS.Daft.name());
        return BizSoftwareCopyrightConvert.entityToDto(bizSoftwareCopyrightService.save(entity));
    }

    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) {

        SoftwareCopyrightQueryCondition condition = new SoftwareCopyrightQueryCondition();
        condition.setGeneratedStatus(generatedStatus);
        condition.setQuery(query);
        List<SoftwareCopyrightQueryItem> softwareCopyrightQueryItems =
                bizSoftwareCopyrightService.softwareCopyrightQuery(condition, pagingInfo);
        return softwareCopyrightQueryItems.stream().map(BizSoftwareCopyrightConvert::softwareCopyrightQueryItemToDto)
                .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);
    }

    @Resource
    private RedisService redisService;

    @Override
    public void callbackGeneratedBaseDoc(CallbackDto callbackDto)  {
        String taskId = callbackDto.getTaskId();
        if (redisService.hasKey(taskId)) {
            redisService.set(taskId, JsonUtils.serialize(callbackDto));
        }
    }
}