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

//枪的类型数据
@ccclass('GunTypeItem')
export class GunTypeItem extends ListItem {
    @property({ type: SpriteFrame, tooltip: "枪类型解锁选中的" })
    public selected_bg: SpriteFrame;
    @property({ type: SpriteFrame, tooltip: "枪类型未解锁选中的" })
    public gary_selected_bg: SpriteFrame;
    @property({ type: SpriteFrame, tooltip: "枪类型解锁的背景" })
    public unlock_bg: SpriteFrame;
    @property({ type: SpriteFrame, tooltip: "枪类型解锁的背景" })
    public not_unlock_bg: SpriteFrame;
    @property({ type: Sprite, tooltip: "枪类型的背景图片" })
    public gun_type_bg: Sprite;
    @property({ type: Node, tooltip: "枪类型解锁" })
    public gou_type_icon: Node;

    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;
        //设置背景图片（根据解锁状态切换）
        if(data.selected){
            this.gun_type_bg.spriteFrame = data.unlocked ? this.selected_bg : this.gary_selected_bg;
        }else{
            this.gun_type_bg.spriteFrame = data.unlocked 
                ? this.unlock_bg 
                : this.not_unlock_bg;
        }
        //设置类型图标（需要确保data包含type_icon字段）
        const icon: string = data.unlocked ? data.type_not_unlock : data.type_unlock;
        ResUtil.setSpriteFrame(icon, this.gou_type_icon);
    }
}