export enum BLOCK_TYPE {
    IMG = 'img',
    TXT = 'txt',
    LINE = 'line',
    TAG = 'tag'
}
export enum BOARD_STATE {
    DEFAULT = 'default',
    INPUT = 'input',
    RIGHT = 'right',
    ERROR = 'error',
    CHECK = 'check'
}
export class Block {
    public static id: number = 10000;
    public id: number;
    public type: BLOCK_TYPE;
    public img: string;
    public line: number;
    public text: string;
    public answer: string;//用户填写答案
    public tag: string;
    constructor(data) {
        console.log(data);
        this.id = Block.id++;
        this.type = data.type;
        this.img = data.image;
        this.text = data.text;
        this.tag = data.tag;
        this.line = Number(data.line);
        this.answer = '';
    }
    addKeyBoard(val) {
        if (!this.answer) this.answer = ''
        this.answer += val;
    }
    deleteKeyBoard() {
        if (!this.answer || this.answer == '') {
            this.answer = ''
            return;
        }
        this.answer = this.answer.substring(0, this.answer.length - 1)
    }
    finished() {
        if (this.type == BLOCK_TYPE.LINE) {
            return this.answer.length != 0
        } else {
            return true
        }
    }
    isRight() {
        if (this.type == BLOCK_TYPE.LINE) {
            return this.answer == this.text;
        } else {
            return true
        }
    }
}
export class Option {
    public static id: number = 10000;
    public id: number;
    public list: Array<Block>;
    constructor(data) {
        this.id = Option.id++;
        console.log(data);
        this.list = data.blocks.map(block => new Block(block));
    }
    isDoubleLine() {
        return this.list.some(block => block.type == BLOCK_TYPE.IMG)
    }
    getList() {
        return this.list.concat();
    }
    finished() {
        return this.list.every(block => block.finished())
    }
    isRight() {
        return this.list.every(block => block.isRight())
    }
}
export class Question {
    public static id: number = 1000;
    public id: string;
    public list: Array<Option>;
    public submit: BOARD_STATE;
    public words;
    public answer;
    constructor(data) {
        this.id = data.id;
        console.log(data);
        this.list = data.options.map(option => new Option(option));
        this.submit = BOARD_STATE.DEFAULT
        this.answer = data.answer
        this.words = data.answer.split('');
        while (this.words.length < 14) {
            let str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
            this.words.push(str[Math.floor(Math.random() * str.length)])
        }
    }
    getWords() {
        let rand = Math.floor(Math.random() * 100);
        let words = this.words.map((w, index) => {
            return {
                word: w,
                color: (index + rand) % 5 + 1,
                rotation: Math.random() < 0.5 ? 3 : -3
            }
        });
        for (let i = 0; i < 10; i++) {
            words.sort((A, B) => {
                return Math.random() - 0.5
            });
        }
        return words;
    }
    setSubmit(state) {
        this.submit = state
    }
    getSubmit() {
        return this.submit;
    }
    getList() {
        return this.list.concat();
    }
    finished() {
        return this.list.every(option => option.finished())
    }
    isRight() {
        return this.list.every(option => option.isRight())
    }
}
export class GameNGT14 {
    public questions: Array<Question>
    public blocks: Array<{ val: string }>
    public blockStruct: Array<{ pId: string, answer: string, point: { x: number, y: number } }>
    public current: number;
    public title: string;
    public total: number;
    public aniEnter;
    public startAudio;

    constructor() {
        this.current = 0;
        this.questions = [];
    }
    reset() {
        this.current = 0;
    }
    initData(data) {
        console.log(data);
        this.questions = data.questions[0].parts.map(part => new Question(part));
        this.blockStruct = data.blockStruct;
        this.blocks = data.blocks;
        this.title = data.title;
        this.startAudio = data.startAudio || "";
        this.aniEnter = data.imgAni;
    }
    getBlockVal(x, y) {
        if (!this.blocks[y] || !this.blocks[y][x]) return;
        return this.blocks[y][x].val;
    }
    getHightList() {
        let data = this.getCurrent();
        let line = this.blockStruct.find(block => block.pId == data.id);
        let list = [];
        for (let i = 0; i < line.answer.length; i++) {
            list.push({
                x: line.point.x + i,
                y: line.point.y,
                rightVal: line.answer[i],
                compare: function (p) {
                    return this.x == p.x && this.y == p.y;
                }
            })
        }
        return list
    }
    getCurrent(): Question {
        return this.questions[this.current];
    }
    static ins: GameNGT14;
    static getIns(): GameNGT14 {
        if (!GameNGT14.ins) GameNGT14.ins = new GameNGT14();
        return GameNGT14.ins;
    }
}