import { _decorator, Component, Node, tween, UITransform, Vec3 } from 'cc';
import { PinAction } from './PinAction';
import { AudioMgr } from './AudioMgr';
import { Clips } from './Enums';
import { Tools } from './Tools';
import { Global } from './Global';

const { ccclass, property } = _decorator;

@ccclass('BucketAction')
export class BucketAction extends Component {
    
    @property({type:Node})
    container:Node

    start() {
    }

    update(deltaTime: number) {  
    }

    clear_container(){
        this.container.removeAllChildren();
    }

    /**
     * 
     * @param pin 
     */
    public put_pins(pin: PinAction[]) {
        pin.forEach(element => {
            this.put_pin(element);
        });
    }

    /**
     * 
     * @param pin 
     */
    public put_pin(pin: PinAction) {
        let target_hole_pos = new Vec3(Tools.random_between(-20,20),Tools.random_between(-30,30),1);
        //
        let world_pos = pin.node.getWorldPosition();
        let target = this.container.getComponent(UITransform).convertToNodeSpaceAR(world_pos);
        this.container.addChild(pin.node);
        pin.node.setPosition(target);

        pin.remove_collider();

        tween(pin.node)
            .to(0.25, { position: target_hole_pos },Global.our_easing)
            .call(() => {
                AudioMgr.ins.playSound(Clips.close_screw);
            })
            .start();
    }

    /**
     * 
     * @param color_id 
     * @param pin_arr 
     */
    public get_pin_arr_by_color_id(color_id:number,pin_arr:PinAction[]){
        this.container.children.forEach(element => {
            if (element.getComponent(PinAction)) {
                let pin_action = element.getComponent(PinAction);
                if(pin_action.pin_color&&pin_action.pin_color.id == color_id){
                     pin_arr.push(pin_action);
                }
            }
        });
    }

    /**
     * 
     * @param pin_arr 
     */
    public get_pin_arr(pin_arr:PinAction[]){
        this.container.children.forEach(element => {
            if (element.getComponent(PinAction)) {
                let pin_action = element.getComponent(PinAction);
                if(pin_action.pin_color){
                     pin_arr.push(pin_action);
                }
            }
        });
    }
}

