import { _decorator, BoxCollider2D, CircleCollider2D, Color, Component, EventTouch, find, Input, instantiate, Node, PolygonCollider2D, Rect, RigidBody2D, Sprite, tween, UITransform, Vec2, Vec3 } from 'cc';
import { ElementAction } from './ElementAction';
import { LayerRootAction } from './LayerRootAction';
import { Clips } from './Enums';
import { AudioMgr } from './AudioMgr';
import { LayerCatAction } from './LayerCatAction';
import { ColorDate } from './ColorDate';
import { Global } from './Global';
const { ccclass, property } = _decorator;

@ccclass('PinAction')
export class PinAction extends Component {
    pin_color: ColorDate = null;
    start() {
        this.node.on(Input.EventType.TOUCH_START, this.touch_start, this);
    }

    update(deltaTime: number) {

    }

    /**
     * 
     * @param group_id 
     * @param pin_color 
     */
    public init_date(group_id: number, pin_color: ColorDate) {
        this.node.getComponent(RigidBody2D).group = group_id;
        this.node.getComponents(BoxCollider2D).forEach(element => {
            element.group = group_id;
        });
        this.node.getComponents(CircleCollider2D).forEach(element => {
            element.group = group_id;
        });
        this.node.getComponents(PolygonCollider2D).forEach(element => {
            element.group = group_id;
        });
        this.pin_color = pin_color;
        //set color
        this.reset_img();
    }

    /**
     * 
     * @param e 
     * @returns 
     */
    touch_start(e: EventTouch) {
        AudioMgr.ins.playSound(Clips.open_screw);
        // ret?boolean ,return put result,true=success,false=failure
        if (Global.layer_cat_action.put_pin(this)) {
            // 需要检查，关卡是否结束
            return;
        }
        Global.layer_empty_action.put_pin(this);

        if (Global.layer_empty_action.is_pin_full()) {
            //TODO 游戏结束,这里有个bug,用延迟处理，后面处理
            tween(this.node).delay(0.75).call(() => {
                if (Global.layer_empty_action.is_pin_full()) {
                    Global.game_over_action.open();
                } else {
                    //console.log("$FFFFFFFFFFFFF");
                }
            }).start();
        }

    }


    public remove_collider() {
        this.node.getComponent(RigidBody2D).enabled = false;
        this.node.getComponent(CircleCollider2D).enabled = false;
        this.node.off(Input.EventType.TOUCH_START);
    }

    /**
     * 
     * @returns void
     */
    reset_img() {
        if (!this.pin_color) {
            return;
        }
        let img = this.node.getComponent(Sprite);
        img.color = new Color(this.pin_color.r, this.pin_color.g, this.pin_color.b, 255);

        let holering = this.node.getChildByName("holering")
        if (holering) {
            tween(holering)
                .to(0.35, { scale: new Vec3(1.2, 1.2, 1) }, Global.our_easing)
                .to(0.35, { scale: new Vec3(0.8, 0.8, 1) }, Global.our_easing)
                .to(0.25, { scale: new Vec3(1, 1, 1) }, Global.our_easing)
                .call(() => {
                    holering.scale = new Vec3(1, 1, 1);
                })
                .start();
        }

    }

    // get_pin_color(arr:ColorDate[]){
    //     if(!this.pin_color){
    //         return;
    //     }
    //     arr.push(this.pin_color);
    // }

}

