package cn.stylefeng.team.project.modular.filing.service.impl;

import cn.stylefeng.roses.kernel.file.api.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.api.pojo.request.SysFileInfoRequest;
import cn.stylefeng.roses.kernel.file.modular.entity.SysFileInfo;
import cn.stylefeng.roses.kernel.file.modular.factory.FileInfoFactory;
import cn.stylefeng.roses.kernel.file.modular.service.SysFileInfoService;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.team.project.modular.filing.exceptions.BusinessExcelExceptionEnum;
import cn.stylefeng.team.project.modular.filing.factory.ByteFileInfoFactory;
import cn.stylefeng.team.project.modular.filing.pojo.aidesign.aiparse.StopAiParseRequest;
import cn.stylefeng.team.project.modular.filing.pojo.aidesign.convertpng.ParseExcelResponse;
import cn.stylefeng.team.project.modular.filing.service.AIDesignService;
import cn.stylefeng.team.project.modular.filing.service.BusinessAiParseLogService;
import cn.stylefeng.team.project.modular.filing.util.ExcelToImageUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
@Slf4j
public class AIDesignServiceImpl implements AIDesignService {

    @Resource
    private SysFileInfoService sysFileInfoService;

    @Resource
    private FileOperatorApi fileOperatorApi;

    @Resource
    private BusinessAiParseLogService businessAiParseLogService;

    @Override
    public ParseExcelResponse parseExcelToPng(MultipartFile file) {

        ParseExcelResponse parseExcelResponse = new ParseExcelResponse();

        SysFileInfo excelFile = createExcelFile(file);
        parseExcelResponse.setExcelFileId(excelFile.getFileId());

        SysFileInfo pngFile = createPngFile(file, excelFile);
        parseExcelResponse.setPngFileId(pngFile.getFileId());

        return parseExcelResponse;
    }

    @Override
    public void stopAiParse(StopAiParseRequest stopAiParseRequest) {
        businessAiParseLogService.updateStopLog(stopAiParseRequest.getAiParseLogId());
    }

    private SysFileInfo createExcelFile(MultipartFile file) {

        SysFileInfoRequest sysFileInfoRequest = new SysFileInfoRequest();
        sysFileInfoRequest.setSecretFlag(YesOrNotEnum.Y.getCode());
        SysFileInfo excelFileInfo = FileInfoFactory.createSysFileInfo(file, sysFileInfoRequest);

        this.sysFileInfoService.storageFile(file, excelFileInfo);

        this.sysFileInfoService.save(excelFileInfo);

        return excelFileInfo;
    }

    private SysFileInfo createPngFile(MultipartFile file, SysFileInfo excelFileInfo) {

        byte[] pngFileBytes = null;
        try {
            pngFileBytes = ExcelToImageUtil.parseExcelFileToPng(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException(BusinessExcelExceptionEnum.EXCEL_EXIST_ERROR);
        }

        SysFileInfo pngFileInfo = ByteFileInfoFactory.createPngFileInfo(pngFileBytes, excelFileInfo);

        this.fileOperatorApi.storageFile(pngFileInfo.getFileBucket(), pngFileInfo.getFileObjectName(), pngFileBytes);

        this.sysFileInfoService.save(pngFileInfo);

        return pngFileInfo;
    }

}
