import { _decorator, Component } from 'cc';
import { CarColors } from './CarColorsGlobalTypes';
import { PinComponent } from './Components/PinComponent';
import { LayerAction } from './LayerAction';
import { EventDispatcher } from '../../core_tgx/easy_ui_framework/EventDispatcher';
import { GameEvent } from './Enum/GameEvent';
const { ccclass, property } = _decorator;

@ccclass('UnitAction')
export class UnitAction extends Component {
    start() {
        EventDispatcher.instance.on(GameEvent.EVENT_UPDATE_LAYER, this.onCheckLayerChild, this);
    }

    onCheckLayerChild() {
        if (!this.node) return

        this.node.children.forEach(layer_node => {
            const layer_action = layer_node.getComponent(LayerAction)!;
            if (layer_action.node.children.length <= 0) {
                console.log('删除一个Layer层 没有element元素了---->')
                layer_action.node.removeFromParent();
                layer_action.node.destroy();
            }
        });
    }

    update(deltaTime: number) {

    }

    public init_layer() {
        this.node.children.forEach(layer_node => {
            layer_node.getComponent(LayerAction).init_layer();
        });
    }

    public init_pin(color_pin_arr: CarColors[]) {
        this.node.children.forEach(layer_node => {
            layer_node.getComponent(LayerAction).init_pin(color_pin_arr);
        });
    }

    get_pin_color(arr: PinComponent[]): PinComponent[] {
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            let layer_action = this.node.children[i].getComponent(LayerAction)!;
            layer_action.get_pin_color(arr);
        }
        return arr
    }

    get_layer(arr: LayerAction[]): LayerAction[] {
        //默认都是不显示的
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            let layer_action = this.node.children[i]!.getComponent(LayerAction)!;
            if (layer_action) {
                arr.push(layer_action);
            }
        }
        return arr;
    }

    get_hole_num(): number {
        let hole_num: number = 0;
        this.node.children.forEach(layer_node => {
            let num = layer_node.getComponent(LayerAction).get_hole_num();
            hole_num += num;
        });
        return hole_num;
    }
}

