package net.northking.cctp.scriptcase.api.scenescript.service;

import com.alibaba.fastjson.JSONObject;
import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.scriptcase.api.scenescript.bean.SceneScript;
import net.northking.cctp.scriptcase.api.scenescript.service.verfiy.*;
import net.northking.cctp.scriptcase.dto.sceneScript.AtuSaveSceneScriptDto;
import net.northking.cctp.scriptcase.dto.sceneScript.SceneNameVerifyDto;
import net.northking.cctp.scriptcase.dto.sceneScript.UserCaseDataVerifyDto;
import net.northking.cctp.scriptcase.enums.ErrorMessage;
import net.northking.cctp.scriptcase.enums.ScriptCaseAppError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SceneDataVerifier implements IDataVerify<AtuSaveSceneScriptDto> {
    private final static Logger logger = LoggerFactory.getLogger(SceneDataVerifier.class);

    @Autowired
    private SceneNameVerify sceneNameVerify;

    @Autowired
    private SceneNodeNameVerify sceneNodeNameVerify;

    @Autowired
    private SceneRelationVerify sceneRelationVerify;

    @Autowired
    private VariableVerify variableVerify;

    @Autowired
    private UserCaseDataVerify userCaseDataVerify;

    @Override
    public boolean verify(AtuSaveSceneScriptDto atuSaveSceneScriptDto) throws Exception {
        try {
            if (atuSaveSceneScriptDto == null || "".equals(atuSaveSceneScriptDto.getScriptJson()))
                throw new PlatformRuntimeException(ScriptCaseAppError.SCENE_FILE_IS_NULL);
            SceneScript sceneScript = JSONObject.parseObject(atuSaveSceneScriptDto.getScriptJson(), SceneScript.class);
            if (sceneScript == null)
                throw new PlatformRuntimeException(ScriptCaseAppError.SCENE_DATA_CHANGE_ERROR);
            SceneNameVerifyDto sceneNameVerifyDto = new SceneNameVerifyDto();
            sceneNameVerifyDto.setSceneScript(sceneScript);
            sceneNameVerifyDto.setProjectId(atuSaveSceneScriptDto.getProjectId());
            logger.debug("SceneDataVerifier->begin sceneNameVerify");
            sceneNameVerify.verify(sceneNameVerifyDto);
            logger.debug("SceneDataVerifier->begin sceneNodeNameVerify");
            sceneNodeNameVerify.verify(sceneScript);
            logger.debug("SceneDataVerifier->begin sceneNodeNameVerify");
            sceneRelationVerify.verify(sceneScript);
            logger.debug("SceneDataVerifier->begin variableVerify");
            variableVerify.setProjectId(atuSaveSceneScriptDto.getProjectId());
            variableVerify.verify(sceneScript);

            if (StringUtils.hasText(atuSaveSceneScriptDto.getDataJson())) {
                UserCaseDataVerifyDto dataDto =
                        JSONObject.parseObject(atuSaveSceneScriptDto.getDataJson(), UserCaseDataVerifyDto.class);
                if(dataDto != null)
                    userCaseDataVerify.verify(dataDto);
            }

            return true;
        } catch (Exception e) {
            logger.debug("场景脚本数据校验失败, e = " + e);
            if (e instanceof PlatformRuntimeException)
                throw e;

            throw new PlatformRuntimeException(new ErrorMessage(e.getMessage()));
        }
    }
}
