export class Option {
    public type;
    public text;
    public right;
    public audioUrl;
    public data;
    public id: number;
    public count: number;
    constructor(data, id) {
        this.id = id;
        this.type = data.type;
        this.text = data.text;
        this.right = data.right;
        this.audioUrl = data.optionAudio;
    }
}
export class Item {
    public page;
    public audio;
    public image;
    public optionList;
    // public questionAudio;
    public wordAudio;
    public contentArr;
    count: number;
    constructor(data, page) {
        // this.audio = data.audio;
        this.image = data.questImg;
        // this.questionAudio = data.questionAudio;
        this.wordAudio = data.wordAudio;
        this.contentArr = data.contentArr;
        let id = page;

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

        // for (let i = 0; i < this.optionList.length; i++) {
        //     this.optionList[i].count = i;
        // }

        this.page = page + 1;
    }
    public randomOptions() {
        this.optionList.sort(function () {
            return (0.5 - Math.random());
        });
    }
}
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>
    public title: string;
    public vectoringAudio: string;
    public waitAudio: string;
    public twoWaitAudio: string;
    public twoVectoringAudio: string;
    public bgImage: string;
    public roundCount: number;
    constructor() {
        //
        this.start = false;
        this.lists = [];
    }
    get len() {
        return this.lists.length;
    }
    public init(data) {
        this.start = false;
        this.lists = [];
        this.data = data.questions;
        this.title = data.title;
        this.vectoringAudio = data.vectoringAudio;
        this.waitAudio = data.waitAudio;
        this.twoWaitAudio = data.twoWaitAudio;
        this.twoVectoringAudio = data.twoVectoringAudio;
        this.bgImage = data.image;
    }
    public start: boolean;
    public page: number;
    reset() {
        this.roundCount = 1;
        this.page = 1;
        this.start = true;
        this.lists = [];
        let arr = [];
        for (let i = 0; i < this.data.length; i++) {
            let data = this.data[i];
            arr.push(new Item(data, i));
        }

        let page = arr[0].page;
        do {
            arr.sort(() => Math.random() - 0.5);
        } while (arr[0].page == page);

        arr.forEach((item, idx) => {
            item.page = idx + 1;
            this.lists.push(item);
        })
    }
    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;
    }
    getTotalLabel() {
        let data = this.getCurrentPage();
        let options = data.optionList.concat();
        options.sort((A, B) => A.id - B.id)
        let str = '';
        options.forEach((item) => { str += item.txt; });
        return str;
    }

    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
        }
    }
    subPage() {
        if (this.page <= 1) {
            return false;
        } else {
            this.page -= 1;
            return true
        }
    }
    get list() {
        return this.lists;
    }
    get isOver() {
        return this.page > this.lists.length;
    }
}

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