
import GameDataCenter from "../../data/GameDataCenter";
import ResSprite from "../../frameWork/compment/ResSprite";
import UEBase from "../../frameWork/compment/UEBase";
import EventMng from "../../manager/EventMng";
import { HcInfoGeziInfo, HcType, HcUnlock } from "../../shared/hc/PtlHcInfo";
import { GridConstant } from "./GridConstant";
import { GridEvent } from "./GridEvent";
import UECube from "./UECube";

const { ccclass, property } = cc._decorator;

export interface I_CellData {
    idx: number;
    ueCube: UECube;
    unlock: HcUnlock;
}
@ccclass
export default class UECell extends UEBase {
    static readonly BundleKey: string = "gridMap";
    static readonly PrefabUrl: string = "UECell";
    static readonly CLS: string = "UECell";

    @property(cc.Node)
    private node_select: cc.Node = null!;
    @property(ResSprite)
    private sp_quality: ResSprite = null;
    @property(cc.Node)
    private node_lock: cc.Node = null;

    cellData: I_CellData = null!;
    Init(cellData: I_CellData) {
        this.cellData = cellData;
        this.node.zIndex = cellData.idx;
        this.SetQuality();
        this.SetLock();
        EventMng.on(GridEvent.HC_CELL_CHANGE, this.OnHcCellChange, this);
        EventMng.on(GridEvent.HC_CELL_SELECT, this.OnHcCellSelect, this);
    }

    /** 监听格子选中 */
    OnHcCellSelect(idx: number) {
        if (!idx) {
            this.SetSelect(false);
        } else if (idx == this.cellData.idx) {
            this.SetSelect(true);
        }
    }

    /** 监听格子变化 */
    OnHcCellChange(data: { idx: number, item: HcInfoGeziInfo }) {
        if (data.idx == this.cellData.idx) {
            if (data.item.type == HcType.wu) {
                this.ClearCube();
            } else {

            }
        }
    }

    /** 创建格子 */
    CreateCell(i: number, j: number) {
        // let cell = cc.instantiate(this.cellPrefab).getComponent(UECell);
        // this.cellLayer.addChild(cell.node);
        // cell.node.width = GridConstant.CELL_WIDTH;
        // cell.node.height = GridConstant.CELL_WIDTH;
        // let pos = GameDataCenter.gridMap.GetPosByVec(i, j);
        // cell.node.setPosition(pos);
        // return cell;
    }

    SetCube(cube: UECube) {
        this.cellData.ueCube = cube;
        this.SetQuality();
    }

    SetQuality() {
        if (this.cellData.ueCube && !this.IsLock()) {
            this.cellData.ueCube.SetZIndex(this.cellData.idx);
            let quality = this.cellData.ueCube.GetQuality();
            this.sp_quality.setSpriteFrame('gridMap', `qualityBg/Img_zjm_diban0${quality}`);
        } else {
            this.sp_quality.setSpriteFrame('gridMap', `qualityBg/Img_zjm_diban00`);
        }
    }

    IsLock(): boolean {
        return this.cellData.unlock == HcUnlock.off;
    }

    ChangeLockState(unlock: HcUnlock) {
        this.cellData.unlock = unlock;
        this.SetLock();
    }

    SetLock() {
        if (this.cellData.unlock == HcUnlock.off) {
            this.node_lock.active = true;
            if (this.cellData.ueCube) {
                this.cellData.ueCube.node.active = false;
            }
        } else {
            this.node_lock.active = false;
            if (this.cellData.ueCube) {
                this.cellData.ueCube.node.active = true;
            }
        }

    }

    GetZIndex() {
        return this.cellData.idx;
    }


    SetSelect(isSelect: boolean) {
        this.node_select.active = isSelect;
    }

    /** 是否有物品可以拖动 */
    CanDrag(): boolean {
        return !this.IsEmpty() && this.cellData.ueCube.CanDrag() && !this.IsLock();
    }

    GetCube(): UECube {
        return this.cellData.ueCube;
    }

    /** 该棋格是否是空的 */
    IsEmpty(): boolean {
        return this.cellData.ueCube == null;
    }

    /** 清空棋格里的棋子 */
    ClearCube() {
        this.cellData.ueCube.unUse();
        this.cellData.ueCube = null;
        this.SetQuality();
    }

    MoveCubeToCell(toCell: UECell) {
        this.cellData.ueCube.MoveToNewPos(toCell.node.position.clone());
    }

}
