import { _decorator, Sprite, Label, ProgressBar, EventTouch,Node, utils } from 'cc';
import ListItem from '../../third/ListItem';
import { ResUtil } from '../../utils/ResUtil';
import { userIns } from '../../data/UserData';
const { ccclass, property } = _decorator;

//枪的属性数据
@ccclass('UpgradeItem')
export class UpgradeItem extends ListItem {
    @property({ type: Sprite, tooltip: "枪的属性图片" })
    public attr_sp: Sprite;
    @property({ type: ProgressBar, tooltip: "属性值的最大进度" })
    public progressBar: ProgressBar;
    @property({ type: Label, tooltip: "属性值的数据文本" })
    public atts_label: Label;
    @property({ type: Label, tooltip: "枪的属性多语言名字" })
    public attribute_lang_lable: Label;

    @property({ type: Node, tooltip: "广告按钮" })
    public ad_free_btn: Node;
    @property({ type: Node, tooltip: "视频按钮" })
    public video_btn: Node;
    @property({ type: Node, tooltip: "金币和钻石按钮" })
    public prop_btn: Node;
    @property({ type: Node, tooltip: "最大等级按钮" })
    public max_btn: Node;

    public data: any = null;
    public cb: Function = null;

    public start() {
        this.ad_free_btn.active = false;
        this.video_btn.active = false;
        this.prop_btn.active = true;
        this.max_btn.active = false;
    }

    /**
     * 点击了按钮
     */
    public onBtnClicked(event:EventTouch, param:any){
        if(!this.data)return;
        let idx: number = Number(param);
        this.cb?.(this.data,idx,event.target);
    }

    /**
     * 数据填充
     * @param data 数据
     * @param cb 回调
     */
    public  init(data: any,cb?: Function){
        if(!data)return;
        this.data = data;
        this.cb = cb;
        //设置属性图标
        ResUtil.setSpriteFrame(data.attr_icon, this.attr_sp);
        //设置进度条
        this.progressBar.progress = data.attr_value / data.totalValue;
        //设置属性数值文本
        this.atts_label.string = `${data.attr_value}/${data.totalValue}`;
        //设置多语言属性名称
        this.attribute_lang_lable.string = data.attribute_lang;
        //升级需要消耗的材料
        const parts:string[] = data.nextExpenditure.split("_");
        const stuff:any = userIns.itemTable.find(e=>e.id == parts[0]);
        if(stuff){
            //设置材料图标
            ResUtil.setSpriteFrame(stuff.icon, this.prop_btn.getChildByName("icon"));
            //设置材料数量文本
            this.prop_btn.getChildByName("num_lable").getComponent(Label).string = `x${parts[1]}`;
        }
        //判断是否可以升级
        this.prop_btn.active = !data.isMaxLevel;
        this.max_btn.active = data.isMaxLevel;
    }
}