import {
    Label,
    RichText,
    MySprite, tweenChange, ShapeRect,

} from './Unit';
import { Content } from '@angular/compiler/src/render3/r3_ast';

class Item {
    /**图片集合 */
    images;

    audioObj;

    _scaleX = 1;

    _scaleY = 1;

    set scaleX(v) {
        this._scaleX = v;
    }
    get scaleX() {
        return this._scaleX;
    }

    set scaleY(v) {
        this._scaleY = v;
    }
    get scaleY() {
        return this._scaleY;
    }

    constructor(images, audioObj, scaleX = 1, scaleY = 1) {
        this.images = images;
        this.audioObj = audioObj;
        this._scaleX = scaleX;
        this._scaleY = scaleY;
    }
}

export class Stage extends Item {

    bg;

    picBg;

    titleBg;

    curTitleLabel;

    curContent;

    mission: Mission;

    baseW = 1162;
    bashH = 642;

    width = 1162;
    height = 642;

    stageContentArr = [];

    curIndex = -1;

    init(theme) {

        this.width = this.baseW * this.scaleX;
        this.height = this.bashH * this.scaleY;

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

        const stageBg = new MySprite();
        stageBg.init(this.images.get(theme + "bg2"));
        stageBg.scaleX = this.scaleX;
        stageBg.scaleY = this.scaleY;
        stageBg.x = stageBg.getBoundingBox().width / 2;
        stageBg.y = stageBg.getBoundingBox().height / 2;
        bg.addChild(stageBg);

        const picBg = new MySprite();
        picBg.init(this.images.get(theme + "pic_bg"));
        picBg.scaleX = this.scaleX;
        picBg.scaleY = this.scaleY;
        picBg.x = this.width / 2;
        picBg.y = this.height / 2;
        bg.addChild(picBg);


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

        const titlePic = new MySprite();
        titlePic.init(this.images.get(theme + "title_bg"));
        titlePic.scaleX = this.scaleX;
        titlePic.scaleY = this.scaleY;
        titlePic.x = this.width / 2 + 230 * this.scaleX;
        titlePic.y = 148 * this.scaleY;

        bg.addChild(titlePic);

        const titleBg = new ShapeRect();
        titleBg.setSize(495 * this.scaleX, 134 * this.scaleY);
        titleBg.x = (this.width - titleBg.width) / 2;
        titleBg.y = 12 * this.scaleY;
        titleBg.alpha = 0;
        this.titleBg = titleBg;
        bg.addChild(titleBg);


        const boxPic = new MySprite();
        boxPic.init(this.images.get(theme + "box"));
        boxPic.scaleX = this.scaleX;
        boxPic.scaleY = this.scaleY;
        boxPic.x = this.width / 2;
        boxPic.y = this.height / 2 - 30 * this.scaleY;
        bg.addChild(boxPic);

    }

    setStageContent(stageItemArr) {
        if (!stageItemArr) {
            return;
        }

        const setScale = (pW, pH, child) => {
            let width = child.width;
            let height = child.height;

            let sx = pW / width;
            let sy = pH / height;

            let scale = Math.min(sx, sy);
            child.setScaleXY(scale);
        }

        const getPic = (imgUrl) => {
            const pic = new MySprite();
            pic.init(this.images.get(imgUrl));
            pic.x = this.picBg.width / 2;
            pic.y = this.picBg.height / 2 - 9 * this.scaleY;

            setScale(719 * this.scaleX, 383 * this.scaleY, pic);

            return pic;
        };

        for (let i = 0; i < stageItemArr.length; ++i) {
            let stageItem = stageItemArr[i];

            if (!stageItem) {
                continue;
            }

            let pic;

            if (stageItem.imgUrl) {
                pic = getPic(stageItem.imgUrl);
            }

            this.stageContentArr.push({ index: stageItem.index, title: stageItem.title, pic: pic })
        }

    }

    showLast(callback = null) {
        if (this.curIndex == 0) {
            if (callback) {
                callback();
            }
            return;
        }

        this.curIndex -= 1;

        this.showContent(callback);
    }

    showNext(callback = null) {
        if (this.curIndex == this.stageContentArr.length - 1) {
            if (callback) {
                callback();
            }
            return;
        }

        this.curIndex += 1;

        this.showContent(callback);
    }

    private showContent(callback = null) {

        let content = this.stageContentArr[this.curIndex];

        this.mission.setStageVal(content.index);

        this.setTitleVal(content.title);

        this.setPic(content, () => {
            this.curContent = content;
        });

        if (callback) {
            callback();
        }
    }

    private setTitleVal(val) {

        if (!val) {
            return;
        }

        if (this.curTitleLabel) {
            this.titleBg.removeChild(this.curTitleLabel);
        }

        console.log('this._scaleX = ' + this._scaleX);
        const title = new RichText();
        title.scaleX = this._scaleX;
        title.scaleY = this._scaleY;
        title.width = this.titleBg.width - 40 * this._scaleX;
        title.disH = 0;
        title.text = val;
        title.textAlign = 'left';
        title.fontSize = 40;
        title.fontName = "BRLNSDB";
        title.fontColor = "#a0e9ff";
        title.refreshSize();
        title.x = (this.titleBg.width - title.width) / 2;
        title.y = this.titleBg.height / 2;
        this.titleBg.addChild(title);

        // title.scaleX = Math.min(this.scaleX, 500 / title.width);
        // title.scaleY = Math.min(this.scaleY, 150 / title.height);

        this.curTitleLabel = title;
    }

    private setPic(content, callback = null) {

        if (this.curContent && this.curContent.pic) {
            this.picBg.removeChild(this.curContent.pic);
        }

        if (content.pic) {
            this.picBg.addChild(content.pic);
        }

        if (callback) {
            callback();
        }
    }
}

export class Mission extends Item {
    bg;

    stageValLabel;

    stageBgPic;

    baseW = 435;
    bashH = 96;

    width = 435;
    height = 96;

    init(theme) {
        this.width = this.baseW * this.scaleX;
        this.height = this.bashH * this.scaleY;

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

        const missionPic = new MySprite();
        missionPic.init(this.images.get(theme + "e-mission"));
        missionPic.x = missionPic.width / 2 - 20;
        missionPic.y = this.height / 2;
        bg.addChild(missionPic, 2);

        const stageBgPic = new MySprite();
        stageBgPic.init(this.images.get(theme + "stage_bg"));
        stageBgPic.alpha = 0;
        stageBgPic.x = missionPic.width + stageBgPic.width / 2 - 70;
        stageBgPic.y = this.height / 2;
        this.stageBgPic = stageBgPic;
        bg.addChild(stageBgPic, 1);

        const stageVal = new Label();
        stageVal.text = "";
        stageVal.fontName = "BRLNSDB";
        stageVal.fontColor = "#ffffff"
        stageVal.fontSize = 34;
        stageVal.textAlign = 'middle';
        stageVal.refreshSize();
        stageVal.x = -stageBgPic.width / 4;
        stageVal.y = -5;

        stageVal.alpha = 0;
        stageBgPic.addChild(stageVal);
        this.stageValLabel = stageVal;
    }

    setStageVal(index) {
        if (!index) {
            this.stageBgPic.alpha = 0;
            this.stageValLabel.alpha = 0;
            return;
        }

        this.stageValLabel.text = 'Stage ' + index;
        this.stageBgPic.alpha = 1;
        this.stageValLabel.alpha = 1;
    }
}