import { Item } from './Item';

import { Course } from '../form/Course';
import { ShapeRect, MySprite, RichText } from './Unit';
import { AudioComponent } from './AudioUtil';

export class Title extends Item {

    private _data;

    bg: ShapeRect;

    audio: AudioComponent;

    set data(data) {
        this._data = data;
    }
    get data() {
        return this._data;
    }

    init(data) {

        this._data = data;

        if (!this._data) {
            this._data = new Course();
            this._data.type = '01';
        }

        this.initBg();
    }

    private initBg() {

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

        if (this._data.type === '01') {
            this.initText();
        }
        else if (this._data.type === '02') {
            this.initAudio();
        }
        else {
            this.initPic();
        }
    }

    private initAudio() {
        let width = 528 * this.scaleX;
        let height = 121 * this.scaleY;
        this.bg.setSize(width, height);

        const audio = new AudioComponent(this.images, this.audioObj, this.scaleX, this.scaleY);
        audio.init(width, height);
        audio.bg.x = 0;
        audio.bg.y = 0;
        this.audio = audio;
        this.audio.setAudio(this.audioObj[this._data.audioUrl]);
        this.bg.addChild(audio.bg);
    }

    private initPic() {
        let width = 777 * this.scaleX;
        let height = 158 * this.scaleY;
        this.bg.setSize(width, height);

        const picBg = new MySprite();
        picBg.init(this.images.get('pic_bg'), 0, 0);
        picBg.scaleX = this.scaleX;
        picBg.scaleY = this.scaleY;
        picBg.x = 0;
        picBg.y = 0;
        this.bg.addChild(picBg);

        const picBg2 = new ShapeRect();
        picBg2.setSize(748 * this.scaleX, 115 * this.scaleY);
        picBg2.alpha = 0;
        picBg2.x = (this.bg.width - picBg2.width) / 2;
        picBg2.y = 33 * this.scaleY;
        this.bg.addChild(picBg2);

        const setScale = (pW, pH, child) => {
            let width = child.width;
            let height = child.height;
            let sx = pW / width;
            let sy = pH / height;

            let scale = Math.min(sx, sy);
            if (scale < 1) {
                child.setScaleXY(scale);
            }
        }

        const getPic = (par: ShapeRect, picUrl) => {
            const pic = new MySprite();
            pic.init(this.images.get(picUrl));
            pic.x = par.width / 2;
            pic.y = par.height / 2;

            setScale(par.width, par.height, pic);

            return pic;
        };

        const pic = getPic(picBg2, this.data.picUrl);
        picBg2.addChild(pic);

    }

    private initText() {
        let width = 730 * this.scaleX;
        let height = 150 * this.scaleY;
        this.bg.setSize(width, height);

        const textBg = new MySprite();
        textBg.init(this.images.get('text_bg'), 0, 0);
        textBg.scaleX = this.scaleX;
        textBg.scaleY = this.scaleY;
        textBg.x = 0;
        textBg.y = 0;
        this.bg.addChild(textBg);

        const textBg2 = new ShapeRect();
        textBg2.setSize(707 * this.scaleX, 106 * this.scaleY);
        textBg2.alpha = 0;
        textBg2.x = (this.bg.width - textBg2.width) / 2;
        textBg2.y = 31 * this.scaleY;
        this.bg.addChild(textBg2);

        let text = new RichText();
        text.text = this.data.text;
        text.fontColor = "#c57500";
        text.fontSize = 34;
        text.fontName = "BRLNSDB";
        text.disH = 0;
        text.textAlign = 'middle';
        text.scaleX = this.scaleX;
        text.scaleY = this.scaleY;
        text.width = textBg2.width - 36 * this.scaleX;
        text.refreshSize();
        text.x = 18 * this.scaleX;
        text.y = textBg2.height / 2;
        textBg2.addChild(text);
    }
}