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

}

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


        this.page = page;
        this.audio = data.audioUrl;
        this.image = data.bgItem.url;
        this.hotZoneItemArr = [];

        for (let i = 0; i < data.hotZoneItemArr.length; i++) {
            let d = data.hotZoneItemArr[i];
            this.hotZoneItemArr.push(new Option(d, i));
        }

        this.changepoints(data);
    }

    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();
    }
}
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<Item>
    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.questions;
        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;
    }
    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));
        }
        this.lists.sort(function () {
            return (0.5 - Math.random());
        });
    }
    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 = GameJigsaw;



