import { _decorator, Vec3,Node, Sprite, Label, EventTouch, tween, easing } from 'cc';
import ListItem from '../../third/ListItem';
import { ResUtil } from '../../utils/ResUtil';
const { ccclass, property } = _decorator;

//任务数据
@ccclass('TaskItem')
export class TaskItem extends ListItem {
    @property({ type: Sprite, tooltip: "任务敌人头像" })
    public enemy_header: Sprite;
    @property({ type: Label, tooltip: "敌人数量" })
    public num_label: Label;
    @property({ type: Label, tooltip: "敌人名字" })
    public enemy_name_label: Label;

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

    public start() {
        this.node.on(Node.EventType.TOUCH_START,this.touchStart,this);
        //点击 物品查看
        this.node.on(Node.EventType.TOUCH_END,()=>{
            if(!this.data)return;
            this.cb?.(this.data);
        },this);
    }


    /**
     * 点击
     */
    private touchStart(event: EventTouch) {
        if(!this.data) return;
        let target: Node = event.target;
        tween(target)
        .to(0.1, {scale: new Vec3(1.02,1.02,1.02)},{easing: easing.backOut})
        .to(0.1, {scale: new Vec3(0.98,0.98,0.98)},{easing: easing.backOut})
        .call(() => {
            target.scale = new Vec3(1,1,1);
        }).start();
    }

    /**
     * 数据填充
     * @param data 敌人数据
     * @param cb 回调
     */
    public  init(data: any,cb?: Function){
        if(!data)return;
        this.data = data;
        this.cb = cb;
        //敌人头像
        ResUtil.setSpriteFrame(data.headshot,this.enemy_header);
        //敌人数量
        this.num_label.string = `x${data.count}`;
        //敌人名字
        this.enemy_name_label.string = data.name_lang;
    }
}