import { _decorator, Node, Label, EventTouch} from 'cc';
import { BaseExp } from '../core/base/BaseExp';
import List from '../third/List';
import { autoBind } from '../extend/AutoBind';
import { userIns } from '../data/UserData';
import { BoomerangItem } from '../items/item/BoomerangItem';
import { uiMgr } from '../core/manager/UIManager';
import { Constants } from '../data/Constants';
import { TossBoomerangUI } from './TossBoomerangUI';
import MsgHints from '../utils/MsgHints';
import { ITEM_TYPE, ResUtil } from '../utils/ResUtil';
import { audioMgr } from '../core/manager/AudioManager';
const { ccclass, property } = _decorator;

//购买飞镖
@ccclass('BuyBoomerangUI')
export class BuyBoomerangUI extends BaseExp {
    @autoBind({ type: List, tooltip: "飞镖列表" })
    public prop_scrollView: List;
    @autoBind({ type: Label, tooltip: "飞镖个数文本" })
    public boomerang_num_lable: Label;
    @autoBind({ type: Node, tooltip: "飞镖按钮" })
    public boomerang_btn: Node;

    //飞镖商店数据
    private dartDatas: any[] = [];

    public show(...args: any[]){
        this.hasAnim = true;
        this.closeOnBlank = false;
        const toss:TossBoomerangUI = uiMgr.getPageComponent(Constants.popUIs.tossBoomerangUI);
        if(toss && toss.shoot_btn){
           toss.shoot_btn.parent.active = false;
        }
        //设置飞镖个数
        this.boomerang_num_lable.string = userIns.data.boomerang;
        this.loadDartData();
    }

    /**
     * 飞镖商店数据
     */
    public loadDartData(){
        this.dartDatas = userIns.boomerangShopTable;
        this.prop_scrollView.numItems = this.dartDatas.length;
    }

    /**
     * 设置飞镖商店数据
     * @param item item节点
     * @param idx 数据信息
     */
    public setDartItemData(item: Node, idx: number) {
        let com:BoomerangItem = item.getComponent(BoomerangItem);
        com.init(this.dartDatas[idx],this.buyClicked.bind(this)); 
    }
    
    /**
     * 购买飞镖
     */
    public buyClicked(data:any,clikTarget:Node){
        const [id,num] = data.price_1.split("_");
        const sData:any = userIns.itemTable.find(e=>e.id == id);
        //是否升级
        let isSucced: boolean = false;
        if(sData.id === 1001){//金币钻石
            if(userIns.data.gold < num){
                MsgHints.show("Insufficient gold!");
            }else{
                userIns.data.gold -= num;
                isSucced = true;
            }
        }else if(sData.id === 1002){//钻石
            if(userIns.data.diamond < num){
                MsgHints.show("Insufficient diamonds!");
            }else{
                userIns.data.diamond -= num;
                isSucced = true;
            }
        }
        if(isSucced){
            audioMgr.playOneShot(Constants.audios.buy);
            ResUtil.flyAnim(ITEM_TYPE.Boomerang, clikTarget, this.boomerang_btn, data.quantity, 50,(b) => {});
            userIns.data.boomerang += data.quantity;
            //设置飞镖个数
            this.boomerang_num_lable.string = userIns.data.boomerang;
            const toss:TossBoomerangUI = uiMgr.getPageComponent(Constants.popUIs.tossBoomerangUI);
            if(toss){
                toss.setBoomerang();
            }
        }
    }

    /**
     * 按钮点击事件
     * @param event 事件
     * @param param 参数
     */
    override onBtnClicked(event:EventTouch, param:any) {
        super.onBtnClicked(event, param);
        let btnName = event.target.name;
        if(btnName === 'close_btn'){
            const toss:TossBoomerangUI = uiMgr.getPageComponent(Constants.popUIs.tossBoomerangUI);
            if(toss && toss.shoot_btn){
               toss.shoot_btn.parent.active = true;
            }
            uiMgr.hide(Constants.popUIs.buyBoomerangUI);
        }
    }
}

