package cn.stylefeng.team.form.modular.manage.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.team.form.api.entity.FormDefinition;
import cn.stylefeng.team.form.api.entity.FormItem;
import cn.stylefeng.team.form.api.entity.FormModel;
import cn.stylefeng.team.form.api.pojo.FormDesignVo;
import cn.stylefeng.team.form.modular.manage.request.FormConfigRequest;
import cn.stylefeng.team.form.modular.manage.service.FormDefinitionService;
import cn.stylefeng.team.form.modular.manage.service.FormItemService;
import cn.stylefeng.team.form.modular.manage.service.FormModelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

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

@Service
public class FormItemSelectServiceImpl implements FormItemService {

    @Resource
    private FormDefinitionService formDefinitionService;

    @Resource
    private FormModelService formModelService;

    @Override
    public List<FormItem> getItemSummaryList(List<Long> itemIds) {
        return null;
    }

    @Override
    public List<FormDesignVo> getBusinessFromDesignList(Long businessId) {

        List<FormDesignVo> result = new ArrayList<>();

        LambdaQueryWrapper<FormModel> formModelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        formModelLambdaQueryWrapper.eq(FormModel::getBusinessId, businessId);
        formModelLambdaQueryWrapper.select(FormModel::getCurrentDefinitionId, FormModel::getFormModelName);
        List<FormModel> list = this.formModelService.list(formModelLambdaQueryWrapper);
        if (ObjectUtil.isEmpty(list)) {
            return new ArrayList<>();
        }

        List<Long> formDefinitionIdList = list.stream().map(FormModel::getCurrentDefinitionId).collect(Collectors.toList());

        LambdaQueryWrapper<FormDefinition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(FormDefinition::getFormDefinitionId, formDefinitionIdList);
        queryWrapper.select(FormDefinition::getFormConfig, FormDefinition::getFormDefinitionId);
        List<FormDefinition> definitionList = this.formDefinitionService.list(queryWrapper);

        for (FormModel formModel : list) {
            for (FormDefinition formDefinition : definitionList) {
                if (formModel.getCurrentDefinitionId().equals(formDefinition.getFormDefinitionId())) {
                    FormDesignVo formConfig = formDefinition.getFormConfig();
                    String modelName = formModel.getFormModelName();

                    if (ObjectUtil.isEmpty(formConfig) || StrUtil.isBlank(modelName)) {
                        continue;
                    } else {
                        formConfig.setFormDefinitionId(formDefinition.getFormDefinitionId());
                        formConfig.setFormModelName(modelName);
                        result.add(formConfig);
                    }
                }
            }
        }

        return result;
    }

    @Override
    public FormDesignVo getFormConfig(FormConfigRequest formItemRequest) {

        Long formDefinitionId = formItemRequest.getFormDefinitionId();
        FormDefinition detail = formDefinitionService.detail(formDefinitionId);

        if (formItemRequest.getMobileFlag() != null && formItemRequest.getMobileFlag()) {
            FormDesignVo mobileFormConfig = detail.getMobileFormConfig();
            if (mobileFormConfig == null) {
                return new FormDesignVo();
            }
            return mobileFormConfig;
        } else {
            FormDesignVo pcFormConfig = detail.getFormConfig();
            if (pcFormConfig == null) {
                return new FormDesignVo();
            }
            return pcFormConfig;
        }
    }

    @Override
    public List<FormItem> getFormFieldList(FormConfigRequest formItemRequest) {
        FormDesignVo formConfig = this.getFormConfig(formItemRequest);
        List<FormItem> formItemList = formConfig.getFormItemList();
        if (ObjectUtil.isEmpty(formItemList)) {
            return new ArrayList<>();
        }

        ArrayList<FormItem> finalResult = new ArrayList<>();
        for (FormItem formItem : formItemList) {
            FormItem temp = new FormItem();
            temp.setFieldCode(formItem.getFieldCode());
            temp.setComponentCode(formItem.getComponentCode());
            temp.setItemLabelName(formItem.getItemLabelName());
            finalResult.add(temp);
        }

        return finalResult;
    }

    @Override
    public void saveOrUpdateFormConfig(FormConfigRequest formItemRequest) {

        Long formDefinitionId = formItemRequest.getFormDefinitionId();
        FormDefinition detail = formDefinitionService.detail(formDefinitionId);

        detail.setFormConfig(formItemRequest.getFormConfig());

        detail.setMobileFormConfig(formItemRequest.getMobileFormConfig());

        this.formDefinitionService.updateById(detail);
    }

}