import { Label, MySprite, ShapeCircle, ShapeRect } from "./Unit";

export class LineSpr{
    images;

    scaleX = 1;

    scaleY = 1;

    bg : ShapeCircle;

    baseWidth = 180;

    baseHeight = 70;

    label : Label;

    init(images, scaleX = 1, scaleY = 1){

        this.images = images;

        this.scaleX= scaleX;

        this.scaleY = scaleY;

        let w = this.baseWidth * this.scaleX;
        let h = this.baseHeight * this.scaleY;

        this.initBg(w, h);

        this.initLine();
    }

    private initBg(w, h){
        const bg = new ShapeRect();
        bg.setSize(w, h);
        bg.alpha = 0;

        this.bg = bg;
    }

    private initLine(){
        const line = new MySprite();
        line.init(this.images.get('line'));
        line.scaleX = this.scaleX;
        line.scaleY = this.scaleY;

        line.y = this.bg.height;
        line.x = this.bg.width / 2;
        this.bg.addChild(line);

        return line;
    }

    showLabel(val){

        if(this.label){
            this.label.text = val;
        }
        else{
            const label = new Label();
            label.text = val;
            label.textAlign = 'center';
            label.fontSize = 60;
            label.fontName = "MMB";
            label.fontColor = "#2f2d32";
            label.setScaleXY(this.scaleX);
            label.refreshSize();
            label.visible = false;
            this.label = label;
            this.bg.addChild(label);
        }

        this.label.visible = true;
        this.label.refreshSize();

        this.label.x = this.bg.width / 2;
        this.label.y = this.bg.height / 2;
    }

    hideLabel(){
        this.label.visible = false; 
        this.label.text = "";
    }
}