/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-04-18 19:11:51
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-04-27 18:52:48
 * @Description: 福利一
 */
import { _decorator, Animation, instantiate, Node, Prefab } from 'cc';
import { oops } from 'db://oops-framework/core/Oops';
import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil';
import VMParent from 'db://oops-framework/libs/model-view/VMParent';
import { UIID } from '../../common/config/GameUIConfig';
import { smc } from '../../common/SingletonModuleComp';
import { LoadBricsItem } from '../LoadBricsItem';
import { ServerHandler } from '../../common/manager/ServerHandler';
const { ccclass, property } = _decorator;

interface LevelInfo {
    level: number;
    eventType: string;
    withdraw: boolean;
    position?: number; //位置
    isFirstNotWithdraw?: boolean; //是否第一个没提现
}


@ccclass('WelfareOne')
export class WelfareOne extends VMParent {
    data: any = {
        itemList: [],
        frequency: 5,
        cur_num: 1,
        total_num: 12,
        wealCount: 0
    }

    @property(Node)
    private progressNode: Node = null!;


    @property(Node)
    private showNode1: Node = null!;  //福利一

    @property(Node)
    private showNode2: Node = null!;  //自动提现进度

    @property(Prefab)
    itemPrefab: Prefab = null!;

    @property(Node)
    itemList: Node = null!;


    start() {
        //三秒后进自动提现进度条
        this.showNode1.active = false;
        this.showNode2.active = false;
        this.showWelfareOne();
        this.updateData();
    }



    //展示福利一界面
    showWelfareOne() {
        this.showNode1.active = true;
        //
        this.scheduleOnce(() => {
            this.showNode1.active = false;
            this.showLoadBar();
        }, 3)
    }


    //更新数据
    updateData() {
        if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
            if (smc.game.GameModel.loadbarInfo.levelProgress) {
                this.data.itemList = smc.game.GameModel.loadbarInfo.levelProgress.levelInfoList
                //进度
                this.data.cur_num = smc.account.AccountModel.curLevel;
                this.data.total_num = this.data.itemList[this.data.itemList.length - 1].level;
                const itemInfoList = this.processLevelInfo(this.data.itemList);
                this.itemList.destroyAllChildren();
                itemInfoList.forEach((item, index) => {
                    const loadLevelItem = instantiate(this.itemPrefab)
                    this.itemList.addChild(loadLevelItem);
                    if (item.position) {
                        loadLevelItem.setPosition(item.position, 0);
                        //设置数据
                        const script = loadLevelItem.getComponent(LoadBricsItem);
                        if (script) {
                            script.updateData(item);
                        }
                    }
                })
            }
        }
    }





    //展示进度条界面
    showLoadBar() {
        this.showNode2.active = true;
        const animation = this.showNode2.getComponent(Animation);
        if (animation) {
            animation.play();
        }
        //3秒后关闭
        this.scheduleOnce(() => {
            // this.updateState();
            //展示解锁
            oops.gui.open(UIID.UnlockFunction);
            oops.gui.remove(UIID.WelfareOne);
        }, 3)
    }

    processLevelInfo(levelInfoList: LevelInfo[], totalLength: number = 602): LevelInfo[] {
        if (levelInfoList.length === 0) return [];
        const firstLevel = levelInfoList[0].level;
        const lastLevel = levelInfoList[levelInfoList.length - 1].level;
        //单段距离 总长度/最后一个level-1
        const singleLength = totalLength / (lastLevel - 1);
        return levelInfoList.map((item, index) => {
            let foundFirstNotWithdraw = false;
            if (index === 0) {
                item.position = this.progressNode.position.x;
            } else if (index === levelInfoList.length - 1) {
                item.position = this.progressNode.position.x + totalLength;
            } else {
                item.position = this.progressNode.position.x + ((item.level - firstLevel) * singleLength);
            }

            // 标记第一个未提现
            if (!foundFirstNotWithdraw && item.withdraw) {
                item.isFirstNotWithdraw = true;
                foundFirstNotWithdraw = true;
            } else {
                item.isFirstNotWithdraw = false;
            }


            return item;
        });
    }




}


