import { _decorator, Component, director, Node, Prefab } from 'cc';
import { ElementAction } from './ElementAction';

import { LayerAction } from './LayerAction';
import { ColorDate } from './ColorDate';
import { Global } from './Global';
import { PinAction } from './PinAction';
import { DataModel } from './DataModel';
import { events } from './Enums';
import { CatColorRules } from './CatColorRules';
const { ccclass, property } = _decorator;

@ccclass('LevelAction')
export class LevelAction extends Component {
    start() {
        console.log("zh:LevelAction.ts start");
    }

    update(deltaTime: number) {
    }

    get_lvl(): number {
        let arr = this.node.name.split("_");
        // console.log("split get_lvl>>>>>:", arr);
        return Number(arr[1].trim());
    }

    get_hole_num(): number {
        let hole_num: number = 0;
        this.node.children.forEach(layer_node => {
            let num = layer_node.getComponent(LayerAction).get_hole_num();
            hole_num += num;
        });
        return hole_num;
    }

    /**
     * 初始化 关
     */
    public init_level() {
        //每次开始把基础颜色打乱
        DataModel.random_base_color();
        DataModel.generate_lvl_color_arr(Global.cur_lvl + 4);
        // init every layer
        this.node.children.forEach(layer_node => {
            // console.log("init_level this.node>>>>>:", this.node);
            layer_node.getComponent(LayerAction).init_layer();
        });

        //
        let color_pin_arr = [];
        let color_cat_arr = [];
        let total_hole_num = this.get_hole_num();
        let need_slot_num = total_hole_num / 3;
        for (let i = 0; i < need_slot_num; i++) {
            let base = DataModel.get_lvl_color();
            color_pin_arr.push(base.clone());
            color_pin_arr.push(base.clone());
            color_pin_arr.push(base.clone());
            color_cat_arr.push(base);
        }
        color_pin_arr.sort(() => {
            return 0.5 - Math.random();
        });
        color_cat_arr.sort(() => {
            return 0.5 - Math.random();
        });
        console.log(" current lvl pin num：", color_pin_arr.length);
        console.log(" current lvl solt num：", color_cat_arr.length);

        this.node.children.forEach(layer_node => {
            // console.log("init_level this.node>>>>>:", this.node);
            layer_node.getComponent(LayerAction).init_pin(color_pin_arr);
        });
        // console.log("need pin  num：",need_slot_num);
        CatColorRules.set_cat_color(color_cat_arr);
        Global.layer_empty_action.init_empty();
        Global.layer_cat_action.init_cat();

        //本关，钉子总数
        Global.init_pin_info(total_hole_num);
        director.on(events.remove_element, this.remove_element, this);
        //默认隐藏一些
        this.set_default_layer();
    }

    /**
     * 
     */
    set_default_layer() {
        //默认都是不显示的
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            let layer_action = this.node.children[i].getComponent(LayerAction);
            layer_action.node.active = false;
        }
        this.remove_element();
    }

    /**
     * 移除元素
     */
    remove_element() {
        let default_show_layer_num = 4;
        let show_num = 0;
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            let layer_action = this.node.children[i].getComponent(LayerAction);
            if (layer_action.get_element_size() <= 0) {
                continue;
            }
            if (show_num < default_show_layer_num) {
                layer_action.layer_active(true);
                show_num++;
            } else {
                layer_action.layer_active(false);
            }
        }
    }

    /**
     * 
     * @returns 
     */
    get_pin_color(): PinAction[] {
        let arr: PinAction[] = [];
        // this.node.children.forEach(layer_node => {
        //     layer_node.getComponent(LayerAction)?.get_pin_color(arr);
        // });
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            let layer_action = this.node.children[i].getComponent(LayerAction);
            layer_action?.get_pin_color(arr);
        }
        return arr
    }
}

