import { _decorator, Component, Node, Rect, tween, UIOpacity, UITransform, Vec2 } from 'cc';
import { PinAction } from './PinAction';
import { ElementAction } from './ElementAction';

import { ColorDate } from './ColorDate';
import { Global } from './Global';
import { DataModel } from './DataModel';
const { ccclass, property } = _decorator;

@ccclass('LayerAction')
export class LayerAction extends Component {
    layer_group_id = 0;
    layer_color: ColorDate = null;
    start() {
        console.log("zh:layerAction.ts start");
    }

    update(deltaTime: number) {
    }

    public get_hole_num(): number {
        let hole_num: number = 0;
        this.node.children.forEach(element_node => {
            hole_num += element_node.getComponent(ElementAction).get_hole_num();
        });
        return hole_num;
    }

    public init_layer() {
        this.layer_group_id = DataModel.get_new_group_index();
        this.layer_color = DataModel.get_layer_color();
        this.node.children.forEach(element_node => {
            element_node.getComponent(ElementAction).init_element(this.layer_group_id, this.layer_color);
        });
    }

    public init_pin(color_pin_arr: ColorDate[]) {
        this.node.children.forEach(element_node => {
            element_node.getComponent(ElementAction).init_pin(this.layer_group_id, color_pin_arr);
        });
    }

    get_pin_color(arr: PinAction[]) {
        this.node.children.forEach(element_node => {
            element_node.getComponent(ElementAction)?.get_pin_color(arr);
        });
    }

    public get_element_size(): number {
        return this.node.children.length;
    }

    /**
     * 
     * @param active 
     * @param t 
     */
    public layer_active(active: boolean, t: number = 0.3) {
        let need_show = !this.node.active;
        this.node.active = active;
        // this.node.opa
        let opc_1 = 100;
        let opc_2 = 200;
        if (need_show && active && this.node.getComponent(UIOpacity)) {
            this.node.getComponent(UIOpacity).opacity = 0;
            tween(this.node.getComponent(UIOpacity))
                .to(t, { opacity: opc_1 }, Global.our_easing)
                .to(t, { opacity: opc_2 }, Global.our_easing)
                .to(t, { opacity: opc_1 }, Global.our_easing)
                .to(t, { opacity: opc_2 }, Global.our_easing)
                .to(t, { opacity: opc_1 }, Global.our_easing)
                .to(t, { opacity: opc_2 }, Global.our_easing)
                .to(t, { opacity: opc_1 }, Global.our_easing)
                .to(t, { opacity: 255 }, Global.our_easing)

                .call(() => {
                    this.node.getComponent(UIOpacity).opacity = 255;
                })
                .start();
        }
    }

}

