//script core ver 3.0.4

include(Resources.id("mtrsteamloco:scripts/js_util.js"));
include(Resources.id("fangsu:scripts/config_sc.js"));
include(Resources.id("fangsu:scripts/model_select_screen.js"));
include(Resources.id("fangsu:scripts/pzx_helper.js"));
include(Resources.id("fangsu:scripts/costom_item_helper.js"));
include(Resources.id("fangsu:scripts/block_connector.js"));
importPackage(java.awt);
importPackage(java.awt.geom);

// var res = {};

function create(ctx, state, block) {
    // state.mainModel = checkAndCacheConfig(block, state, "mainModel", "fangsu:screendoor/kaba.json");

    // state.subModel = checkAndCacheConfig(block, state, "subModel", "common");

    state.jikongState = checkAndCacheConfig(block, state, "jikongState", 0);
    state.config = {};

    // state.dmh = [];

    // state.needRef = true;
    // state.needRefBox = true;
    state.needRefJikong = true;
}

function render(ctx, state, block) {
    // if (state.cacheMainMod != block.getCustomConfig("mainModel") || state.cacheSubMod != block.getCustomConfig("subModel")) {
    //     state.needRef = true;
    // }
    // if (state.isLeft != state.cacheIsLeft) {
    //     state.needRef = true;
    //     state.cacheIsLeft = state.isLeft;
    //     block.putCustomConfig("isLeft", state.isLeft ? "true" : "false");
    // }
    let blockConnector = getBlockConnector();

    // if (state.config.mainModel) state.mainModel = state.config.mainModel;
    // if (state.config.subModel) state.subModel = state.config.subModel;
    if (state.config.jikongState != undefined) state.jikongState = state.config.jikongState;
    state.config = {};

    state.needRefJikong = checkCache(block, state, "jikongState") || state.needRefJikong;

    if (state.needRefJikong) {
        let worldPos = block.getWorldPosVector3f();
        let thisBlockPos = [worldPos.x(), worldPos.y(), worldPos.z()];
        //up
        if (
            blockConnector.getBlockStateParam([thisBlockPos[0], thisBlockPos[1] + 1, thisBlockPos[2]], "type") == "screendoor" ||
            blockConnector.getBlockStateParam([thisBlockPos[0], thisBlockPos[1] + 1, thisBlockPos[2]], "type") == "screendoor_glass"
        ) {
            let nowBlockPos;
            nowBlockPos = [thisBlockPos[0], thisBlockPos[1] + 1, thisBlockPos[2]];
            //x+
            while (
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
            ) {
                blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
                nowBlockPos[0]++;
            }
            nowBlockPos = [thisBlockPos[0], thisBlockPos[1] + 1, thisBlockPos[2]];
            //x-
            while (
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
            ) {
                blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
                nowBlockPos[0]--;
            }
            nowBlockPos = [thisBlockPos[0], thisBlockPos[1] + 1, thisBlockPos[2]];
            //z+
            while (
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
            ) {
                blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
                nowBlockPos[2]++;
            }
            nowBlockPos = [thisBlockPos[0], thisBlockPos[1] + 1, thisBlockPos[2]];
            //z-
            while (
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
            ) {
                blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
                nowBlockPos[2]--;
            }
        }
        //down
        if (
            blockConnector.getBlockStateParam([thisBlockPos[0], thisBlockPos[1] - 1, thisBlockPos[2]], "type") == "screendoor" ||
            blockConnector.getBlockStateParam([thisBlockPos[0], thisBlockPos[1] - 1, thisBlockPos[2]], "type") == "screendoor_glass"
        ) {
            let nowBlockPos;
            nowBlockPos = [thisBlockPos[0], thisBlockPos[1] - 1, thisBlockPos[2]];
            //x+
            while (
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
            ) {
                blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
                nowBlockPos[0]++;
            }
            nowBlockPos = [thisBlockPos[0], thisBlockPos[1] - 1, thisBlockPos[2]];
            //x-
            while (
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
            ) {
                blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
                nowBlockPos[0]--;
            }
            nowBlockPos = [thisBlockPos[0], thisBlockPos[1] - 1, thisBlockPos[2]];
            //z+
            while (
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
            ) {
                blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
                nowBlockPos[2]++;
            }
            nowBlockPos = [thisBlockPos[0], thisBlockPos[1] - 1, thisBlockPos[2]];
            //z-
            while (
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
                blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
            ) {
                blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
                nowBlockPos[2]--;
            }
        }
        let nowBlockPos;
        nowBlockPos = [thisBlockPos[0] + 1, thisBlockPos[1], thisBlockPos[2]];
        //x+
        while (
            blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
            blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
        ) {
            blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
            nowBlockPos[0]++;
        }
        nowBlockPos = [thisBlockPos[0] - 1, thisBlockPos[1], thisBlockPos[2]];
        //x-
        while (
            blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
            blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
        ) {
            blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
            nowBlockPos[0]--;
        }
        nowBlockPos = [thisBlockPos[0], thisBlockPos[1], thisBlockPos[2] + 1];
        //z+
        while (
            blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
            blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
        ) {
            blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
            nowBlockPos[2]++;
        }
        nowBlockPos = [thisBlockPos[0], thisBlockPos[1], thisBlockPos[2] - 1];
        //z-
        while (
            blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor" ||
            blockConnector.getBlockStateParam([nowBlockPos[0], nowBlockPos[1], nowBlockPos[2]], "type") == "screendoor_glass"
        ) {
            blockConnector.setBlockStateParam(nowBlockPos, "jikongState", state.jikongState);
            nowBlockPos[2]--;
        }
    }

    // state.needRef = checkCache(block, state, "mainModel") || checkCache(block, state, "subModel") || checkCache(block, state, "forceState") || state.needRef;

    // if (
    //     state.cacheMat.rX != block.rotateX ||
    //     state.cacheMat.rY != block.rotateY ||
    //     state.cacheMat.rZ != block.rotateZ ||
    //     state.cacheMat.tX != block.translateX ||
    //     state.cacheMat.tY != block.translateY ||
    //     state.cacheMat.tZ != block.translateZ
    // ) {
    //     // 如果有任一属性不同，则更新缓存并标记需要刷新
    //     state.cacheMat = {
    //         rX: block.rotateX,
    //         rY: block.rotateY,
    //         rZ: block.rotateZ,
    //         tX: block.translateX,
    //         tY: block.translateY,
    //         tZ: block.translateZ
    //     };
    //     state.needRefBox = true;
    // }

    // if (state.needRef) {
    //     try {
    //         for (let dm of state.dmh) dm.close();
    //     } catch (e) {}

    //     state.cacheMainMod = block.getCustomConfig("mainModel");
    //     state.cacheSubMod = block.getCustomConfig("subModel");

    //     let loadedJSON = JSON.parse(loadResource("str", state.cacheMainMod));
    //     let loaded = parseObj(loadedJSON.door[state.isLeft ? "left" : "right"]);
    //     if (state.cacheSubMod in loaded) state.model = loaded[state.cacheSubMod];
    //     else {
    //         state.model = JSON.parse(loadResource("str", state.cacheMainMod)).door[state.isLeft ? "left" : "right"][0];
    //         block.putCustomConfig("subModel", state.model.key);
    //         block.sendUpdateC2S();
    //     }

    //     let mainModel = loadResource("partedModel", JSON.parse(loadResource("str", state.cacheMainMod)).model);
    //     // print(JSON.stringify(mainModel))
    //     for (let i = 0; i < state.model.doors.length; i++) {
    //         let door = state.model.doors[i];
    //         // print("[DEBUG] ", door.subModel)
    //         let rawModel = mainModel.get(door.subModel).copy();
    //         rawModel.sourceLocation = null;
    //         if (state.model.flipV) rawModel.applyUVMirror(false, true);
    //         state.dmh[i] = new DynamicModelHolder();
    //         state.dmh[i].uploadLater(rawModel);
    //     }
    //     state.needRef = false;
    //     state.needRefBox = true;
    // }

    //集控状态 0-正常 1-关 2-开

    // ctx.setDebugInfo("mainModel", block.getCustomConfig("mainModel"));
    // ctx.setDebugInfo("isLeft", state.isLeft);
}

function dispose(ctx, state, block) {
    // try {
    //     state.dynamicModelHolder.close();
    // } catch (e) {}
}
function use(ctx, state, block, player) {
    let configs = [];
    let sc;
    // configs.push(
    //     buildConfigItem(ComponentUtil.getString(ComponentUtil.translatable("cfg.content.mainmodel")), "mainModel", {
    //         type: "screendoor",
    //         defaultVal: block.getCustomConfig("mainModel"),
    //         showConditition: true
    //     })
    // );
    // configs.push(
    //     buildConfigItem(ComponentUtil.getString(ComponentUtil.translatable("cfg.content.submodel")), "subModel", {
    //         type: "screendoor",
    //         saveKey: "subModel",
    //         path: "door." + (state.isLeft ? "left" : "right"),
    //         showConditition: true
    //     })
    // );

    configs.push(
        buildConfigItem(ComponentUtil.getString(ComponentUtil.translatable("cfg.screendoor.jikong_state")), "list", {
            listItems: [
                { text: ComponentUtil.getString(ComponentUtil.translatable("cfg.screendoor.jikong_normal")), val: 0 },
                { text: ComponentUtil.getString(ComponentUtil.translatable("cfg.screendoor.jikong_close")), val: 1 },
                { text: ComponentUtil.getString(ComponentUtil.translatable("cfg.screendoor.jikong_open")), val: 2 }
            ],
            type: "list",
            savePos: "jikongState",
            default: state.jikongState
        })
    );

    sc = createConfigSc(configs, null, { ctx, state, entity: block, block }, { title: ComponentUtil.getString(ComponentUtil.translatable("cfg.title")) });
    displayConfigSc(sc);
}
