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

export class Exercise {

    exercise;

    _listeningAudio;

    answers = [];

    constructor(exercise) {
        this.exercise = exercise;
    }

    addAnswer(answer: Answer) {
        this.answers.push(answer);
    }

    set listeningAudio(audio) {
        this._listeningAudio = audio;
    }

    get listeningAudio() {
        return this._listeningAudio;
    }

    reset() {
        for (let i = 0; i < this.answers.length; ++i) {
            this.answers[i].reset();
        }
    }
}

export class Answer {

    bg;

    /**内容类型：T-文本，P-图片；默认为文本 */
    contentType;

    /**内容 */
    contentSprite: MySprite;

    answerContent;

    scaleX = 1;

    scaleY = 1;

    images = new Map();

    inited = false;

    right = false;

    selected = false;

    constructor(images, answerContent) {

        this.images = images;

        this.answerContent = answerContent;
    }

    setScaleXY(scale) {
        this.scaleX = this.scaleY = scale;
    }

    init() {

        if (this.inited) {
            return;
        }

        this.right = this.answerContent.right;

        const bg = new ShapeRect();
        bg.setSize(this.getWidth(), this.getHeight());
        bg.alpha = 0;
        this.bg = bg;

        this.setContent(bg);
    }

    /**
     * 设置答案卡片内容
     * @param parItem 
     */
    setContent(parItem) {

        //图片存在，则渲染图片
        if (this.answerContent.pic_url) {

            const content_pic = new MySprite();
            content_pic.init(this.images.get(this.answerContent.pic_url));
            content_pic.alpha = 1;

            let w = content_pic.width;

            let h = content_pic.height;

            let scale = Math.min((parItem.height - 15 * this.scaleX) / h, (parItem.width - 15 * this.scaleY) / w);
            //如果小于1,则缩放图片
            if (scale < 1) {
                content_pic.setScaleXY(scale);
            }

            content_pic.x = this.getWidth() / 2;
            content_pic.y = this.getHeight() / 2;
            parItem.addChild(content_pic);

            this.contentType = 'P';

            this.contentSprite = content_pic;
        }
        else {
            let content_val = new Label();
            content_val.text = this.answerContent.text;
            content_val.textAlign = 'middle';
            content_val.fontSize = 66;
            content_val.fontName = "MMTextBook-Bold";
            content_val.fontColor = "#FBEB4F";
            content_val.setOutline(5, '#315305');
            content_val.alpha = 0;

            content_val.refreshSize();

            content_val.setMaxSize(parItem.width - 60 * this.scaleX);

            content_val.x = (this.getWidth() - content_val.getBoundingBox().width) / 2;

            content_val.y = this.getHeight() / 2;

            parItem.addChild(content_val);

            this.contentType = 'T';

            this.contentSprite = content_val;

        }

    }

    getWidth() {
        return 234 * this.scaleX;
    }

    getHeight() {
        return 178 * this.scaleY;
    }

    /**
     * 从指定位置移动到新位置
     * @param sX 左上角x坐标
     * @param sY 左上角Y坐标
     * @param toX 左上角x坐标
     * @param toY 左上角Y坐标
     * @param callback 
     */
    moveIn(sX, sY, toX, toY, callback = null) {

        console.log(' answer move in ');
        if (this.contentType == 'T') {
            this.bg.x = toX;
            this.bg.y = toY;
            tweenChange(this.contentSprite, { alpha: 1 }, 0.4, callback);
        }
        else if (this.contentType == 'P') {
            this.contentSprite.alpha = 1;
            this.bg.x = sX;
            this.bg.y = sY;

            let newToX = toX;
            let newToY = toY;
            tweenChange(this.bg, { x: newToX, y: newToY }, 0.2, callback);
        }
    }

    /**
     * 从当前位置移到指定位置
     * @param toX 左上角x坐标
     * @param toY 左上角Y坐标
     * @param callback 
     */
    moveOut(toX, toY, callback = null) {

        console.log(' answer moveOut  ');

        if (this.contentType == 'T') {
            this.bg.x = toX;
            this.bg.y = toY;

            tweenChange(this.contentSprite, { alpha: 0 }, 0.3, callback);
        }
        else if (this.contentType == 'P') {

            this.bg.x = toX;
            this.bg.y = toY;
            tweenChange(this.contentSprite, {alpha: 0 }, 0.3, callback);
        }
    }

    reset() {
        this.selected = false;
    }
}

export function getDefaultExercisesItem() {
    let exercises = {
        audio_url: "",
        answers: []
    }

    for (let i = 0; i < 2; ++i) {
        let answer = getDefaultAnswer();
        exercises.answers.push(answer);
    }
    return exercises;
}

export function getDefaultAnswer() {
    let answer = {
        contentType: "T",
        text: "Hello",
        pic_url: "",
        right: true
    };

    return answer;
}