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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.team.form.api.FormItemServiceApi;
import cn.stylefeng.team.form.api.pojo.FormDesignVo;
import cn.stylefeng.team.project.api.ViewDataAction;
import cn.stylefeng.team.project.api.entity.Business;
import cn.stylefeng.team.project.api.pojo.BusinessSearchRequest;
import cn.stylefeng.team.project.modular.business.service.BusinessService;
import cn.stylefeng.team.project.modular.businessrender.pojo.response.BusinessRenderConfigVo;
import cn.stylefeng.team.project.modular.businessrender.service.BusinessRenderConfigService;
import cn.stylefeng.team.view.api.ViewApi;
import cn.stylefeng.team.view.api.pojo.ViewConfig;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

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

@Service
public class BusinessRenderConfigServiceImpl implements BusinessRenderConfigService {

    @Resource
    private ViewApi viewApi;

    @Resource
    private BusinessService businessService;

    @Resource
    private FormItemServiceApi formItemServiceApi;


    @Override
    public BusinessRenderConfigVo getConfig(BusinessSearchRequest businessRequest) {

        LambdaQueryWrapper<Business> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Business::getBusinessId, businessRequest.getBusinessId());
        queryWrapper.select(Business::getBusinessName);
        Business one = businessService.getOne(queryWrapper, false);
        if (one == null) {
            return new BusinessRenderConfigVo();
        }

        ViewConfig viewConfig = null;
        if (businessRequest.getViewId() != null) {
            viewConfig = viewApi.getViewConfig(businessRequest.getViewId(), true);
        } else {
            viewConfig = viewApi.getViewConfigByBusinessId(businessRequest.getBusinessId(), true);
        }

        viewConfig.setSqlTemplate(null);

        List<FormDesignVo> businessFromDesignList = formItemServiceApi.getBusinessFromDesignList(businessRequest.getBusinessId());

        BusinessRenderConfigVo businessRenderConfigVo = new BusinessRenderConfigVo();
        businessRenderConfigVo.setViewConfig(viewConfig);
        businessRenderConfigVo.setBusinessName(one.getBusinessName());
        businessRenderConfigVo.setFormDesignConfigList(businessFromDesignList);
        return businessRenderConfigVo;
    }

    @Override
    public List<String> getCustomRenderApiList() {

        Map<String, ViewDataAction> viewDataActionMap = SpringUtil.getBeansOfType(ViewDataAction.class);
        if (ObjectUtil.isNotEmpty(viewDataActionMap)) {
            Collection<ViewDataAction> values = viewDataActionMap.values();
            return values.stream().map(i -> i.getClass().getName()).collect(Collectors.toList());
        } else {
            return CollectionUtil.newArrayList();
        }
    }

}
