import { Label, Label2, LabelText, Line, MySprite, ShapeRect } from "./Unit";

class Eexercises {
    images;

    scaleX = 1;

    scaleY = 1;

    bg: ShapeRect;

    baseWidth = 380;

    baseHeight = 160;

    data;

    titleBg;

    cardBg;

    wordArr = [];

    title;

    titleColor = "#5f1782";

    wordBg;


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

        this.data = data;

        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.initPic();

        this.initTile();

        this.initWord();
    }

    private initBg(w, h) {
        const bg = new ShapeRect();
        bg.setSize(w, h);
        bg.fillColor = "#f9f2d4";

        this.bg = bg;
    }

    initPic() {

        const cardBg = new MySprite();
        cardBg.init(this.getImageObj('card'));
        cardBg.scaleX = this.scaleX;
        cardBg.scaleY = this.scaleY;
        cardBg.x = this.bg.width / 2;
        cardBg.y = this.bg.height / 2;
        this.bg.addChild(cardBg);
        this.cardBg = cardBg;

        const titleBg = new MySprite();
        titleBg.init(this.getImageObj('title'));
        titleBg.scaleX = this.scaleX;
        titleBg.scaleY = this.scaleY;
        titleBg.x = this.bg.width / 2;
        titleBg.y = titleBg.getBoundingBox().height / 2 - 2 * this.scaleY;
        this.bg.addChild(titleBg);
        this.titleBg = titleBg;


    }

    initTile() {
        const title = new Label();
        title.text = this.data.val;
        title.textAlign = 'center';
        title.fontSize = 56;
        title.fontName = "ahronbd-1";
        title.fontColor = this.titleColor;
        title.setScaleXY(this.scaleX);
        title.refreshSize();
        title.setMaxSize(300 * this.scaleX);
        title.x = this.bg.width / 2;
        title.y = this.titleBg.getBoundingBox().height / 2 + 5 * this.scaleY;

        this.bg.addChild(title);

        this.title = title;
    }

    initWord() {

        let tw = 0;
        let h = 0
        for (let i = 0; i < this.data.wordArr.length; ++i) {
            let word = this.data.wordArr[i];
            let text = this.getWord(word);
            tw += text.getBoundingBox().width;
            h = text.getBoundingBox().height;
        }

        //let dw = 28 * this.scaleX;
        let dw = 28;

        tw += (this.data.wordArr.length - 1) * dw;

        let scale = this.scaleX;

        if (tw >= this.bg.width - 20 * this.scaleX) {
            scale = this.bg.width / tw;
        }

        let wordBg = new ShapeRect();
        wordBg.setSize( tw, h);
        wordBg.setScaleXY(scale);
        wordBg.alpha = 0;
        wordBg.x = (this.bg.width - wordBg.getBoundingBox().width) / 2;
        wordBg.y = this.bg.height - 30 * this.scaleY - wordBg.getBoundingBox().height;
        this.bg.addChild(wordBg);
        this.wordBg = wordBg;
        //let x = (this.bg.width - tw) / 2;
        let x = 0;

        for (let i = 0; i < this.wordArr.length; ++i) {

            let text = this.wordArr[i];

            let box = text.getBoundingBox();

            text.x = x + dw * i;

            x += box.width;
            text.y = box.height / 2;

            const textBg = new ShapeRect();
            textBg.setSize(box.width, box.height);
            textBg.x = text.x;
            textBg.y = text.y - box.height / 2;
            textBg.alpha = 0;
            //this.bg.addChild(textBg);
            wordBg.addChild(textBg);
            text.textBg = textBg;

            if(text.data.isFill == '1'){
                let underLine = new Line();
                underLine.lineColor = "#949494";
                underLine.lineWidth = 3;
                underLine.y = text.y + box.height / 2;
                underLine.x = text.x;
                underLine.setLine(box.width);
                text.underLine = underLine;
                //this.bg.addChild(underLine);
                wordBg.addChild(underLine);
                text.alpha = 0;
            }
            
            //this.bg.addChild(text);
            wordBg.addChild(text);
        }
    }

    private getWord(val) {
        let text = new Label2();
        text['data'] = val;
        text.fontName = "MMB";
        let fontSize = 38;
        text.fontSize = fontSize;

        let textArr = new Array<LabelText>();
        text.textArr = textArr;

        for (let i = 0; i < val.letterArr.length; ++i) {

            var letter = val.letterArr[i];

            let labelText = new LabelText();
            labelText.val = letter.val;

            labelText.fontColor = letter.fontColor;

            textArr.push(labelText);
        }

        //text.scaleX = this.scaleX;
        //text.scaleY = this.scaleY;
        text.refreshSize();

        this.wordArr.push(text);

        return text;
    }

    getImageObj(key) {
        return this.images.get(key);
    }

    resetScale(titleScale, wordScale){

        this.title.setScaleXY(titleScale);

        this.wordBg.setScaleXY(wordScale);
        this.wordBg.y = this.bg.height - 30 * this.scaleY - this.wordBg.getBoundingBox().height;
        this.wordBg.x = (this.bg.width - this.wordBg.getBoundingBox().width) / 2;
    }
}

export class EexercisesA extends Eexercises {

    getImageObj(key) {
        return this.images.get(key + "_a");
    }


}

export class EexercisesB extends Eexercises {

    getImageObj(key) {
        return this.images.get(key + "_b");
    }


}