﻿// @Author 空伊伊
// @论坛主页 https://forum.ark.online/home.php?mod=space&uid=113&do=thread&view=me&from=space

export class RollingTips {

    // 单例
    private static _insatnce: RollingTips
    public static get instance() {
        if (RollingTips._insatnce == null) {
            RollingTips._insatnce = new RollingTips()
        }
        return RollingTips._insatnce
    }

    private _uiWidget: UserWidget
    private _rootCanvas: Canvas
    private _textBlockArray: RollingTextBlock[] = []

    private leftX: number = 50
    private bottomY: number = 1080 - 300


    /**创建一个文本框 */
    private createText() {
        // 首次创建，如果没有UI对象，就创建一个
        if (!this._uiWidget) {
            // 创建一个UI对象
            this._uiWidget = UserWidget.newObject();
            this._uiWidget.addToViewport(1)
            // 首次创建，如果没有rootCanvas，就创建一个
            if (!this._rootCanvas) {
                this._rootCanvas = Canvas.newObject()
                this._rootCanvas.size = new Vector2(1920, 1080);
                this._rootCanvas.position = Vector2.zero
                this._uiWidget.rootContent = this._rootCanvas
            }
        }
        let textBlock = new RollingTextBlock(this._rootCanvas)
        this._textBlockArray.push(textBlock)
        return textBlock
    }

    /**展示一条提示 */
    public showTip(str: string) {
        let textBlock: RollingTextBlock = null
        for (let text of this._textBlockArray) {
            if (text.isFinish) {
                textBlock = text
                break
            }
        }
        if (textBlock == null) {
            textBlock = this.createText()
        }
        for (let text of this._textBlockArray) {
            if (!text.isFinish) {
                text.up()
            }
        }
        textBlock.textBlock.position = new Vector2(this.leftX, this.bottomY)
        textBlock.show(str)
    }

    /**获得一个随机颜色值 */
    private getRandomHexColor(): string {
        const randomValue = Math.floor(Math.random() * 16777215); // 0xFFFFFF
        return `#${randomValue.toString(16).padStart(6, '0')}`;
    }

    /**播报测试 */
    public testTips() {
        let color = RollingTips.instance.getRandomHexColor()
        RollingTips.instance.showTip(`<color=${color}>随机播报：</color>` + Math.random().toFixed(5))
    }

}

class RollingTextBlock {

    public textBlock: TextBlock
    public isFinish: boolean = true

    constructor(canvas: Canvas) {
        // 创建一个文本框，并添加到画布上
        this.textBlock = TextBlock.newObject(canvas)
        this.textBlock.size = new Vector2(500, 50)
        this.textBlock.isRichText = true
        this.textBlock.fontSize = 30
        this.textBlock.fontColor = LinearColor.white
        this.textBlock.textAlign = TextJustify.Left
        this.textBlock.outlineColor = LinearColor.black
        this.textBlock.outlineSize = 2
        this.textBlock.renderOpacity = 0
    }

    public show(str: string) {
        this.textBlock.text = str
        const duration = 5000 // 持续时间（毫秒）
        let percent = 0
        this.isFinish = false
        let inter = setInterval(() => {
            this.textBlock.renderOpacity = this.getOpacityByPercent(percent)
            percent++
            if (percent >= 100) {
                clearInterval(inter)
                this.isFinish = true
                this.textBlock.renderOpacity = 0
            }
        }, duration / 100);
    }

    public up() {
        this.textBlock.position = this.textBlock.position.clone().add(new Vector2(0, -50))
    }

    private getOpacityByPercent(percent: number) {
        // 前10% 渐显
        if (percent <= 10) {
            return percent / 10
            // 中间70% 用来显示            
        } else if (percent <= 80) {
            return 1
            // 后20% 渐隐
        } else if (percent <= 100) {
            return (100 - percent) / 20
        } else {
            return 0
        }
    }

}