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

//枪的数据
@ccclass('GunItem')
export class GunItem extends ListItem {
    @property({ type: SpriteFrame, tooltip: "解锁的背景" })
    public unlock_bg: SpriteFrame;
    @property({ type: SpriteFrame, tooltip: "解锁的背景" })
    public not_unlock_bg: SpriteFrame;
    @property({ type: Sprite, tooltip: "枪的背景图片" })
    public gun_bg: Sprite;
    @property({ type: Node, tooltip: "枪选中勾选" })
    public gou_icon: Node;
    @property({ type: Node, tooltip: "枪选中勾选父节点" })
    public gou_n: Node;
    @property({ type: Node, tooltip: "枪选未解锁图片" })
    public lock_n: Node;
    @property({ type: Node, tooltip: "设置枪的图片" })
    public gun_sp: Node;
    @property({ type: Label, tooltip: "枪的名字" })
    public gun_name_lable: 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;
            if(!this.data.unlocked){
                MsgHints.show(`${this.data.name_lang} not unlocked!`);
                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;
        //设置解锁状态 未解锁时显示锁图标
        this.lock_n.active = !data.unlocked;
        //已解锁时显示勾选父节点
        this.gou_n.active = data.unlocked;

        //设置枪械背景 根据解锁状态使用不同背景图
        this.gun_bg.spriteFrame = data.unlocked ? this.unlock_bg : this.not_unlock_bg;
        //枪的图片
        const icon: string = data.unlocked ? data.gun_unlock_icon : data.gun_not_unlock_icon;
        ResUtil.setSpriteFrame(icon, this.gun_sp);
        //设置选中状态（根据当前使用枪械ID判断）
        if(data.unlocked){
           this.gou_icon.active = data.selected;
        }
        //枪的名字
        this.gun_name_lable.string = data.name_lang;
    }
}