import Gamecfg from "../../common/gameCfg";
import GameDataCenter from "../../data/GameDataCenter";
import ResSprite from "../../frameWork/compment/ResSprite";
import UEBase from "../../frameWork/compment/UEBase";
import { HcType } from "../../shared/hc/PtlHcInfo";
import AssetMgr from "../../utils/AssetMgr";
import UEHomeRole from "./UEHomeRole";

const { ccclass, property } = cc._decorator;

export interface I_CubeData {
    /** 物品类型 */
    type: HcType;
    /** 物品ID */
    id: number;
    /** 位置索引 */
    idx: number;
}
@ccclass
export default class UECube extends UEBase {
    static readonly BundleKey: string = "gridMap";
    static readonly PrefabUrl: string = "UECube";
    static readonly CLS: string = "UECube";

    @property(cc.Node)
    private itemNode: cc.Node = null!;
    @property(cc.Label)
    private lbt_num: cc.Label = null!;
    @property(cc.Label)
    private lbt_key: cc.Label = null!;
    @property(ResSprite)
    private sp_item: ResSprite = null!;
    @property(cc.Node)
    private node_emitter_num: cc.Node = null!;

    cubeData: I_CubeData = null!;
    quality: number = 0;
    ueHomeRole: UEHomeRole = null!;
    Init(cubeData: I_CubeData) {
        this.cubeData = cubeData;
        this.SetZIndex(cubeData.idx);
        this.node_emitter_num.active = false;
        if (cubeData.type == HcType.emitter) {
            let mergePropCfg = Gamecfg.emitterInfo.getItem(cubeData.id.toString());
            this.sp_item.setSpriteFrame('gridMap', `mergeProp/${mergePropCfg.icon}`);
            this.quality = mergePropCfg.quality;
            this.node_emitter_num.active = true;
        } else if (cubeData.type == HcType.material) {
            let mergePropCfg = Gamecfg.mergePropInfo.getItem(cubeData.id.toString());
            this.sp_item.setSpriteFrame('gridMap', `mergeProp/${mergePropCfg.icon}`);
            this.quality = mergePropCfg.quality;
        } else if (cubeData.type == HcType.equip) {
            let mergePropCfg = Gamecfg.equipInfo.getItem(cubeData.id.toString());
            this.sp_item.setSpriteFrame('gridMap', `mergeEquip/${mergePropCfg.icon}`);
            this.quality = mergePropCfg.quality;
        } else if (cubeData.type == HcType.user) {
            this.ueHomeRole = AssetMgr.instantiateUE(UEHomeRole);
            this.itemNode.addChild(this.ueHomeRole.node);
            this.ueHomeRole.Init(cubeData);
        } else if (cubeData.type == HcType.monster) {
            this.ueHomeRole = AssetMgr.instantiateUE(UEHomeRole);
            this.itemNode.addChild(this.ueHomeRole.node);
            this.ueHomeRole.Init(cubeData);
        }
        // this.lbt_key.string = `${cubeData.type}_${cubeData.id}`;
    }

    /** 获取品质 */
    GetQuality() {
        return this.quality;
    }

    /** 触发点击 */
    TriggerClick() {
        if (this.cubeData.type == HcType.emitter) {
            CC_PREVIEW && console.log("触发点击发射器");
            GameDataCenter.gridMap.sendEmitter(this.cubeData.idx);
        } else if (this.cubeData.type == HcType.monster) {
            CC_PREVIEW && console.log("点击怪物开始战斗");
            GameDataCenter.gridMap.sendFightHc(this.cubeData.idx);
        } else if (this.cubeData.type == HcType.equip) {

        }
    }


    GetCubeData(): I_CubeData {
        return this.cubeData;
    }

    SetZIndex(zIndex: number) {
        this.node.zIndex = zIndex;
        this.cubeData.idx = zIndex;
        // this.lbt_num.string = zIndex.toString();
    }

    /** 是否有物品可以拖动 */
    CanDrag(): boolean {
        if (this.cubeData.type == HcType.monster) return false;
        return true;
    }

    StartDrag(): void {
        this.node.zIndex = 1000;
    }

    UpdateDragPosition(pos: cc.Vec3): void {
        this.node.position = pos.sub(this.node.position);
    }

    EndDrag(): void {
        this.node.zIndex = this.cubeData.idx;
    }

    ClearCell() {

    }

    /** 回到原来位置 */
    BackToOriginalPos(needMove: boolean) {
        let originalPos = GameDataCenter.gridMap.GetPosByIdx(this.cubeData.idx);
        if (needMove) {
            cc.tween(this.node)
                .to(0.15, { position: originalPos })
                .call(() => {
                    this.EndDrag();
                    this.PlayJellyAnim();
                })
                .start();
        } else {
            this.itemNode.setPosition(originalPos.clone());
            this.EndDrag();
            this.PlayJellyAnim();
        }

    }

    /** 播放果冻效果 */
    PlayJellyAnim() {
        cc.tween(this.itemNode)
            .to(0.1, { scale: 1.3 })
            .to(0.1, { scale: 0.9 })
            .to(0.08, { scale: 1 })
            .start();
    }

    /** 播放合成动画 */
    PlayMergeAnim() {
        cc.tween(this.itemNode)
            .by(0.1, { position: cc.v3(0, 40) })
            .by(0.1, { position: cc.v3(0, -40) })
            .by(0.1, { position: cc.v3(0, 10) })
            .by(0.1, { position: cc.v3(0, -10) })
            .start();
    }

    /** 移动到新的位置 */
    MoveToNewPos(newPos: cc.Vec3, duration: number = 0.1) {
        cc.tween(this.node)
            .to(duration, { position: newPos })
            .call(() => {
                this.EndDrag();
            })
            .start();
    }

    public unUse() {
        this.node.destroy();
    }
}
