import ImgVoice from "../common/script/ImgVoice_hy14_pipei";

export let imageZize = {
    width: 1100,
    height: 572

}


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.audio = data.audio_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 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<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 hotZoneItemArr: any;
    public init(data) {
        this.data = data;
        this.start = false;
        this.lists = [];
        this.hotZoneItemArr = 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();
    }
    public start: boolean;
    public page: number;
    reset() {
        this.page = 1;
        this.start = true;
        this.lists = [];
        for (let i = 0; i < this.hotZoneItemArr.length; i++) {
            let data = this.hotZoneItemArr[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;
    public changepoints(wh: any) {
        let data = this.data;
        if (wh.w > 1300) {
            imageZize.width = 1150;
            let sx = imageZize.width / wh.w;
            imageZize.height = wh.h * sx;
        } else {
            imageZize.width = wh.w;
            imageZize.height = wh.h;
        }


        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
                text: item.text,
                width: item.rect.width * scX,
                height: item.rect.height * scY,
            }
        })
        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;
    }
    get opdata() {
        return this.hotZoneItemArr;
    }
}

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

