/**
 * 模块的入口类，提供对外的接口方法
 */
import {EditorContext} from "./vo/editorContext";
import {ParseResult} from "./vo/parseResult";
import {ParseServiceFactory} from "../service/parseServiceFactory";
import {ParseService} from "../service/parseService";
import {CommonTokenStream, Token} from "antlr4ts";
import {Recovermark} from "../strategy/recovermark";
import {SymbolElement} from "../symbols/symbolElement";
import {CodeCompletionRequest} from "./vo/codeCompletionRequest";

export class EditorParser {

    public static parse(editorContext:EditorContext): ParseResult{


        let parseService = ParseServiceFactory.createParseService(editorContext);
        return EditorParser.start(parseService);

    }

    //处理逻辑
    private static start(parseService:ParseService): ParseResult{
        let result = new ParseResult();

        //1.先对语句进行解析
        let tokenStream: CommonTokenStream = parseService.parserSQL();
        if (tokenStream == undefined){
            console.log("SQL语句解析错误，返回的tokenStream为undefined");
            return EditorParser.generateErrorParseResult(parseService);
        }


        //2.看看是否进行了修复，如果修复了就再解析一次
        if (Recovermark.isRecover){
            parseService.editorContext = Recovermark.editorContext;
            tokenStream = parseService.parserSQL();
        }

        //3.如果解析还是出错了，就返回结果，进行错误提示，并进行关键字提示。
        if (Recovermark.errorMarkList != undefined){
            return EditorParser.generateErrorParseResult(parseService);

        }


        //4.找到当前需要补全的Token
        let token:Token;
        try {
            token = parseService.findTokenByRangeOffSetInAST(tokenStream);
        }catch (err) {
            console.log("当前光标在AST中定位错误");
            console.log(err)
        }
        if (token == undefined){
            console.log("当前光标在AST中定位错误");
            return EditorParser.generateErrorParseResult(parseService);
        }


        //5.在符号表中找到对应的符号元素
        let symbolElement:SymbolElement = parseService.findSymbolElementByTokenInSymbolTable(token);
        if (symbolElement == undefined){
            console.log("无法在符号表中找到当前token");

            return EditorParser.generateErrorParseResult(parseService);
        }


        //6.生成需要向后端发送补全请求的参数对象
        let completionRequest:CodeCompletionRequest = parseService.generateCodeCompletionRequest(symbolElement);


        //7.构造统一的返回值，统一返回。
        return this.generateCorrectParseResult(completionRequest);

    }

    private static generateErrorParseResult(parseService: ParseService):ParseResult {
        let result = new ParseResult();
        result.isSucceed = false;
        result.errorMarkList = Recovermark.errorMarkList;
        result.expectList = parseService.getKeyWorld();
        Recovermark.errorMarkList = [];
        return result;
    }

    private static generateCorrectParseResult(completionRequest:CodeCompletionRequest):ParseResult{
        let result = new ParseResult();
        result.isSucceed = true;
        result.completionRequest = completionRequest;

        return result;
    }

}