import { Widget, _decorator, Node, tween, v3, Label, Vec2, v2, math, Tween } from 'cc';
import { GameMgr } from '../manager/GameMgr';
import { AudioMgr } from '../manager/AudioMgr';
import { CoinMgr } from '../manager/CoinMgr';
import { ArrayUtil } from '../util/ArrayUtil';
import { InputBlock } from '../misc/InputBlock';
import { TimeMgr } from '../manager/TimeMgr';
import { UI } from '../enum/UI';
import { UIBase } from '../scriptBase/UIBase';
import { CoinItem } from '../uiItem/CoinItem';
import { Global } from '../Global';
import { TransMgr } from '../manager/TransMgr';
import { BYTEDANCE } from 'cc/env';
import { ModeName } from '../enum/Mode';
import { AdMgr } from '../manager/AdMgr';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('UI/UIWin')
@requireComponent(Widget)
export class UIWin extends UIBase {

    private pointer: Node = null
    private lbCoin: Label = null
    private coinItem: CoinItem = null

    private coin: number = 0
    public set Coin(v: number) {
        this.coin = Math.max(v, 0)
        this.lbCoin.string = `x${this.coin}`
    }

    private multi: number = 1

    private readonly posArr: Vec2[] = [
        v2(-120, -74),
        v2(-72, 80),
        v2(-17, 29),
        v2(32, 86),
        v2(90, 120),
    ]

    private readonly multiArr: number[] = [2, 3, 5, 3, 4]

    private tw: Tween<Node> = null

    protected onLoad(): void {
        // this.pointer = this.findNode('Pointer')
        // this.lbCoin = this.findComp('LbCoin', Label)
        // this.coinItem = this.findComp('CoinItem', CoinItem)
    }

    public onOpen(data?: unknown): void {
        AudioMgr.stopBgm()
        AudioMgr.playSfx('通关成功')

        GameMgr.passGame()

        // this.coinItem.init(CoinMgr.CurCoin)

        GameMgr.Pause = true

        const duration: number = 0.75
        // this.tw = tween(this.pointer).sequence(
        //     tween(this.pointer).to(duration, { position: v3(120, -25) }),
        //     tween(this.pointer).to(duration, { position: v3(-120, -25) })
        // ).repeatForever().start()

        // this.Coin = Global.Normal_Level_Coin_Default_Cnt + (GameMgr.CurLevel - 1) * Global.Normal_Level_Coin_Add_Cnt
        // CoinMgr.CurCoin += this.coin

    }

    public onClose(data?: unknown): void {
        GameMgr.Pause = false
        InputBlock.Active = false
    }

    protected async onBtnMultiClick() {
        const onSucc = async () => {
            this.tw.stop()
            const idx: number = ArrayUtil.pickItem([0, 0, 0, 0, 0, 1, 2, 3])
            this.multi = this.multiArr[idx]
            const posRange: Vec2 = this.posArr[idx]
            const posX: number = math.randomRangeInt(posRange.x, posRange.y)
            // this.pointer.setPosition(posX, this.pointer.position.y)

            const extraCoin: number = (this.multi - 1) * this.coin
            this.Coin = this.multi * this.coin

            CoinMgr.CurCoin += extraCoin
            // this.coinItem.updateCoin(CoinMgr.CurCoin)

            InputBlock.Active = true
            await TimeMgr.delay(2)
            await TransMgr.Inst.fadeIn()
            GameMgr.startGame()
            this.close()
            TransMgr.Inst.fadeOut()
        }

        AdMgr.showRewardedVideo(onSucc)
    }

    protected async onBtnNextClick() {
        // this.coinItem.updateCoin(CoinMgr.CurCoin)

        InputBlock.Active = true
        await TimeMgr.delay(2)
        await TransMgr.Inst.fadeIn()
        GameMgr.startGame()
        this.close()
        TransMgr.Inst.fadeOut()
    }

    protected async onBtnCloseClick() {
        await TransMgr.Inst.fadeIn()
        this.close()
        GameMgr.quitGame()
        this.open(UI.Main)
        TransMgr.Inst.fadeOut()
    }

}


