import { _decorator, BoxCollider2D, CircleCollider2D, Component, EventTouch, Input, instantiate, Node, PolygonCollider2D, Prefab, RigidBody2D, tween, UITransform, Vec2, Vec3 } from 'cc';
import { LayerAction } from './LayerAction';
import { ElementAction } from './ElementAction';
import { HoleAction } from './HoleAction';
import { PinAction } from './PinAction';

import { LevelAction } from './LevelAction';
import { PoolMgr } from './PoolMagr';
import { Tools } from './Tools';
import { ColorDate } from './ColorDate';
import { Global } from './Global';
const { ccclass, property } = _decorator;

@ccclass('LayerRootAction')
export class LayerRootAction extends Component {
    lvl_action: LevelAction
    update(deltaTime: number) {
    }

    public init_root(lvl: number) {
        Tools.clearFromParent(this.node, LevelAction);
        let level_prebab = PoolMgr.ins.getPrefab("lvl_" + lvl);
        if (!level_prebab) {
            console.log("没有这个关卡》〉》〉》", lvl);
            //return
            console.log("重置一个等级1");
            Global.restart_default(0);
            lvl = Global.next_level();
            level_prebab = PoolMgr.ins.getPrefab("lvl_" + lvl);
        }
        let lvl_node = instantiate(level_prebab);
        this.lvl_action = lvl_node.getComponent(LevelAction)
        this.lvl_action.init_level();
        this.node.addChild(lvl_node);
    }
    /**
     * 随机打乱钉子颜色
     */
    random_pin() {
        let arr: PinAction[] = [];
        if (this.lvl_action) {
            arr = this.lvl_action.get_pin_color();
        }

        let t = 0.05;
        let y = this.node.getPosition().y;
        tween(this.node)
            .to(t, { position: new Vec3(30, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(-30, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(30, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(-30, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(20, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(-20, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(20, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(-20, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(10, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(-10, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(2, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(-2, y, 0) }, Global.our_easing)
            .to(t, { position: new Vec3(0, y, 0) }, Global.our_easing)
            .delay(0.3)
            .call(() => {
                this.node.setPosition(new Vec3(0, y, 0));

                let arr_color: ColorDate[] = [];
                arr.forEach(element => {
                    let c = element.pin_color;
                    arr_color.push(c.clone());
                });
                arr_color.sort(() => {
                    return 0.5 - Math.random();
                });
                if (arr.length == arr_color.length) {
                    for (let i = 0; i < arr.length; i++) {
                        let pin_color = arr[i].pin_color;
                        let c = arr_color[i];
                        pin_color.r = c.r;
                        pin_color.g = c.g;
                        pin_color.b = c.b;
                        pin_color.id = c.id;
                        arr[i].reset_img();
                    }
                }
            })
            .start();


    }

}

