package cn.stylefeng.workflow.design.modular.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.FormModelServiceApi;
import cn.stylefeng.team.form.api.entity.FormItem;
import cn.stylefeng.team.form.api.pojo.FormDesignVo;
import cn.stylefeng.workflow.api.pojo.config.node.form.FieldPermission;
import cn.stylefeng.workflow.api.pojo.config.node.form.FormPermission;
import cn.stylefeng.workflow.api.timer.BaseWorkflowTimerAction;
import cn.stylefeng.workflow.design.modular.pojo.request.FormPermissionRequest;
import cn.stylefeng.workflow.design.modular.service.DesignConfigService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

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

@Service
public class DesignConfigServiceImpl implements DesignConfigService {

    @Resource
    private FormModelServiceApi formModelServiceApi;

    @Override
    public List<FormPermission> getFormPermissionList(FormPermissionRequest formPermissionRequest) {

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

        List<FormDesignVo> fromDesignList = formModelServiceApi.getFromDesignList(formPermissionRequest.getFormDefinitionIdList());

        for (FormDesignVo formDesignVo : fromDesignList) {

            FormPermission formPermission = new FormPermission();

            formPermission.setFormName(formDesignVo.getFormModelName());

            formPermission.setFormDefinitionId(formDesignVo.getFormDefinitionId());

            formPermission.setFieldPermissionList(parseToFieldPermission(formDesignVo.getFormItemList()));

            result.add(formPermission);
        }

        return result;
    }

    @Override
    public List<String> getActionClasses() {

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

    private List<FieldPermission> parseToFieldPermission(List<FormItem> formItemList) {

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

        for (FormItem formItem : formItemList) {

            FieldPermission fieldPermission = createFieldPermission(formItem);


            List<FormItem> formItemChildren = formItem.getChildren();
            if (ObjectUtil.isNotEmpty(formItemChildren)) {
                List<FieldPermission> tablePermissions = new ArrayList<>();
                for (FormItem tableColumn : formItemChildren) {
                    FieldPermission tableColumnPermission = createFieldPermission(tableColumn);
                    tablePermissions.add(tableColumnPermission);
                }
                fieldPermission.setSubTableFieldPermissionList(tablePermissions);
            }

            result.add(fieldPermission);
        }

        return result;
    }

    private FieldPermission createFieldPermission(FormItem formItem) {

        FieldPermission fieldPermission = new FieldPermission();

        fieldPermission.setFieldCode(formItem.getFieldCode());

        fieldPermission.setFieldName(formItem.getItemLabelName());

        fieldPermission.setHide(false);
        fieldPermission.setEdit(false);
        fieldPermission.setRequire(false);

        return fieldPermission;
    }

}
