// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NumberToImage extends cc.Component {

    @property(cc.SpriteAtlas)
    fontUI: cc.SpriteAtlas = null;

    static font: any = null;
    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        if (this.fontUI != null) {
            NumberToImage.font = this.fontUI;
        }
    }

    //第一个参数  数字， 第二个参数  数字间距  第三个参数 占位宽度  第四个参数 目标节点
    static numberToImageNodes(number, width, posX, name, targetNode: cc.Node, middle: boolean = false) {
        const numStr = number.toString();
        let cha = 0;
        if (number > 99) cha = -posX
        else if (number < 10) cha = posX
        if (targetNode.children.length > 0)
            targetNode.removeAllChildren();
        for (let i = 0; i < numStr.length; i++) {
            const digit = parseInt(numStr[i], 10);
            const node = new cc.Node();
            const sprite = node.addComponent(cc.Sprite);
            // debugger;
            sprite.spriteFrame = this.font._spriteFrames[name + digit + ""];
            // 将节点添加到目标节点下
            node.x = i * width + cha;
            node.parent = targetNode;
        }
        if (middle) {
            targetNode.x = targetNode.x - targetNode.children.length * (width - 15) / 2;
        }
    }




    static getTimeMargin(number, width, name, targetNode: cc.Node) {
        let timeArr = [];
        let total = 0;
        total = number;
        let hour = 0;
        hour = parseInt((total / 3600) + "");//计算整数小时数
        let afterHour = total - hour * 60 * 60;//取得算出小时数后剩余的秒数
        let min = parseInt((afterHour / 60) + "");//计算整数分
        let m = "" + min;

        if (min < 10) m = "0" + min;
        let afterMin = total - hour * 60 * 60 - min * 60;//取得算出分后剩余的秒数
        let miao = afterMin + "";

        if (afterMin < 10) miao = "0" + afterMin;

        let result = m + miao;
        for (let i = 0; i < result.length; i++) {
            const digit = parseInt(result[i], 10);
            timeArr.push(digit);
        }

        if (targetNode.children.length > 0) {
            for (let i = 0; i < targetNode.children.length; i++) {
                targetNode.children[i].getComponent(cc.Sprite).spriteFrame = this.font._spriteFrames[name + timeArr[i] + ""];
            }
        }
        else {
            for (let i = 0; i < 4; i++) {
                const node = new cc.Node();
                const sprite = node.addComponent(cc.Sprite);
                const digit = timeArr[i];
                // debugger;
                sprite.spriteFrame = this.font._spriteFrames[name + digit + ""];
                // 将节点添加到目标节点下
                node.x = i * width;
                if (i > 1) node.x += 35;
                node.parent = targetNode;
            }
        }

    }

    // static calculateAndConvert(num1: number, num2: number, operator: '+' | '-' | '*' | '/', spriteFrames: SpriteFrame[]): Node[] {
    //     let result: number;
    //     switch (operator) {
    //         case '+':
    //             result = num1 + num2;
    //             break;
    //         case '-':
    //             result = num1 - num2;
    //             break;
    //         case '*':
    //             result = num1 * num2;
    //             break;
    //         case '/':
    //             result = num1 / num2;
    //             break;
    //         default:
    //             throw new Error('不支持的运算符');
    //     }

    //     // 处理结果为小数的情况，这里简单取整
    //     result = Math.floor(result);
    //     return this.numberToImageNodes(result, spriteFrames);
    // }

    // update (dt) {}
}
