package com.hotwj.platform.securityManagement.violationInfo.strategy;

import com.hotwj.platform.flow.domain.SysFlowInstance;
import com.hotwj.platform.flow.domain.dto.FlowNextDto;
import com.hotwj.platform.flow.strategy.IFlowStrategy;
import com.hotwj.platform.securityManagement.violationInfo.domain.HotViolationInfo;
import com.hotwj.platform.securityManagement.violationInfo.mapper.HotViolationInfoMapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.service.DictService;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ViolationHandleStrategy implements IFlowStrategy {

    private static final String FLOW_CODE = "VIOLATION_HANDLE";
    private static final String NODE_PARTY_SIGN = "PARTY_SIGN_NODE";
    private static final String NODE_WAIT_INTERVIEW_INFO = "WAIT_INTERVIEW_INFO_NODE";
    private static final String NODE_INTERVIEWED_SIGN = "INTERVIEW_SIGN_NODE";
    private static final String NODE_AUTO_DONE = "AUTO_DONE_NODE";
    private static final String SYSTEM_USER = "SYSTEM";

    private final HotViolationInfoMapper violationInfoMapper;
    private final DictService dictService;

    @Override
    public String getFlowCode() {
        return FLOW_CODE;
    }

    @Override
    public String getFlowName() {
        return "违规处理";
    }

    @Override
    public FlowNextDto next(SysFlowInstance instance, String currentNodeCode) {
        if (instance == null || StringUtils.isBlank(instance.getBusinessId())) {
            return null;
        }
        HotViolationInfo info = violationInfoMapper.selectById(instance.getBusinessId());
        if (info == null) {
            return null;
        }
        boolean needInterview = Long.valueOf(1L).equals(info.getNeedInterview());

        if (NODE_PARTY_SIGN.equals(currentNodeCode)) {
            if (!needInterview) {
                return null;
            }

            if (!isInterviewInfoComplete(info)) {
                return new FlowNextDto(NODE_WAIT_INTERVIEW_INFO, buildNodeName(info, "等待约谈信息"), SYSTEM_USER);
            }

            if (StringUtils.isBlank(info.getInterviewedSignatureImage())) {
                return new FlowNextDto(NODE_INTERVIEWED_SIGN, buildNodeName(info, "被约谈人签名"), info.getDriverId());
            }

            return new FlowNextDto(NODE_AUTO_DONE, buildNodeName(info, "已处理"), SYSTEM_USER);
        }

        if (NODE_WAIT_INTERVIEW_INFO.equals(currentNodeCode)) {
            if (isInterviewInfoComplete(info) && StringUtils.isBlank(info.getInterviewedSignatureImage())) {
                return new FlowNextDto(NODE_INTERVIEWED_SIGN, buildNodeName(info, "被约谈人签名"), info.getDriverId());
            }
            return new FlowNextDto(NODE_AUTO_DONE, buildNodeName(info, "已处理"), info.getDriverId());
        }

        if (NODE_INTERVIEWED_SIGN.equals(currentNodeCode)) {
            return null;
        }

        if (NODE_AUTO_DONE.equals(currentNodeCode)) {
            return null;
        }

        return new FlowNextDto(NODE_AUTO_DONE, buildNodeName(info, "已处理"), info.getDriverId());
    }

    @Override
    public FlowNextDto getInitialNode(String businessId) {
        HotViolationInfo info = null;
        if (StringUtils.isNotBlank(businessId)) {
            info = violationInfoMapper.selectById(businessId);
        }
        if (info == null) {
            return new FlowNextDto(NODE_PARTY_SIGN, "违规通知：车辆 违规，请及时处理。（当事人签名）", null);
        }

        return new FlowNextDto(NODE_PARTY_SIGN, buildNodeName(info, "当事人签名"), info.getDriverId());
    }

    private boolean isInterviewInfoComplete(HotViolationInfo info) {
        return info != null
                && info.getInterviewTime() != null
                && StringUtils.isNotBlank(info.getInterviewPlace())
                && StringUtils.isNotBlank(info.getInterviewReason())
                && StringUtils.isNotBlank(info.getInterviewContent())
                && StringUtils.isNotBlank(info.getInterviewerSignatureImage());
    }

    private String buildNodeName(HotViolationInfo info, String statusText) {
        String plate = info != null && StringUtils.isNotBlank(info.getPlateNumber()) ? info.getPlateNumber() : "车辆";
        String typeLabel = "违规";
        if (info != null && info.getViolationType() != null) {
            String label = dictService.getDictLabel("hot_violation_type", String.valueOf(info.getViolationType()));
            if (StringUtils.isNotBlank(label)) {
                typeLabel = label;
            }
        }
        String status = StringUtils.isNotBlank(statusText) ? statusText : "待处理";
        return "违规通知：" + plate + " " + typeLabel + "，请及时处理。（" + status + "）";
    }
}
