// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import LocalStorage from "../../Data/LocalStorage";
import ShopSkin from "./ShopSkin";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ItemSkin extends cc.Component {

    id: number = 0;

    @property(cc.Node)
    lock: cc.Node = null;

    @property(cc.Sprite)
    worm: cc.Sprite = null;
    
    @property(cc.Node)
    selected: cc.Node = null;

    @property(cc.Node)
    iconChecked: cc.Node = null;
    
    isLock: boolean = true;
    isSlected: boolean = true;
    skinWorm: cc.SpriteFrame = null;

    shop: ShopSkin = null;

    updateSkin(data, shopSkin: ShopSkin){
        this.shop = shopSkin;
        this.skinWorm = data.skin;
        this.id = data.id;
        this.worm.spriteFrame = data.skin;
        this.isSlected = data.isSelected;
        this.isLock = data.isLock;
        if (!this.isLock) {
            this.lock.active = true;
            this.selected.active = false;
            this.iconChecked.active = false;
        }
        if (data.isSelected) {
            this.selected.active = true;
        }
    }

    onSelected(){
        this.shop.disableSelected();
        this.shop.idClick = this.id;
        this.shop.showSkin.spriteFrame = this.skinWorm;
        this.shop.scaleWorm();
        this.selected.active = true;
        if (!this.isLock) {
            this.shop.btnEquip.active = false;
            this.shop.btnBuy.active = true;
        }else{
            this.shop.btnEquip.active = true;
            this.shop.btnBuy.active = false;
            if (this.isSlected) {
                this.shop.txtEquip.string = "Equipped";
            }else{
                this.shop.txtEquip.string = "Equip";
            }
        }
    }

    openWorm(){
        this.shop.disableChecked();
        this.isSlected = true;
        this.isLock = true;
        this.selected.active = true;
        this.shop.showSkin.spriteFrame = this.skinWorm;
        this.shop.scaleWorm();
        this.shop.btnEquip.active = true;
        this.shop.btnBuy.active = false;
        this.iconChecked.active = true;
        this.lock.active = false;
        this.shop.txtEquip.string = "Equipped";
        LocalStorage.setWormActive(this.id);
        LocalStorage.setWormSelected(this.id);
    }


    onActiveSelected(isActive: boolean){
        this.selected.active = isActive;
    }

    onActiveChecked(isActive: boolean){
        this.iconChecked.active = isActive;
    }

}
