import { _decorator, Component, Node, Skeleton, sp, Sprite, SpriteAtlas, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
import { GameConstant } from './GameConstant';
import { ResourceLoader } from './ResourceLoader';

@ccclass('Item')
export class Item extends Component {
    @property(Node)
    icon: Node = null;
    
    @property(Node)
    iconSpine: Node = null;

    @property(Node)
    wildTimesIcon: Node = null;

    @property(Node)
    wildTimesIconSpine: Node = null;

    @property(SpriteAtlas)
    iconSpriteFrame: SpriteAtlas[] = [];

    public iconType = 0;
    public isActive = false;
    public wildTimes = 1;
    public lineCount = 0;

    start() {
        this.iconType = 0;
        this.isActive = false;
    }

    setIconType(iconType, isNormal, wildTimes = 1) {
        // 设置图标类型
        this.iconType = iconType;
        this.wildTimes = wildTimes;

        // 设置图标图片
        let spriteAtlasName = `${(iconType + 1).toString().padStart(2, '0')}_default`;
        let spriteFrameName = isNormal ? GameConstant.ICON_NORMAL_SPRITE_NAME[iconType] : GameConstant.ICON_BLURRED_SPRITE_NAME[iconType]
        let spriteFrame = ResourceLoader.instance.getResource<SpriteAtlas>(spriteAtlasName).getSpriteFrame(spriteFrameName);
        // console.log(spriteFrame);
        this.icon.getComponent(Sprite).spriteFrame = spriteFrame;

        // 设置图标spine动画
        let spineName = `S${(iconType + 1).toString().padStart(2, '0')}`;
        this.iconSpine.getComponent(sp.Skeleton).skeletonData = ResourceLoader.instance.getResource(spineName);

        this.makeItem();
    }

    getIconType() {
        return this.iconType;
    }

    makeItem() {
        this.icon.scale = new Vec3(1, 1, 1);//new Vec3(0.95, 0.95, 0.95);
        this.icon.x = 0;
        this.icon.y = 0;
        this.iconSpine.scale = new Vec3(1, 1, 1);//new Vec3(0.95, 0.95, 0.95);
        this.iconSpine.x = 0;
        this.iconSpine.y = 0;
        this.wildTimesIcon.active = false;
        this.wildTimesIconSpine.active = false;

        if (this.iconType <= GameConstant.ICON_TYPE.SYMBOL_FREE) {
            // this.iconBg.node.scale = 0.9;
            // this.normalBG.scale = 0.9;
        } else if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_WILD) {
            this.iconSpine.position = new Vec3(-4.5, -18, 0);

            let wildTimesSpriteFrameName = this.wildTimes > 1 ? ("02_x" + this.wildTimes) : "02_wild";
            let wildTimesSpriteFrame = ResourceLoader.instance.getResource<SpriteAtlas>("02_default").getSpriteFrame(wildTimesSpriteFrameName);
            this.wildTimesIcon.getComponent(Sprite).spriteFrame = wildTimesSpriteFrame;

            let wildTimesSpineName = this.wildTimes > 1 ? ("S02_x" + this.wildTimes) : "S02_wild";
            this.wildTimesIconSpine.getComponent(sp.Skeleton).skeletonData = ResourceLoader.instance.getResource(wildTimesSpineName);

            this.wildTimesIcon.active = true;
            
            if (this.wildTimes == 1) {
                this.wildTimesIcon.position = new Vec3(0, -60, 0);
                this.wildTimesIconSpine.position = new Vec3(-3, 55.5, 0);
            } else if (this.wildTimes == 2) {
                this.wildTimesIcon.position = new Vec3(0, -50, 0);
                this.wildTimesIconSpine.position = new Vec3(-4, 34, 0);
            } else if (this.wildTimes >= 3) {
                this.wildTimesIcon.position = new Vec3(0, -54, 0);
                this.wildTimesIconSpine.position = new Vec3(-4, 36, 0);
            }
        } else if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_COW) {
            this.iconSpine.position = new Vec3(1.5, -20.5, 0)
        } else if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_EAGLE) {
            this.iconSpine.position = new Vec3(4.5, -13, 0)
        } else if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_LEOPARD) {
            this.iconSpine.position = new Vec3(1, -2.5, 0)
        } else if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_WOLF) {
            this.iconSpine.scale = new Vec3(0.5, 0.5, 0.5);
            this.iconSpine.position = new Vec3(5, -113.5, 0)
        } else if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_ELK) {
            this.iconSpine.position = new Vec3(-1.5, -94, 0)
        } else if (this.iconType >= GameConstant.ICON_TYPE.SYMBOL_A) {
            // this.iconSpine.position = new Vec3(1, 3, 0)
            // this.iconSpine.scale = new Vec3(0.5, 0.5, 0.5);
        } else if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_K) {
            this.iconSpine.scale = new Vec3(0.8, 0.8, 0.8);
        } else if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_Q) {
            this.iconSpine.scale = new Vec3(0.8, 0.8, 0.8);
        } else if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_J) {
            this.iconSpine.scale = new Vec3(0.8, 0.8, 0.8);
        } else if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_10) {
            this.iconSpine.scale = new Vec3(0.8, 0.8, 0.8);
        } else if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_9) {
            this.iconSpine.scale = new Vec3(0.8, 0.8, 0.8);
        }
    }

    showNormal() {
        console.log("showNormal")
        this.iconSpine.getComponent(sp.Skeleton).clearTracks();
        this.iconSpine.getComponent(sp.Skeleton).setToSetupPose()
        this.iconSpine.active = false;

        this.icon.active = true;
        this.wildTimesIconSpine.getComponent(sp.Skeleton).clearTracks();
        this.wildTimesIconSpine.getComponent(sp.Skeleton).setToSetupPose()
        this.wildTimesIconSpine.active = false;
    }

    showLightAni() {
        console.log("showLightAni")
        this.iconSpine.active = true;
        this.icon.active = false;

        this.iconSpine.getComponent(sp.Skeleton).clearTracks();
        this.iconSpine.getComponent(sp.Skeleton).setAnimation(0, "win", false);
        if (this.iconType == GameConstant.ICON_TYPE.SYMBOL_WILD) {
            this.wildTimesIconSpine.active = true;
            this.wildTimesIconSpine.getComponent(sp.Skeleton).clearTracks();
            let wildSpineName = "wild";
            if (this.wildTimes == 2)
                wildSpineName = "x2";
            else if (this.wildTimes == 3)
                wildSpineName = "x3";
            else if (this.wildTimes == 5)
                wildSpineName = "x5";
            this.wildTimesIconSpine.getComponent(sp.Skeleton).setAnimation(0, wildSpineName, false);
        }
    }

    showSpecialAni() {
        if(this.iconType != GameConstant.SPECIAL_ICON) {
            return;
        }
        this.icon.active = false;
        this.iconSpine.active = true;

        this.iconSpine.getComponent(sp.Skeleton).clearTracks();
        this.iconSpine.getComponent(sp.Skeleton).setAnimation(0, "win", false);
    }

    update(deltaTime: number) {
        
    }
}


