package cn.stylefeng.workflow.instance.core.condition;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.sys.api.OrgApproverServiceApi;
import cn.stylefeng.roses.kernel.sys.api.OrganizationServiceApi;
import cn.stylefeng.roses.kernel.sys.api.enums.org.DetectModeEnum;
import cn.stylefeng.roses.kernel.sys.api.pojo.org.CompanyDeptDTO;
import cn.stylefeng.workflow.api.enums.ApproverTypeEnum;
import cn.stylefeng.workflow.api.enums.SelectScopeEnum;
import cn.stylefeng.workflow.api.pojo.InstanceContext;
import cn.stylefeng.workflow.api.pojo.config.node.approver.ApproverConfig;
import cn.stylefeng.workflow.api.pojo.config.node.approver.ApproverSummary;
import cn.stylefeng.workflow.api.pojo.config.node.approver.detail.NodeApproverPolicy;
import cn.stylefeng.workflow.instance.core.condition.base.BaseApproverRule;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class DeptPolicyApproverRule extends BaseApproverRule {

    @Override
    public boolean approverEvaluate(InstanceContext instanceContext, ApproverConfig approverConfig) {

        ApproverSummary mainApprover = approverConfig.getMainApprover();
        if (ObjectUtil.isEmpty(mainApprover)) {
            return false;
        }

        if (!ApproverTypeEnum.ORG_SELECT.getValue().equals(mainApprover.getApproverType())) {
            return false;
        }

        List<NodeApproverPolicy> nodeApproverPolicyList = mainApprover.getNodeApproverPolicyList();
        if (ObjectUtil.isEmpty(nodeApproverPolicyList)) {
            return false;
        }

        return true;
    }

    @Override
    public void approverExecute(InstanceContext instanceContext, ApproverConfig approverConfig) {

        List<NodeApproverPolicy> nodeApproverPolicyList = approverConfig.getMainApprover().getNodeApproverPolicyList();

        Set<Long> userIdList = new LinkedHashSet<>();

        for (NodeApproverPolicy nodeApproverPolicy : nodeApproverPolicyList) {
            List<Long> orgApproverList = getOrgApproverList(nodeApproverPolicy, instanceContext);
            if (ObjectUtil.isNotEmpty(orgApproverList)) {
                userIdList.addAll(orgApproverList);
            }
        }

        setResult(new ArrayList<>(userIdList));
    }

    @Override
    public String getBaseRuleName() {
        return DeptPolicyApproverRule.class.getName();
    }

    @Override
    public int getBaseRulePriority() {
        return 200;
    }

    private List<Long> getOrgApproverList(NodeApproverPolicy nodeApproverPolicy, InstanceContext instanceContext) {

        Long orgId = null;

        if (SelectScopeEnum.DEPT.getValue().equals(nodeApproverPolicy.getSelectScope())) {
            orgId = nodeApproverPolicy.getOrgId();
        }

        else if (SelectScopeEnum.SUBMIT_DEPT.getValue().equals(nodeApproverPolicy.getSelectScope())) {

            Long applicantOrgId = instanceContext.getRuntimeInstance().getApplicantOrgId();
            if (ObjectUtil.isNotEmpty(applicantOrgId)) {

                OrganizationServiceApi organizationServiceApi = SpringUtil.getBean(OrganizationServiceApi.class);
                CompanyDeptDTO orgCompanyInfo = organizationServiceApi.getCompanyDeptInfo(applicantOrgId);
                orgId = orgCompanyInfo.getDeptId();
            }
        }

        else if (SelectScopeEnum.SUBMIT_COMPANY.getValue().equals(nodeApproverPolicy.getSelectScope())) {

            Long applicantOrgId = instanceContext.getRuntimeInstance().getApplicantOrgId();
            if (ObjectUtil.isNotEmpty(applicantOrgId)) {

                OrganizationServiceApi organizationServiceApi = SpringUtil.getBean(OrganizationServiceApi.class);
                CompanyDeptDTO orgCompanyInfo = organizationServiceApi.getOrgCompanyInfo(applicantOrgId);
                orgId = orgCompanyInfo.getCompanyId();
            }
        }

        if (ObjectUtil.isEmpty(orgId)) {
            return new ArrayList<>();
        }

        OrgApproverServiceApi orgApproverServiceApi = SpringUtil.getBean(OrgApproverServiceApi.class);
        List<Long> userIdList = orgApproverServiceApi.getDeptOrgApprover(orgId, Convert.toInt(nodeApproverPolicy.getApproveGroupType()), 0,
                DetectModeEnum.TO_TOP);

        if (ObjectUtil.isNotEmpty(userIdList)) {
            return userIdList;
        }

        return new ArrayList<>();
    }

}
