import { Item, setScale } from './Item';
import { Sheepfold, Course, Sheep } from '../form/Course';
import { ShapeRect, Label, MySprite, randomSortByArr } from './Unit';
import { playAudio, pauseAudio } from './AudioUtil';

export class Stage extends Item {

    _data;

    set data(v) {
        if (v) {
            this._data = v;
        }
        else {
            this._data = new Course();
        }
    }

    get data() {
        return this._data;
    }

    sheepfoldArr: Array<SheepfoldItem> = [];

    sheepArr: Array<SheepItem> = [];

    sheepBg: ShapeRect;

    sheepCoordinates = [];

    init(data) {

        this.data = data;

        this.width = 1280;

        this.height = 550;

        const bg = new ShapeRect();
        bg.setSize(this.width, this.height);
        bg.alpha = 0;
        this.bg = bg;

        //初始化羊圈
        this.initSheepfold();

        this.initSheep();
    }

    private initSheepfold() {

        let maxW = 1144 * this.scaleX;

        let maxH = 260 * this.scaleY;

        let width = 0;

        let height = 0;

        const sheepfoldBg = new ShapeRect();

        let disW = 54 * this.scaleX;

        for (let i = 0; i < this.data.sheepfoldArr.length; ++i) {
            const sheepfold = new SheepfoldItem(this.images, this.audioObj, this.scaleX, this.scaleY);
            sheepfold.init(this.data.sheepfoldArr[i]);
            this.sheepfoldArr.push(sheepfold);
            sheepfold.x = (disW + sheepfold.width) * i;
            
            sheepfold.y = 0;
            height = sheepfold.height;

            width = sheepfold.width;
            sheepfoldBg.addChild(sheepfold.bg);
        }

        let totalW = this.data.sheepfoldArr.length * width + (this.data.sheepfoldArr.length - 1) * disW;


        sheepfoldBg.setSize(totalW, height);
        sheepfoldBg.alpha = 0;
        this.bg.addChild(sheepfoldBg);


        const sx = maxW / totalW;
        if (sx < 1) {
            sheepfoldBg.setScaleXY(sx);
        }
        sheepfoldBg.x = (this.width - sheepfoldBg.getBoundingBox().width) / 2;
        sheepfoldBg.y = 60 * this.scaleY + (maxH - sheepfoldBg.getBoundingBox().height) / 2;

    }

    private initSheep() {
        let maxW = 1152 * this.scaleX;

        let maxH = 185 * this.scaleY;

        let width = 0;

        let height = 0;

        const sheepBg = new ShapeRect();
        this.sheepBg = sheepBg;
        sheepBg.alpha = 0;
        this.bg.addChild(sheepBg);

        let disW = 60;

        for (let i = 0; i < this.data.sheepArr.length; ++i) {
            const sheep = new SheepItem(this.images, this.audioObj, this.scaleX, this.scaleY);
            sheep.init(this.data.sheepArr[i]);
            this.sheepArr.push(sheep);
            width = sheep.width;
            height = sheep.height;
        }

        //取出间隔
        for (let i = disW; i > 5; --i) {

            let totalW = this.sheepArr.length * width + (this.sheepArr.length - 1) * i * this.scaleX;

            disW = i;

            if (totalW < maxW) {
                break;
            }
        }

        disW *= this.scaleX;

        for (let i = 0; i < this.sheepArr.length; ++i) {

            let x = (disW + this.sheepArr[i].width) * i;

            let y = 0;
            if (i % 2 === 0) {
                y = (maxH - height) / 2 + 5 * this.scaleY;
            }
            else {
                y = (maxH - height) / 2 - 5 * this.scaleY;
            }
            this.sheepCoordinates.push({ x: x, y: y });
        }

        let totalW = this.sheepArr.length * width + (this.sheepArr.length - 1) * disW * this.scaleX;
        sheepBg.setSize(totalW, maxH);
        const sx = maxW / totalW;
        if (sx < 1) {
            sheepBg.setScaleXY(sx);
        }
        sheepBg.x = (this.width - sheepBg.getBoundingBox().width) / 2;
        sheepBg.y = 334 * this.scaleY + (maxH - sheepBg.getBoundingBox().height) / 2;
    }

    randomSheep() {

        this.sheepBg.removeChildren();

        //清空羊圈
        for (let i = 0; i < this.sheepfoldArr.length; ++i) {
            this.sheepfoldArr[i].claer();
        }

        this.sheepArr = randomSortByArr(this.sheepArr);

        for (let i = 0; i < this.sheepArr.length; ++i) {
            this.sheepArr[i].answered = false;
            this.sheepArr[i].x = this.sheepCoordinates[i].x;
            this.sheepArr[i].y = this.sheepCoordinates[i].y;

            this.sheepArr[i].initX = this.sheepArr[i].x;
            this.sheepArr[i].initY = this.sheepArr[i].y;
            this.sheepBg.addChild(this.sheepArr[i].bg);
        }
    }
}


export class SheepfoldItem extends Item {

    _data;

    set data(v) {
        if (v) {
            this._data = v;
        }
        else {
            this._data = new Sheepfold();
        }
    }

    get data() {
        return this._data;
    }

    bubbleBg: ShapeRect;

    /**羊圈范围 */
    rangeBg: ShapeRect;

    startX = 0;

    startY = 0;

    init(data) {

        this.data = data;

        const bg = new ShapeRect();
        bg.alpha = 0;
        this.bg = bg;

        this.initPic();

        this.initBubble();


    }

    private initPic() {
        const pic = this.getPic('sheepfold');
        this.width = pic.width;
        this.height = pic.height;
        pic.x = this.width / 2;
        pic.y = this.height / 2;
        this.bg.addChild(pic);

        const rangeBg = new ShapeRect();
        rangeBg.setSize(300 * this.scaleX, 280 * this.scaleY);
        rangeBg.alpha = 0;
        rangeBg.x = (this.width - rangeBg.width) / 2;
        rangeBg.y = this.height - rangeBg.height;
        this.bg.addChild(rangeBg);
        this.rangeBg = rangeBg;
    }

    private initBubble() {

        const bg = new ShapeRect();
        bg.alpha = 0;
        this.bubbleBg = bg;
        this.bg.addChild(bg);

        const bubble = this.getPic('bubble', 0, 0);
        bg.addChild(bubble);
        bg.setSize(bubble.getBoundingBox().width, bubble.getBoundingBox().height);

        if(this.data.contentType === 'P'){
            const pic = this.getPic(this.data.picUrl);
            pic.x = bg.width / 2;
            pic.y = bg.height / 2;
            //将scale恢复成1
            pic.setScaleXY(1);
            setScale(bg.width - 45 * this.scaleX, bg.height - 10 * this.scaleY, pic);
            bg.addChild(pic);
        }
        else{
            let text = new Label();
            text.text = this.data.word;
            text.textAlign = 'middle';
            text.fontSize = 90;
            text.fontName = "BRLNSDB";
            text.fontColor = "#000000";
            text.alpha = 1;
            text.scaleX = this.scaleX;
            text.scaleY = this.scaleY;
            text.setMaxSize(bg.width - 30 * this.scaleX);
            text.x = (bg.width - text.getBoundingBox().width) / 2;
            text.y = bg.height / 2;
            bg.addChild(text);
        }
        

        bg.x = (this.width - bg.width) / 2;
        bg.y = - (20 + bg.height);

       
    }


    click(callback = null) {

        playAudio(this.audioObj[this.data.id], true);

        if (callback) {
            callback();
        }
    }

    stopAudio() {
        pauseAudio(this.audioObj[this.data.id], true);
    }

    into(sheep: SheepItem) {
        this.rangeBg.addChild(sheep.bg);

        if (this.startX + sheep.bg.width > this.rangeBg.width) {
            this.startY += 40 * this.scaleY + 10 * Math.random();
            this.startX = 10 * Math.random() + 15 * this.scaleX;
        }

        sheep.x = this.startX;
        sheep.y = this.startY + 10 * Math.random();

        this.startX += 70 * this.scaleX;
    }

    claer() {
        this.rangeBg.removeChildren();
        this.startX = 0;
        this.startY = 0;
    }
}

export class SheepItem extends Item {

    private _data;

    answered = false;

    initX = 0;

    initY = 0;

    light: MySprite;

    set data(v) {
        if (v) {
            this._data = v;
        }
        else {
            this._data = new Sheep();
        }
    }

    get data() {
        return this._data;
    }

    init(data) {
        this.data = data;

        const bg = new ShapeRect();
        bg.alpha = 0;
        this.bg = bg;

        const pic = this.getPic('sheep');
        this.width = pic.width;
        this.height = pic.height;
        pic.x = this.width / 2;
        pic.y = this.height / 2;
        this.bg.addChild(pic);

        const bubble = new Bubble(this.images, this.audioObj, this.scaleX, this.scaleY);
        bubble.init('s_bubble', this.data.contentType, this.data.contentType === 'T' ? this.data.word : this.data.picUrl);
        bubble.x = (this.width - bubble.width) / 2 - 5 * this.scaleX;
        bubble.y = (this.height - bubble.height) / 2 + 5 * this.scaleY;
        this.bg.addChild(bubble.bg);

        const light = this.getPic('light');
        light.alpha = 0;
        light.x = pic.x;
        light.y = pic.y;
        this.bg.addChild(light);
        this.light = light;
    }

    checkRight(sheepfoldItem: SheepfoldItem) {

        if (sheepfoldItem.data.id === this._data.sheepfoldId) {
            this.answered = true;
            return true;
        }
        else {
            return false;
        }
    }

    into(sheepfoldItem: SheepfoldItem, callback = null) {

        playAudio(this.audioObj['right'], true);

        sheepfoldItem.into(this);

        if (callback) {
            callback();
        }
    }

    showLight() {
        this.light.alpha = 1;
    }

    hideLight() {
        this.light.alpha = 0;
    }

    playAudio(){
        playAudio(this.audioObj[this.data.audioUrl], true);
    }

    stopAudio(){
        pauseAudio(this.audioObj[this.data.audioUrl], true);
    }
}

export class Bubble extends Item {

    text: Label;

    pic: MySprite;

    init(picName, valType, val) {

        const bg = new ShapeRect();
        bg.alpha = 0;
        this.bg = bg;

        const pic = this.getPic(picName);
        this.width = pic.width;
        this.height = pic.height;
        pic.x = this.width / 2;
        pic.y = this.height / 2;


        bg.addChild(pic);


        if (valType === 'P') {
            this.initPic(val);
        }
        else {
            this.initText(val);
        }
    }

    private initPic(picUrl) {
        const pic = this.getPic(picUrl);
        pic.x = this.width / 2;
        pic.y = this.height / 2;
        this.pic = pic;
        //将scale恢复成1
        pic.setScaleXY(1);
        setScale(this.width - 40 * this.scaleX, this.height - 10 * this.scaleY, pic);
        this.bg.addChild(pic);
    }

    private initText(val) {
        let text = new Label();
        text.text = val;
        text.textAlign = 'middle';
        text.fontSize = 28;
        text.fontName = "BRLNSDB";
        text.fontColor = "#000000";
        text.alpha = 1;
        // text.scaleX = this.scaleX;
        // text.scaleY = this.scaleY;
        text.setMaxSize(this.width - 30 * this.scaleX);
        text.x = (this.width - text.getBoundingBox().width) / 2;
        text.y = this.height / 2;
        this.text = text;
        this.bg.addChild(text);
        this._scaleX = text.scaleX;
        this._scaleY = text.scaleY;
    }
}