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

export class BigCard{
    data;

    images;

    scaleX = 1;

    scaleY = 1;

    bg : ShapeRect;

    closePic : MySprite;

    shown = false;

    contentSpr;

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

        this.images = images;

        this.scaleX = scaleX;

        this.scaleY = scaleY;

        this.initView();
    }

    private initView(){
        
        let w = 1021 * this.scaleX;

        let h = 600 * this.scaleY

        const bg = new ShapeRect();
        bg.setSize(w, h);
        this.bg = bg;

        this.initCard();

        this.closePic = this.getColsePic(this.bg);

        this.bg.setScaleXY(0);
    }

    private initCard(){
        const card = new MySprite();
        card.init(this.images.get('card'));
        card.setScaleXY(this.scaleX);

        card.x = this.bg.width / 2;
        card.y = this.bg.height / 2;

        this.bg.addChild(card);


        
    }

    private getColsePic(parent) {
        const closePic = new MySprite();
        closePic.init(this.images.get('close'));
        closePic.scaleX = this.scaleX;
        closePic.scaleY = this.scaleY;

        closePic.x = parent.width;

        closePic.y = 0;
        parent.addChild(closePic);

        return closePic;
    }

    setContent(data){

        this.shown = false;

        if(this.contentSpr){
            this.bg.removeChild(this.contentSpr);
        }

        this.data = data;

        let mW = 960 * this.scaleX;

        let mH = 540 * this.scaleY;

        const pic = new MySprite();
        pic.init(this.images.get(this.data.picUrl));
        pic.x = this.bg.width / 2;
        pic.y = this.bg.height / 2;

        let scale = Math.min(mW / pic.width, mH / pic.height);
        pic.setScaleXY(scale);
        this.contentSpr = pic;
        this.bg.addChild(pic);
    }

    showCard(toX, toY, callback = null){
        this.shown = true;

        tweenChange(this.bg, {scaleX: 1, scaleY: 1, x : toX, y : toY}, 0.3, callback);
    }

    hideCard(toX, toY, callback = null){
        

        tweenChange(this.bg, { scaleX: 0, scaleY: 0, x: toX, y: toY }, 0.3, () => {

            this.shown = false;

            callback && callback();
        });
    }
}