import {
    MySprite, tweenChange,
    ShapeRect,
    Label
} from './Unit';

export class Stage {

    /**背景 */
    bg;

    /**舞台 */
    stage: ShapeRect;

    //图片数组
    images = new Map();

    scaleX = 1;

    scaleY = 1;

    prompt;

    leftDoor;

    leftDoorInitX;

    rightDoor;
    rightDorrInitX;

    constructor(images) {
        this.images = images;
    }

    setScaleXY(scale) {
        this.scaleX = this.scaleY = scale;
    }

    init() {

        const stage_box = new MySprite();
        stage_box.init(this.images.get('stage_box'), 0.5, 1);
        stage_box.scaleX = this.scaleX;
        stage_box.scaleY = this.scaleY;

        let width = 1280 * this.scaleX;
        let height = 713 * this.scaleY;


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

        const stage_bg = new MySprite();
        stage_bg.init(this.images.get('stage_bg'));
        stage_bg.scaleX = this.scaleX;
        stage_bg.scaleY = this.scaleY;
        stage_bg.x = width / 2;
        stage_bg.y = 53 * this.scaleY + stage_bg.getBoundingBox().height / 2;
        bg.addChild(stage_bg);


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

        let prompt = new Label();
        prompt.text = '0/0';
        prompt.textAlign = 'middle';
        prompt.fontSize = 66;
        prompt.fontName = "BRLNSDB";
        prompt.fontColor = "#F2CE29";
        prompt.setOutline(3, '#93490B');
        prompt.setShadow(0, 4, 3.0, '#A6643F');
        prompt.scaleX = this.scaleX;
        prompt.scaleY = this.scaleY;
        prompt.refreshSize();
        prompt.x = 120 * this.scaleX;
        prompt.y = 116 * this.scaleY + prompt.getBoundingBox().height / 2;
        this.prompt = prompt;
        bg.addChild(prompt);

        //初始化门
        this.initDoor();

        stage_box.x = width / 2 + 1 * this.scaleX;
        stage_box.y = height;
        bg.addChild(stage_box);
    }

    private initDoor() {
        const leftDoor = new MySprite();
        leftDoor.init(this.images.get('doorleft'), 0);
        leftDoor.scaleX = this.scaleX;
        leftDoor.scaleY = this.scaleY;
        leftDoor.x = 75 * this.scaleX - leftDoor.getBoundingBox().width;
        leftDoor.y = 51 * this.scaleY + leftDoor.getBoundingBox().height / 2;
        this.bg.addChild(leftDoor);
        this.leftDoor = leftDoor;
        this.leftDoorInitX = leftDoor.x;

        const rightDoor = new MySprite();
        rightDoor.init(this.images.get('doorright'), 1);
        rightDoor.scaleX = this.scaleX;
        rightDoor.scaleY = this.scaleY;
        rightDoor.x = leftDoor.x + leftDoor.getBoundingBox().width * 4;
        rightDoor.y = leftDoor.y;
        this.bg.addChild(rightDoor);
        this.rightDoor = rightDoor;
        this.rightDorrInitX = rightDoor.x;
    }

    setPromptText(text) {
        this.prompt.text = text;
    }

    closeDoor(callback = null) {

        tweenChange(this.leftDoor, { x: 75 * this.scaleX }, 2, callback);

        let toX = this.rightDorrInitX - this.rightDoor.getBoundingBox().width;
        tweenChange(this.rightDoor, { x: toX }, 2);
    }

    openDoor(callback = null) {
        tweenChange(this.leftDoor, { x: this.leftDoorInitX }, 2, callback);

        tweenChange(this.rightDoor, { x: this.rightDorrInitX }, 2);
    }
}