export class Option {
    type;
    txt;
    picUrl;
    audioUrl;
    public data;
    public id: number;
    public count: number;
    constructor(data, id) {
        this.id = id;
        this.data = data;
        this.type = data.type;
        this.txt = data.text;
        this.picUrl = data.image;
        this.audioUrl = data.optionAudio;
    }
}
export class Item {
    public page;
    public audio;
    public image;
    public optionList;
    public voice;
    public time;
    constructor(data, page) {
        this.audio = data.audio;
        this.image = data.image;
        this.voice = data.voice;
        this.time = data.time;
        let id = 0;

        this.optionList = data.optionList.map(o => {
            return new Option(o, id++);
        })

        this.optionList.sort(function () {
            return (0.5 - Math.random());
        });
        for (let i = 0; i < this.optionList.length; i++) {
            this.optionList[i].count = i;
        }

        this.page = page;
    }
}
export default class Game {
    private static ins: Game;
    public static getIns(): Game {
        if (!Game.ins) Game.ins = new Game();
        return Game.ins;
    }
    private data: any;
    public lists: Array<Item>
    constructor() {
        //
        this.start = false;
        this.lists = [];
    }
    get len() {
        return this.lists.length;
    }
    public aniEnter: any;
    public init(data) {
        this.start = false;
        this.lists = [];
        this.data = data.item.questionList;
        this.aniEnter = data.aniEnter;
    }
    public start: boolean;
    public page: number;
    reset() {
        this.page = 1;
        this.start = true;
        this.lists = [];
        for (let i = 0; i < this.data.length; i++) {
            let data = this.data[i];
            this.lists.push(new Item(data, i + 1));
        }
    }
    checkSuccess(arr: Option[]) {
        let data = this.getCurrentPage();
        let options = data.optionList.concat();
        options.sort((A, B) => A.id - B.id)
        for (let i = 0; i < options.length; i++) {
            if (arr[i] && arr[i].id != options[i].id) {
                return false;
            }
        }
        return true;
    }


    getDataByPage(page): Item {
        return this.lists.filter(li => li.page == page)[0]
    }
    getCurrentPage(): Item {
        let page = this.page;
        return this.lists.filter(li => li.page == page)[0]
    }
    getTotalPageNum() {
        return this.lists.length;
    }
    getCurrentPageNum() {
        return this.page;
    }
    addPage() {
        if (this.page >= this.lists.length) {
            return false;
        } else {
            this.page += 1;
            return true
        }
    }
    get isOver() {
        return this.page > this.lists.length;
    }
}

let win: any = window;
win.Game = Game;