export let imageZize = {
    width: 960,
    height: 608

}

export class OptionJigsaw {
    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 ItemJigsaw {
    public page;
    public audio;
    public image;
    public optionList;
    public voice;
    public time;
    public id;
    count: number;
    constructor(data, page) {
        // this.audio = data.audio;
        this.image = data.pic_url;
        // this.voice = data.voice;
        // this.time = data.time;
        this.id = page;

        // this.optionList = data.options.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 GameJigsaw {
    private static ins: GameJigsaw;
    public static getIns(): GameJigsaw {
        if (!GameJigsaw.ins) GameJigsaw.ins = new GameJigsaw();
        return GameJigsaw.ins;
    }
    private data: any;
    public lists: Array<ItemJigsaw>
    public title: string;
    public titleAudio: string;
    public image: string;
    public bgImage: string;
    public questionScore: string;
    constructor() {
        //
        this.start = false;
        this.lists = [];
    }
    get len() {
        return this.lists.length;
    }
    public aniEnter: any;
    public endImgAni: any;
    public tipBg: string;
    public tipPage: any;
    public tipSwitch: any;
    public endImgTitle: any;
    public jumpIdx: any;
    public init(data) {
        this.start = false;
        this.lists = [];
        this.data = data.hotZoneItemArr;
        this.aniEnter = data.imgAni;
        this.endImgAni = data.endImgAni;
        this.title = data.title;
        this.titleAudio = data.audio;
        this.questionScore = data.questionScore;
        this.image = data.bgItem.url;
        this.bgImage = data.image;
        this.tipBg = data.tipBg;
        this.tipPage = data.tipPage;
        this.tipSwitch = data.tipSwitch;
        this.endImgTitle = data.endImgTitle;
        this.jumpIdx = data.jumpIdx || -1;
        this.changepoints(data);
    }
    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 ItemJigsaw(data, i));
        }
        this.lists.sort(function () {
            return (0.5 - Math.random());
        });
    }
    checkSuccess(arr: OptionJigsaw[]) {
        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;
    }
    public pointsArr;
    private changepoints(data) {
        let scX = imageZize.width / data.bgItem.rect.width;
        let scY = imageZize.height / 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
            }
        })
        this.pointsArr = arr.concat();
    }

    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): ItemJigsaw {
        return this.lists.filter(li => li.page == page)[0]
    }
    getCurrentPage(): ItemJigsaw {
        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 = GameJigsaw;

