import { _decorator, Component, EventTouch, Input, Node, tween, UITransform, Vec3 } from 'cc';
import { AudioMgr } from './AudioMgr';
import { Clips } from './Enums';
import { Global } from './Global';
import { PinAction } from './PinAction';
import { Tools } from './Tools';
const { ccclass, property } = _decorator;

@ccclass('EmptyHoleAction')
export class EmptyHoleAction extends Component {
    @property({type:Node})
    lock:Node;
    defalut_lock:boolean = false;//默认不锁
    isLocked:boolean = false;
    onLoad(): void {
        //显示锁头
        this.defalut_lock = this.lock.active;
    }
    start() {
        // this.node.on(Input.EventType.TOUCH_START, this.unlock_hole, this);
    }

    set_lock_unlock(l:boolean){
        this.isLocked = l;
        if(this.lock){
            tween(this.lock)
            .to(0.25,{scale:new Vec3(1.2,1.2,1)},Global.our_easing)
            .to(0.45,{scale:new Vec3(0.8,0.8,1)},Global.our_easing)
            .to(0.25,{scale:new Vec3(1,1,1)},Global.our_easing)
            .call(()=>{
                this.lock.scale = new Vec3(1,1,1);
                this.lock.active = l;
            })
            .start();
           }
    }

    init_empty_hole(){
        this.set_lock_unlock(this.defalut_lock);
        this.clear_pins();
    }

    unlock_hole(e: EventTouch) {
        // console.log("开始。。。empty ",this.isLocked);
        if(!this.isLocked){
            return;
        }
        this.set_lock_unlock(false);
    }

    update(deltaTime: number) {
        
    }

    /**
     * 获取钉子
     * @param arr 
     * @returns 
     */
    public get_pin_arr(arr:PinAction[] = null): PinAction[] {
        if(!arr){
            arr = [];
        }
        this.node.children.forEach(element => {
            if (element.getComponent(PinAction)) {
                arr.push(element.getComponent(PinAction));
            }
        });
        return arr;
    }

    /**
     * 
     * @param color_id 
     * @param pin_arr 
     */
    public get_pin_arr_by_color_id(color_id:number,pin_arr:PinAction[]){
        this.node.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);
                }
            }
        });
    }



    /**
     * 
     * @returns 
     */
    private get_temp_position(): Vec3 {
        //钉子数量
        let pin_arr = this.get_pin_arr();
        if(pin_arr.length>0){
            //有钉子了
            return null;
        }
        return new Vec3(0,0,0);
    }

    private clear_pins(){
        // this.node.children.forEach(element => {
        //     if (element.getComponent(PinAction)) {
        //         Tools.clearUI(element);
        //     }
        // });
        Tools.clearFromParent(this.node,PinAction);
    }

    public can_able_put():boolean{
        if(this.isLocked){
            return false;
        }
        let target_hole_pos = this.get_temp_position();
        if (target_hole_pos) {
            return true;
        }else{
            return false;
        }
    }
    /**
     * 
     * @param pin 
     * @returns 
     */

    public put_pin(pin: PinAction):boolean {

        let target_hole_pos = this.get_temp_position();
        if (!target_hole_pos) {
            // 没找到孔的点
            // console.log("没找到孔的点");
            return false;
        }
        //
        let world_pos = pin.node.getWorldPosition();
        let target = this.node.getComponent(UITransform).convertToNodeSpaceAR(world_pos);
        this.node.addChild(pin.node);
        pin.node.setPosition(target);

        // let target = find("Canvas").getComponent(UITransform).convertToNodeSpaceAR(hole_world_pos);
        pin.remove_collider();
        // Global.pin_prgress_computed(1);

        tween(pin.node)
            .to(0.25, { position: target_hole_pos },Global.our_easing)
            .call(() => {
                AudioMgr.ins.playSound(Clips.close_screw);
                // if (this.get_temp_hole_num()<=0) {
                //     //如果已经满了。
                //     this.full_complete();
                // }
            })
            .start();
        return true;
    }
}

