import { _decorator, Node, Label, Sprite, EventTouch, Tween, tween, Vec3} from 'cc';
import { BaseExp } from '../core/base/BaseExp';
import List from '../third/List';
import { autoBind } from '../extend/AutoBind';
import { userIns } from '../data/UserData';
import MsgHints from '../utils/MsgHints';
import { Constants } from '../data/Constants';
import { stateMgr } from '../core/manager/StateManager';
import { UpgradeItem } from '../items/item/UpgradeItem';
import { uiMgr } from '../core/manager/UIManager';
import { ResUtil } from '../utils/ResUtil';
import i18n from '../core/i18n/runtime-scripts/LanguageData';
const { ccclass, property } = _decorator;

@ccclass('SuccessUI')
export class SuccessUI extends BaseExp {
    @autoBind({ type: Node, tooltip: "旋转光节点" })
    public rotation_light: Node;
    @autoBind({ type: Sprite, tooltip: "获得的物品图片" })
    public obtain_good: Sprite;
    @autoBind({ type: Sprite, tooltip: "获得的物品图片缩影" })
    public good_sy_sp: Sprite;
    @autoBind({ type: Label, tooltip: "获得的物品的名字" })
    public name_lable: Label;
    @autoBind({ type: Label, tooltip: "描述文字" })
    public dec_lable: Label;
    @autoBind({ type: Node, tooltip: "底部提交确认按钮" })
    public confim_btn: Node;
    @autoBind({ type: Node, tooltip: "拥有状态" })
    public owned_lable: Node;

    //奖励的数据
    private data: any = null;
    //奖励的id
    private id: string = '';
    //回调函数
    private cb:Function = null;

    start() {
        this.closeOnBlank = false;
    }
    
    public show(...args: any[]){
        this.id = args[0];
        this.cb = args[1];
        this.hasAnim = true;
        //加载数据ui
        this.ui();
    }

    /**
     * 加载数据ui
     */
    public ui(){
        //获得的光圈一直旋转
        Tween.stopAllByTarget(this.rotation_light);
        tween(this.rotation_light)
            .by(2, { eulerAngles: new Vec3(0, 0, -360) })
            .repeatForever()
            .start();

        this.data = userIns.boomerangTable.find(e=>e.id == this.id);
        //设置图片
        ResUtil.setSpriteFrame(this.data.icon,this.obtain_good);
        //设置图片缩影
        ResUtil.setSpriteFrame(this.data.s_icon,this.good_sy_sp);
        //设置名字
        this.name_lable.string = i18n.isZh ? this.data.name : this.data.name_lang;
        //设置描述
        this.dec_lable.string = i18n.isZh? this.data.description : this.data.dec_lang;
        const [gun_id,num] = this.data.quantity.split('_');
        if(gun_id == 1001 || gun_id == 1002){//金币和钻石
            this.owned_lable.active = false;
            this.confim_btn.active = false;
            //是金币和钻石自动关闭
            this.scheduleOnce(()=>{
               uiMgr.hide(Constants.popUIs.obtainUI,this.cb);
            },2);
        }else{
            this.confim_btn.active = true;
            //是解锁的枪 这儿需要判断是否已经拥有的了这把枪 如果已经拥有了这把枪 就转换成钻石和金币
            if(userIns.isHasGun(gun_id)){//拥有这把枪转换成金币
               this.owned_lable.active = true;
               const [good_id,amount] = this.data.value.split('_');
               const sData:any = userIns.itemTable.find(e=>e.id == good_id);
               this.dec_lable.string = `Already owned ${this.data.name_lang}, converted ${amount} ${sData.name_lang}`;
            }else{
                this.owned_lable.active = false;
            }
        }
    }


    /**
     * 按钮点击事件
     * @param event 事件
     * @param param 参数
     */
    override onBtnClicked(event:EventTouch, param:any) {
        super.onBtnClicked(event, param);
        let btnName = event.target.name;
        if(btnName === 'confim_btn' 
            || btnName === 'close_btn'){
            this.unscheduleAllCallbacks();
            uiMgr.hide(Constants.popUIs.obtainUI,this.cb);
        }
    }
}

