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

import JiaZai from "../../Script/JiaZai";
import Utils from "../../Script/module/Pay/Utils";
import NumberToImage from "../../Script/NumberToImage";
import { MiniGameSdk } from "../../Script/Sdk/MiniGameSdk";


const { ccclass, property } = cc._decorator;

@ccclass
export default class WinStreak extends cc.Component {

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

    @property(cc.Node)
    content: cc.Node = null;
    //月卡按钮切换
    @property(cc.Node)
    getBtn: cc.Node = null;

    public juwai = false;
    btn_Touch: boolean = false;


    onLoad() {
        this.btn_Touch = true;
    }



    start() {
        // this.init();
    }
    init() {
        if (cc.fx.GameConfig.GM_INFO.winStreak > 0) {
            let hammer = this.node.getChildByName("hammer").children;
            let count = cc.fx.GameConfig.GM_INFO.winStreak;
            let time = 0.1;
            if (count > 5) {
                time = 0.05;
            }
            if (count > 10) {
                count = 10;
            }
            for (let i = 0; i < count; i++) {
                if (i < 10) {
                    hammer[i].active = true;
                    hammer[i].scaleX = hammer[i].scaleY = 0;
                    cc.tween(hammer[i])
                        .delay(i * time)
                        .to(0.2, { scaleX: 0.7, scaleY: 0.7 })  // 快速放大到0.7
                        .to(0.1, { scaleX: 0.4, scaleY: 0.4 })  // 突然缩小制造弹性感
                        .to(0.1, { scaleX: 0.6, scaleY: 0.6 })  // 再次弹起到0.6
                        .to(0.1, { scaleX: 0.5, scaleY: 0.5 })  // 最终稳定到0.5
                        .to(0.05, { scaleX: 0.55, scaleY: 0.45 }) // X轴抖动效果
                        .to(0.05, { scaleX: 0.45, scaleY: 0.55 }) // Y轴抖动效果
                        .to(0.1, { scaleX: 0.5, scaleY: 0.5 })   // 恢复最终比例
                        .start();
                }
            }
        }
        this.numberToImageNodes2(cc.fx.GameConfig.GM_INFO.winStreak, 10, 5, "", this.node.getChildByName("number"), true);
    }
    //入场动画
    setAction() {
    }

    setReward(id) {
    }

    closeWinStreak() {
        this.node.active = false;
    }

    openLoad() {
        this.node.getChildByName("Loading").active = true;
        this.node.getChildByName("Loading").getChildByName("load").stopAllActions();
        this.node.getChildByName("Loading").getChildByName("load").runAction(cc.rotateTo(2, 1080).repeatForever());
    }

    openConfirmBox() {
        this.closeLoad();
        this.node.getChildByName("ConfirmBox").active = true;
    }
    closeConfirmBox() {
        this.node.getChildByName("ConfirmBox").active = false;
    }

    closeLoad() {
        this.node.getChildByName("Loading").active = false;
    }

    //点击去完成
    clickBtn(event, data) {
        this.node.active = false;
        const jiazaiNode = cc.find("Canvas"); // 假设 JiaZai 挂在 Canvas 节点
        const jiazaiComp = jiazaiNode.getComponent(JiaZai);
        if (jiazaiComp) {
            jiazaiComp.startGame();
        }
    }

    numberToImageNodes2(number, width, posX, name, targetNode: cc.Node, right: 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();

        const digitNodes: cc.Node[] = [];
        for (let i = 0; i < numStr.length; i++) {
            let digit = parseInt(numStr[i], 10);
            const node = new cc.Node();
            const sprite = node.addComponent(cc.Sprite);
            if (numStr[i] == ":") digit = 10;
            sprite.spriteFrame = this.fontUI._spriteFrames[name + digit + ""];
            digitNodes.push(node);
        }

        // 计算总宽度
        // const totalWidth = (numStr.length - 1) * width + (digitNodes[0]?.width || 0);
        // 计算总宽度，累加每个节点的宽度和间距
        let totalWidth = 0;
        for (let i = 0; i < digitNodes.length; i++) {
            totalWidth += digitNodes[i].width;
            if (i < digitNodes.length - 1) {
                totalWidth += width;
            }
        }
        if (right) {
            // 新右对齐逻辑：从右向左排列
            let currentX = 0; // 最右侧起点
            for (let i = digitNodes.length - 1; i >= 0; i--) {
                const node = digitNodes[i];
                node.x = currentX - node.width;
                currentX -= (node.width + width); // 向左累加宽度和间距
                if (targetNode) node.parent = targetNode;
            }
        } else {
            let currentX = cha;
            for (let i = 0; i < digitNodes.length; i++) {
                const node = digitNodes[i];
                node.x = currentX;
                currentX += node.width + width;
                if (targetNode) node.parent = targetNode;
            }
        }
    }

    update() {

    }
}
