/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.util;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.state.properties.WoodType;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.EroticDungeon;
import top.r3944realms.lib39.util.lang.Tuple;

import java.util.Map;

public class WoodTypeHelper {
    public static WoodType[] SUPPORTED_WOOD_TYPES = new WoodType[] {
        WoodType.OAK,
        WoodType.SPRUCE,
        WoodType.BIRCH,
        WoodType.ACACIA,
        WoodType.CHERRY,
        WoodType.JUNGLE,
        WoodType.DARK_OAK,
        WoodType.CRIMSON,
        WoodType.WARPED,
        WoodType.MANGROVE,
        WoodType.BAMBOO,
    };
    public static final Map<WoodType, Tuple> WOOD_TYPE_LANG_MAP = Map.ofEntries(
            Map.entry(WoodType.OAK, Tuple.of("Oak",  "橡木", "橡木", "柞木")), //英文， 简体中文， 繁体中文（台湾），文言文
            Map.entry(WoodType.SPRUCE, Tuple.of("Spruce", "云杉木", "杉木", "雲杉")),
            Map.entry(WoodType.BIRCH, Tuple.of("Birch", "白桦木", "樺木", "白樺")),
            Map.entry(WoodType.ACACIA, Tuple.of("Acacia", "金合欢木", "相思木", "金合歡")),
            Map.entry(WoodType.CHERRY, Tuple.of("Cherry", "樱花木", "櫻花木", "櫻")),
            Map.entry(WoodType.JUNGLE, Tuple.of("Jungle", "丛林木", "叢林木", "叢林")),
            Map.entry(WoodType.DARK_OAK, Tuple.of("Dark Oak", "深色橡木", "黑橡木", "烏樫")),
            Map.entry(WoodType.CRIMSON, Tuple.of("Crimson", "绯红木", "緋紅木", "緋紅")),
            Map.entry(WoodType.WARPED, Tuple.of("Warped", "诡异木", "扭曲木", "詭譎")),
            Map.entry(WoodType.MANGROVE, Tuple.of("Mangrove", "红树木", "紅樹木", "紅樹")),
            Map.entry(WoodType.BAMBOO, Tuple.of("Bamboo", "竹", "竹", "竹"))
    );
    public static final Map<WoodType, Tuple> WOOD_ITEM_MAP = Map.ofEntries(
            Map.entry(WoodType.OAK, Tuple.of(
                    Items.OAK_LOG,
                    Items.STRIPPED_OAK_LOG,
                    Items.OAK_WOOD,
                    Items.STRIPPED_OAK_WOOD,
                    Items.OAK_PLANKS,
                    Items.OAK_STAIRS,
                    Items.OAK_SLAB,
                    Items.OAK_FENCE,
                    Items.OAK_FENCE_GATE
            )),
            Map.entry(WoodType.SPRUCE, Tuple.of(
                    Items.SPRUCE_LOG,
                    Items.STRIPPED_SPRUCE_LOG,
                    Items.SPRUCE_WOOD,
                    Items.STRIPPED_SPRUCE_WOOD,
                    Items.SPRUCE_PLANKS,
                    Items.SPRUCE_STAIRS,
                    Items.SPRUCE_SLAB,
                    Items.SPRUCE_FENCE,
                    Items.SPRUCE_FENCE_GATE
            )),
            Map.entry(WoodType.BIRCH, Tuple.of(
                    Items.BIRCH_LOG,
                    Items.STRIPPED_BIRCH_LOG,
                    Items.BIRCH_WOOD,
                    Items.STRIPPED_BIRCH_WOOD,
                    Items.BIRCH_PLANKS,
                    Items.BIRCH_STAIRS,
                    Items.BIRCH_SLAB,
                    Items.BIRCH_FENCE,
                    Items.BIRCH_FENCE_GATE
            )),
            Map.entry(WoodType.JUNGLE, Tuple.of(
                    Items.JUNGLE_LOG,
                    Items.STRIPPED_JUNGLE_LOG,
                    Items.JUNGLE_WOOD,
                    Items.STRIPPED_JUNGLE_WOOD,
                    Items.JUNGLE_PLANKS,
                    Items.JUNGLE_STAIRS,
                    Items.JUNGLE_SLAB,
                    Items.JUNGLE_FENCE,
                    Items.JUNGLE_FENCE_GATE
            )),
            Map.entry(WoodType.ACACIA, Tuple.of(
                    Items.ACACIA_LOG,
                    Items.STRIPPED_ACACIA_LOG,
                    Items.ACACIA_WOOD,
                    Items.STRIPPED_ACACIA_WOOD,
                    Items.ACACIA_PLANKS,
                    Items.ACACIA_STAIRS,
                    Items.ACACIA_SLAB,
                    Items.ACACIA_FENCE,
                    Items.ACACIA_FENCE_GATE
            )),
            Map.entry(WoodType.DARK_OAK, Tuple.of(
                    Items.DARK_OAK_LOG,
                    Items.STRIPPED_DARK_OAK_LOG,
                    Items.DARK_OAK_WOOD,
                    Items.STRIPPED_DARK_OAK_WOOD,
                    Items.DARK_OAK_PLANKS,
                    Items.DARK_OAK_STAIRS,
                    Items.DARK_OAK_SLAB,
                    Items.DARK_OAK_FENCE,
                    Items.DARK_OAK_FENCE_GATE
            )),
            Map.entry(WoodType.CHERRY, Tuple.of(
                    Items.CHERRY_LOG,
                    Items.STRIPPED_CHERRY_LOG,
                    Items.CHERRY_WOOD,
                    Items.STRIPPED_CHERRY_WOOD,
                    Items.CHERRY_PLANKS,
                    Items.CHERRY_STAIRS,
                    Items.CHERRY_SLAB,
                    Items.CHERRY_FENCE,
                    Items.CHERRY_FENCE_GATE
            )),
            Map.entry(WoodType.CRIMSON, Tuple.of(
                    Items.CRIMSON_STEM,
                    Items.STRIPPED_CRIMSON_STEM,
                    Items.CRIMSON_HYPHAE,
                    Items.STRIPPED_CRIMSON_HYPHAE,
                    Items.CRIMSON_PLANKS,
                    Items.CRIMSON_STAIRS,
                    Items.CRIMSON_SLAB,
                    Items.CRIMSON_FENCE,
                    Items.CRIMSON_FENCE_GATE
            )),
            Map.entry(WoodType.WARPED, Tuple.of(
                    Items.WARPED_STEM,
                    Items.STRIPPED_WARPED_STEM,
                    Items.WARPED_HYPHAE,
                    Items.STRIPPED_WARPED_HYPHAE,
                    Items.WARPED_PLANKS,
                    Items.WARPED_STAIRS,
                    Items.WARPED_SLAB,
                    Items.WARPED_FENCE,
                    Items.WARPED_FENCE_GATE
            )),
            Map.entry(WoodType.MANGROVE, Tuple.of(
                    Items.MANGROVE_LOG,
                    Items.STRIPPED_MANGROVE_LOG,
                    Items.MANGROVE_WOOD,
                    Items.STRIPPED_MANGROVE_WOOD,
                    Items.MANGROVE_PLANKS,
                    Items.MANGROVE_STAIRS,
                    Items.MANGROVE_SLAB,
                    Items.MANGROVE_FENCE,
                    Items.MANGROVE_FENCE_GATE
            )),
            Map.entry(WoodType.BAMBOO, Tuple.of(
                    Items.BAMBOO_BLOCK,
                    Items.STRIPPED_BAMBOO_BLOCK,
                    Items.BAMBOO_BLOCK,
                    Items.STRIPPED_BAMBOO_BLOCK,
                    Items.BAMBOO_PLANKS,
                    Items.BAMBOO_STAIRS,
                    Items.BAMBOO_SLAB,
                    Items.BAMBOO_FENCE,
                    Items.BAMBOO_FENCE_GATE
            ))
    );

    /**
     * 获取原木 Item
     * @param woodType 木材类型
     * @return 原木 Item
     */
    public static Item getLogItem(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        return tuple != null ? (Item) tuple.get(0) : Items.AIR;
    }

    /**
     * 获取剥皮原木 Item
     * @param woodType 木材类型
     * @return 剥皮原木 Item
     */
    public static Item getStrippedLogItem(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        return tuple != null ? (Item) tuple.get(1) : Items.AIR;
    }

    /**
     * 获取木头 Item（带皮木块）
     * @param woodType 木材类型
     * @return 木头 Item
     */
    public static Item getWoodItem(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        return tuple != null ? (Item) tuple.get(2) : Items.AIR;
    }

    /**
     * 获取剥皮木头 Item
     * @param woodType 木材类型
     * @return 剥皮木头 Item
     */
    public static Item getStrippedWoodItem(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        return tuple != null ? (Item) tuple.get(3) : Items.AIR;
    }

    /**
     * 获取木板 Item
     * @param woodType 木材类型
     * @return 木板 Item
     */
    public static Item getPlanksItem(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        return tuple != null ? (Item) tuple.get(4) : Items.AIR;
    }

    /**
     * 获取台阶 Item
     * @param woodType 木材类型
     * @return 台阶 Item
     */
    public static Item getStairsItem(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        return tuple != null ? (Item) tuple.get(5) : Items.AIR;
    }

    /**
     * 获取半砖 Item
     * @param woodType 木材类型
     * @return 半砖 Item
     */
    public static Item getSlabItem(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        return tuple != null ? (Item) tuple.get(6) : Items.AIR;
    }

    /**
     * 获取栅栏 Item
     * @param woodType 木材类型
     * @return 栅栏 Item
     */
    public static Item getFenceItem(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        return tuple != null ? (Item) tuple.get(7) : Items.AIR;
    }

    /**
     * 获取栅栏门 Item
     * @param woodType 木材类型
     * @return 栅栏门 Item
     */
    public static Item getFenceGateItem(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        return tuple != null ? (Item) tuple.get(8) : Items.AIR;
    }

    /**
     * 获取所有相关的木材 Item
     * @param woodType 木材类型
     * @return Tuple 包含所有相关 Item
     */
    public static Tuple getAllWoodItems(WoodType woodType) {
        return WOOD_ITEM_MAP.get(woodType);
    }

    /**
     * 检查是否包含指定木材类型
     * @param woodType 木材类型
     * @return 是否包含
     */
    public static boolean containsWoodType(WoodType woodType) {
        return WOOD_ITEM_MAP.containsKey(woodType);
    }

    /**
     * 获取所有基础建筑方块
     * @param woodType 木材类型
     * @return 包含基础方块的数组
     */
    public static Item[] getBasicBuildingBlocks(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        if (tuple == null) {
            return new Item[0];
        }
        return new Item[] {
                tuple.get(0), // 原木
                tuple.get(1), // 剥皮原木
                tuple.get(2), // 木头
                tuple.get(3), // 剥皮木头
                tuple.get(4)  // 木板
        };
    }

    /**
     * 获取所有建筑装饰方块（台阶和半砖）
     * @param woodType 木材类型
     * @return 包含台阶和半砖的数组
     */
    public static Item[] getBuildingDecorationBlocks(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        if (tuple == null) {
            return new Item[0];
        }
        return new Item[] {
                tuple.get(4), // 木板
                tuple.get(5), // 台阶
                tuple.get(6)  // 半砖
        };
    }

    /**
     * 获取所有建筑方块（包括台阶和半砖）
     * @param woodType 木材类型
     * @return 包含所有建筑方块的数组
     */
    public static Item[] getAllBuildingBlocks(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        if (tuple == null) {
            return new Item[0];
        }
        return new Item[] {
                tuple.get(0), // 原木
                tuple.get(1), // 剥皮原木
                tuple.get(2), // 木头
                tuple.get(3), // 剥皮木头
                tuple.get(4), // 木板
                tuple.get(5), // 台阶
                tuple.get(6)  // 半砖
        };
    }

    /**
     * 获取所有栅栏相关方块
     * @param woodType 木材类型
     * @return 包含栅栏和栅栏门的数组
     */
    public static Item[] getFenceBlocks(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        if (tuple == null) {
            return new Item[0];
        }
        return new Item[] {
                tuple.get(7), // 栅栏
                tuple.get(8)  // 栅栏门
        };
    }

    /**
     * 获取所有建筑结构方块（不包括原木等基础方块）
     * @param woodType 木材类型
     * @return 包含木板、台阶、半砖、栅栏、栅栏门的数组
     */
    public static Item[] getArchitecturalBlocks(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        if (tuple == null) {
            return new Item[0];
        }
        return new Item[] {
                tuple.get(4), // 木板
                tuple.get(5), // 台阶
                tuple.get(6), // 半砖
                tuple.get(7), // 栅栏
                tuple.get(8)  // 栅栏门
        };
    }

    /**
     * 获取所有与木材相关的方块
     * @param woodType 木材类型
     * @return 包含所有相关方块的数组
     */
    public static Item[] getAllWoodBlocks(WoodType woodType) {
        Tuple tuple = WOOD_ITEM_MAP.get(woodType);
        if (tuple == null) {
            return new Item[0];
        }
        Item[] items = new Item[tuple.size()];
        for (int i = 0; i < tuple.size(); i++) {
            items[i] = tuple.get(i);
        }
        return items;
    }

    public static @NotNull String getWoodSpecialFullName(@NotNull WoodType woodType) {
        String wood = woodType.name();
        if (woodType.equals(WoodType.CRIMSON) || woodType.equals(WoodType.WARPED)) {
            wood += "_stem";
        } else if(woodType.equals(WoodType.BAMBOO)) {
            wood += "_block";
        }
        else wood += "_log";
        return wood;
    }
    public static @NotNull String getWoodLog(@NotNull WoodType woodType) {
        return getWoodSpecialFullName(woodType);
    }

    public static @NotNull String getStrippedWoodName(@NotNull WoodType woodType) {
        return "stripped_" + getWoodSpecialFullName(woodType);
    }

    public static @NotNull String getWoodPlankName(@NotNull WoodType woodType) {
        return getWoodSimpleName(woodType) + "_planks";
    }


    public static @NotNull ResourceLocation getWoodLogRL(@NotNull WoodType woodType) {
        return EroticDungeon.mrl("block/" + getWoodLog(woodType));
    }

    public static @NotNull ResourceLocation getStrippedWoodRL(@NotNull WoodType woodType) {
        return EroticDungeon.mrl("block/" + getStrippedWoodName(woodType));
    }

    public static @NotNull ResourceLocation getWoodPlankRL(@NotNull WoodType woodType) {
        return EroticDungeon.mrl("block/" + getWoodPlankName(woodType));
    }

    public static @NotNull ResourceLocation getDirectlyWoodLogRL(@NotNull WoodType woodType) {
        return EroticDungeon.mrl("textures/block/" + getWoodLog(woodType) + ".png");
    }

    public static @NotNull ResourceLocation getDirectlyStrippedWoodRL(@NotNull WoodType woodType) {
        return EroticDungeon.mrl("textures/block/" + getStrippedWoodName(woodType) + ".png");
    }

    public static @NotNull ResourceLocation getDirectlyWoodPlankRL(@NotNull WoodType woodType) {
        return EroticDungeon.mrl("textures/block/" + getWoodPlankName(woodType) + ".png");
    }

    public static @NotNull String getWoodSimpleName(@NotNull WoodType woodType) {
        return woodType.name();
    }
    public static Tuple getWoodNameTranslation(@NotNull WoodType woodType) {
       return WOOD_TYPE_LANG_MAP.get(woodType);
    }
}
