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

import cn.hutool.core.util.RandomUtil;
import cn.stylefeng.roses.kernel.pinyin.api.PinYinApi;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.team.form.api.FormModelServiceApi;
import cn.stylefeng.team.form.api.entity.FormDefinition;
import cn.stylefeng.team.form.api.pojo.FormDesignVo;
import cn.stylefeng.team.meta.api.MetaFieldServiceApi;
import cn.stylefeng.team.meta.api.MetaModelServiceApi;
import cn.stylefeng.team.meta.api.entity.MetaField;
import cn.stylefeng.team.meta.api.entity.MetaModel;
import cn.stylefeng.team.project.api.entity.Business;
import cn.stylefeng.team.project.modular.business.enums.BusinessExceptionEnum;
import cn.stylefeng.team.project.modular.business.service.BusinessService;
import cn.stylefeng.team.project.modular.filing.constants.BevProdConstants;
import cn.stylefeng.team.project.modular.filing.entity.BevProdFilingList;
import cn.stylefeng.team.project.modular.filing.factory.FormDefinitionGenerateFactory;
import cn.stylefeng.team.project.modular.filing.pojo.generateform.BaseMetaInfo;
import cn.stylefeng.team.project.modular.filing.pojo.generateform.GenerateFormRequest;
import cn.stylefeng.team.project.modular.filing.service.BevProdFilingListService;
import cn.stylefeng.team.project.modular.filing.service.FormAutoGenerateService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FormAutoGenerateServiceImpl implements FormAutoGenerateService {

    @Resource
    private MetaModelServiceApi metaModelServiceApi;

    @Resource
    private MetaFieldServiceApi metaFieldServiceApi;

    @Resource
    private BevProdFilingListService bevProdFilingListService;

    @Resource
    private FormModelServiceApi formModelServiceApi;

    @Resource
    private BusinessService businessService;

    @Resource
    private PinYinApi pinYinApi;

    @Override
    public void autoGenerateForm(GenerateFormRequest generateFormRequest) {
        BaseMetaInfo baseMetaInfo = this.createBaseMetaInfo(generateFormRequest);
        this.autoGenerateForm(baseMetaInfo);
    }

    @Override
    public FormDesignVo autoGenerateForm(BaseMetaInfo baseMetaInfo) {

        Business business = businessService.getById(baseMetaInfo.getBusinessId());

        FormDefinition formDefinition = formModelServiceApi.createFormModelAndDefinition(
                business.getBusinessCategoryId(),
                business.getBusinessId(),
                baseMetaInfo.getSubTitle(), BevProdConstants.AUTO_GEN_FORM_PREFIX + pinYinApi.parsePinyinString(baseMetaInfo.getSubTitle()));

        FormDesignVo formDesignVo = FormDefinitionGenerateFactory.generateFormDetail(baseMetaInfo, formDefinition);

        formModelServiceApi.updateFormDefinition(formDefinition);

        return formDesignVo;
    }

    private BaseMetaInfo createBaseMetaInfo(GenerateFormRequest generateFormRequest) {
        BaseMetaInfo baseMetaInfo = new BaseMetaInfo();

        baseMetaInfo.setBusinessId(generateFormRequest.getBusinessId());

        Business business = businessService.getById(generateFormRequest.getBusinessId());
        if (business == null) {
            throw new ServiceException(BusinessExceptionEnum.BUSINESS_NOT_EXISTED);
        }

        Long businessId = generateFormRequest.getBusinessId();
        List<MetaModel> metaModelList = metaModelServiceApi.getBusinessMetaModelList(businessId, null);
        baseMetaInfo.setMetaModelList(metaModelList);

        List<Long> definitionIdList = metaModelList.stream().map(MetaModel::getCurrentDefinitionId).collect(Collectors.toList());

        List<MetaField> metaFieldList = metaFieldServiceApi.quickGetFieldList(definitionIdList);
        baseMetaInfo.setMetaFieldList(metaFieldList);

        BevProdFilingList businessFilingInfo = bevProdFilingListService.getBusinessFilingInfo(businessId);
        if (businessFilingInfo != null) {
            baseMetaInfo.setMainTitle(businessFilingInfo.getMainTitle());
            baseMetaInfo.setSubTitle(businessFilingInfo.getSubTitle());
            baseMetaInfo.setFilingNo(businessFilingInfo.getFilingNo());
            baseMetaInfo.setFilingVersion(businessFilingInfo.getFilingVersion());
        } else {
            baseMetaInfo.setMainTitle(business.getBusinessName());
            baseMetaInfo.setSubTitle(business.getBusinessName());
            baseMetaInfo.setFilingNo(RandomUtil.randomString(10));
            baseMetaInfo.setFilingVersion("1.0");
        }

        return baseMetaInfo;
    }

}
