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

export class Stage {

    /**背景 */
    bg;

    /**舞台 */
    stage: ShapeRect;

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

    scaleX = 1;

    scaleY = 1;

    prompt;

    leftDoor;

    rightDoor;

    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'));
        stage_box.scaleX = this.scaleX;
        stage_box.scaleY = this.scaleY;

        let width = stage_box.getBoundingBox().width;
        let height = stage_box.getBoundingBox().height;


        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 = 52 * 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;
        stage_box.y = height /2;
        bg.addChild(stage_box);
        
        const left = new MySprite();
        left.init(this.images.get('bg_1'));
        left.scaleX = this.scaleX;
        left.scaleY = this.scaleY;
        left.x = - left.getBoundingBox().width / 2;
        left.y = height - left.getBoundingBox().height/ 2;
        bg.addChild(left);

        const right = new MySprite();
        right.init(this.images.get('bg_1'));
        right.scaleX = this.scaleX;
        right.scaleY = this.scaleY;
        right.x = bg.width + left.getBoundingBox().width / 2;
        right.y = left.y;
        bg.addChild(right);
    }

    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.y = 51 * this.scaleY + leftDoor.getBoundingBox().height / 2;
        this.bg.addChild(leftDoor);
        this.leftDoor = leftDoor;

        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 * 2;
        rightDoor.y = leftDoor.y;
        this.bg.addChild(rightDoor);
        this.rightDoor = rightDoor;

        leftDoor.scaleX = 0;
        rightDoor.scaleX = 0;
    }

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

    closeDoor(callback = null){

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

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

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

        tweenChange(this.rightDoor, {scaleX : 0}, 2);
    }
}