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

export class Card {

    images;

    mapScale = 1;

    bg: ShapeRect;

    initX = 0;

    initY = 0;

    toX = 0;

    toY = 0;

    canClick = false;

    curAnswer = null;

    bgPic: MySprite;

    bgWord: MySprite;

    h = 0;

    canHit = false;

    endFlag = false;

    init(images, mapScale = 1) {

        this.mapScale = mapScale;
        this.images = images;

        const bg = new ShapeRect();
        bg.alpha = 0;
        this.bg = bg;

        const pic = new MySprite();
        pic.init(this.images.get('bg_ip'));
        pic.setScaleXY(mapScale);
        bg.setSize(pic.getBoundingBox().width, pic.getBoundingBox().height);
        pic.x = bg.width / 2;
        pic.y = bg.height / 2;
        bg.addChild(pic);

        this.initPic();

        this.canClick = false;

        this.h = bg.height;

    }

    private initPic() {
        const bgPic = new MySprite();
        bgPic.init(this.images.get('bg_pic'));
        bgPic.setScaleXY(this.mapScale);
        bgPic.x = this.bg.width / 2;
        bgPic.y = - bgPic.getBoundingBox().height / 2 + 15 * this.mapScale;
        this.bgPic = bgPic;
        this.bg.addChild(bgPic);
        this.bgPic.visible = false;

        const bgWord = new MySprite();
        bgWord.init(this.images.get('bg_word'));
        bgWord.setScaleXY(this.mapScale);
        bgWord.x = this.bg.width / 2;
        bgWord.y = - bgWord.getBoundingBox().height / 2 + 15 * this.mapScale;
        this.bgWord = bgWord;
        this.bg.addChild(bgWord);
        this.bgWord.visible = false;
    }


    showNext(answer, callback = null) {

        this.curAnswer = answer;

        if (!answer) {

            if (callback) {
                callback();
            }
        }

        this.bgPic.removeChildren();

        this.bgWord.removeChildren();

        this.bgPic.visible = false;
        this.bgWord.visible = false;

        const setMaxScale = (target: MySprite, maxW, maxH) => {
            const sx = maxW / target.width;
            const sy = maxH / target.height;
            const s = Math.min(sx, sy);
            target.setScaleXY(Math.round(s * 1000) / 1000);
        }

        if (answer.contentType == 'P') {

            this.bgPic.visible = true;
            const pic = new MySprite();
            pic.init(this.images.get(answer.picUrl));
            pic.x = 0;
            pic.y = 0;
            setMaxScale(pic, 230, 172);
            this.bgPic.addChild(pic);

            this.h = this.bgPic.getBoundingBox().height;
        }
        else if (answer.contentType == 'T') {
            this.bgWord.visible = true;

            const label = new Label();
            label.text = answer.val;
            label.fontSize = 70;
            label.fontName = 'MMTextBook-Bold';
            label.textAlign = 'center';
            label.fontColor = '#18408d';
            label.setMaxSize(320);
            label.y = 0;
            label.x = 0;
            this.bgWord.addChild(label);
            this.h = this.bgWord.getBoundingBox().height;
        }

        if (callback) {
            callback();
        }
    }


}