export class Item {
    public page;
    public audio;
    public duration;
    public optionList;
    public bgItem;
    public hotZoneItemArr;
    public pointsArr;
    public text: string;
    public isClick: boolean;
    constructor(data, page, { w, h }) {
        this.audio = data.audio;
        this.text = data.text;
        this.bgItem = data.bgItem;
        this.hotZoneItemArr = data.hotZoneItemArr;

        this.page = page;
        this.isClick = false;
        this.changepoints(data, { w, h });
    }
    private changepoints(data, { w, h }) {
        let scX = w / data.bgItem.rect.width;
        let scY = h / data.bgItem.rect.height;
        let px = data.bgItem.rect.x;
        let py = data.bgItem.rect.y;
        this.pointsArr = [];
        let arr = [];
        data.hotZoneItemArr.forEach((item, idx) => {
            let p = item.rect;
            arr[idx] = {
                x: (p.x + (p.width / 2)) * scX,// - px
                y: -((p.y + (p.height / 2)) * scY),// - py
                width: p.width * scX,
                height: p.height * scY,
                audio: item.audio_url
            }
        })
        this.pointsArr = arr.concat();
    }
}
export enum GAME_STATE {
    WAIT,
    RUNNING,
    OVER
}
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 state: GAME_STATE;
    public total: number;

    constructor() {
        this.start = false;
        this.lists = [];
        this.state = GAME_STATE.WAIT;
    }
    get len() {
        return this.lists.length;
    }
    public question: { text, audio };
    public isCanClick: boolean;
    public failCount;
    public dispelCount;
    public startAudio: string;
    public init(data) {
        this.startAudio = data.startAudio || "";
        this.start = false;
        this.lists = [];
        this.data = data.questions;
    }
    public start: boolean;
    public page: number;
    reset({ w, h }) {
        this.isCanClick = false;
        this.failCount = 0;
        this.dispelCount = 0;
        this.page = 0;
        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, { w, h }));
        }
        this.state = GAME_STATE.WAIT;
        this.total = this.lists.length;
    }
    public getCardInfo() {
        let option: Item = this.lists.splice(0, 1)[0];
        console.log(" this.cardInfo=======", option)
        return option;
    }
    public getQuestions() {
        return this.lists.concat();
    }
    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() {
        this.page += 1;
    }
    subPage() {
        this.page -= 1;
    }
    hasPre() {
        return this.page > 0;
    }
    hasNext() {
        return this.page < this.lists.length - 1;
    }
    get getTotal() {
        return this.lists.length;
    }
    get isOver() {
        return this.page > this.lists.length;
    }
}