import { _decorator, Component, Node } from 'cc';
import { CatAction } from './CatAction';
import { PinAction } from './PinAction';
import { ColorDate } from './ColorDate';
const { ccclass, property } = _decorator;

@ccclass('LayerCatAction')
export class LayerCatAction extends Component {

    onLoad(): void {

    }
    start() {

    }

    update(deltaTime: number) {

    }


    /**
     * put a pin to slot by pin_color
     * @param pin 
     * @returns 
     */
    public put_pin(pin: PinAction): boolean {
        let can_move_arr: CatAction[] = [];
        this.node.children.forEach(slot => {
            if (slot.getComponent(CatAction).check_able_put(pin)) {
                can_move_arr.push(slot.getComponent(CatAction));
            }
        });
        let target: CatAction = null;
        can_move_arr.forEach(element => {
            if (!target) {
                target = element;
            } else {
                if (element.get_temp_hole_num() < target.get_temp_hole_num()) {
                    target = element;
                }
            }

        });
        if (target) {
            return target.put_pin(pin, "LayerSolot");
        }
        return false;
    }

    /**
     * 
     */
    public init_cat() {
        this.node.children.forEach(slot => {
            slot.getComponent(CatAction)?.init_cat();
            // slot.getComponent(CatAction)?.into_scence();
        });
    }

    /**
     * 
     * @returns 
     */
    public get_cat_color_arr() {
        let arr: ColorDate[] = [];
        this.node.children.forEach(slot => {
            if (slot.getComponent(CatAction)) {
                let color = slot.getComponent(CatAction).get_cat_color();
                if (color) {
                    arr.push(color);
                }
            }
        });
        return arr;
    }

}

