package com.r3944realms.leashedplayer.datagen.generator;


import com.r3944realms.leashedplayer.LeashedPlayer;
import com.r3944realms.leashedplayer.client.renderer.item.ConditionalRangeItemModel;
import com.r3944realms.leashedplayer.client.renderer.item.properties.conditional.ChargeExtend;
import com.r3944realms.leashedplayer.client.renderer.item.properties.conditionalRange.BowPull;
import com.r3944realms.leashedplayer.content.items.ModItemRegister;
import com.r3944realms.leashedplayer.datagen.LanguageAndOtherData.ModLangKeyValue;
import com.r3944realms.leashedplayer.extend.CrossbowItem$ChargeTypeExtend;
import net.minecraft.client.data.models.ItemModelGenerators;
import net.minecraft.client.data.models.ItemModelOutput;
import net.minecraft.client.data.models.model.ItemModelUtils;
import net.minecraft.client.data.models.model.ModelInstance;
import net.minecraft.client.data.models.model.ModelLocationUtils;
import net.minecraft.client.data.models.model.ModelTemplates;
import net.minecraft.client.renderer.item.ItemModel;
import net.minecraft.client.renderer.item.properties.numeric.CrossbowPull;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;

public class ModItemModelGenerator extends ItemModelGenerators {
    private static List<Item> objectList;
    public ModItemModelGenerator(ItemModelOutput itemModelOutput, BiConsumer<ResourceLocation, ModelInstance> modelOutput) {
        super(itemModelOutput, modelOutput);
        objectList = new ArrayList<>();
        init();
    }

    @Override
    public void run() {
        DefaultModItemModelRegister();
        generateTippedArrow(ModItemRegister.TIPPED_LEASH_ROPE_ARROW.get());
        generateCrossbow(Items.CROSSBOW);
        generateBow(Items.BOW);
    }

    private void init() {
        for(ModLangKeyValue obj : ModLangKeyValue.values()) {
            if(!obj.isDefaultItem()) continue;
            objectList.add(obj.getItem());
        }
    }
    /**
     * @implNote <br/>&nbsp;先有纹理才会成功构建
     */
    private void DefaultModItemModelRegister() {
        objectList.forEach(i -> generateFlatItem(i, ModelTemplates.FLAT_ITEM));
    }

    @Override
    public void generateBow(@NotNull Item bowItem) {
        ItemModel.Unbaked itemmodel$unbaked = ItemModelUtils.plainModel(ModelLocationUtils.getModelLocation(bowItem));
        ItemModel.Unbaked itemmodel$unbaked1 = ItemModelUtils.plainModel(this.createFlatItemModel(bowItem, "_pulling_0", ModelTemplates.BOW));
        ItemModel.Unbaked itemmodel$unbaked2 = ItemModelUtils.plainModel(this.createFlatItemModel(bowItem, "_pulling_1", ModelTemplates.BOW));
        ItemModel.Unbaked itemmodel$unbaked3 = ItemModelUtils.plainModel(this.createFlatItemModel(bowItem, "_pulling_2", ModelTemplates.BOW));
        ItemModel.Unbaked itemmodel$unbaked4 = ItemModelUtils.plainModel(
                com.r3944realms.leashedplayer.utils.dataModel.ItemModelUtils.createFlatItemModelByPath(LeashedPlayer.MOD_ID, "item/bow_lra_pulling_0", ModelTemplates.BOW, this.modelOutput)
        );
        ItemModel.Unbaked itemmodel$unbaked5 = ItemModelUtils.plainModel(
                com.r3944realms.leashedplayer.utils.dataModel.ItemModelUtils.createFlatItemModelByPath(LeashedPlayer.MOD_ID, "item/bow_lra_pulling_1", ModelTemplates.BOW, this.modelOutput)

        );
        ItemModel.Unbaked itemmodel$unbaked6 = ItemModelUtils.plainModel(
                com.r3944realms.leashedplayer.utils.dataModel.ItemModelUtils.createFlatItemModelByPath(LeashedPlayer.MOD_ID, "item/bow_lra_pulling_2", ModelTemplates.BOW, this.modelOutput)
        );
        this.itemModelOutput
                .accept(
                        bowItem,
                        ItemModelUtils.conditional(
                                ItemModelUtils.isUsingItem(),
                                com.r3944realms.leashedplayer.utils.dataModel.ItemModelUtils.conditionalRange(
                                        new BowPull(),
                                        0.05F,
                                        itemmodel$unbaked1,
                                        new ConditionalRangeItemModel.Entry[] {
                                                com.r3944realms.leashedplayer.utils.dataModel.ItemModelUtils.override(itemmodel$unbaked4, 0.0F),
                                                com.r3944realms.leashedplayer.utils.dataModel.ItemModelUtils.override(itemmodel$unbaked5, 0.65F),
                                                com.r3944realms.leashedplayer.utils.dataModel.ItemModelUtils.override(itemmodel$unbaked6, 0.9F)
                                        },
                                        new ConditionalRangeItemModel.Entry[] {
                                                com.r3944realms.leashedplayer.utils.dataModel.ItemModelUtils.override(itemmodel$unbaked1, 0.0F),
                                                com.r3944realms.leashedplayer.utils.dataModel.ItemModelUtils.override(itemmodel$unbaked2, 0.65F),
                                                com.r3944realms.leashedplayer.utils.dataModel.ItemModelUtils.override(itemmodel$unbaked3, 0.9F)
                                        }
                                ),
                                itemmodel$unbaked
                        )
                );
    }
    @Override
    public void generateCrossbow(@NotNull Item crossbowItem) {
        ItemModel.Unbaked itemmodel$unbaked = ItemModelUtils.plainModel(ModelLocationUtils.getModelLocation(crossbowItem));
        ItemModel.Unbaked itemmodel$unbaked1 = ItemModelUtils.plainModel(this.createFlatItemModel(crossbowItem, "_pulling_0", ModelTemplates.CROSSBOW));
        ItemModel.Unbaked itemmodel$unbaked2 = ItemModelUtils.plainModel(this.createFlatItemModel(crossbowItem, "_pulling_1", ModelTemplates.CROSSBOW));
        ItemModel.Unbaked itemmodel$unbaked3 = ItemModelUtils.plainModel(this.createFlatItemModel(crossbowItem, "_pulling_2", ModelTemplates.CROSSBOW));
        ItemModel.Unbaked itemmodel$unbaked4 = ItemModelUtils.plainModel(this.createFlatItemModel(crossbowItem, "_arrow", ModelTemplates.CROSSBOW));
        ItemModel.Unbaked itemmodel$unbaked5 = ItemModelUtils.plainModel(this.createFlatItemModel(crossbowItem, "_firework", ModelTemplates.CROSSBOW));
        ItemModel.Unbaked itemmodel$unbaked6 = ItemModelUtils.plainModel(
                com.r3944realms.leashedplayer.utils.dataModel.ItemModelUtils.createFlatItemModelByPath(LeashedPlayer.MOD_ID, "item/crossbow_leash_rope_arrow", ModelTemplates.CROSSBOW, this.modelOutput)
        );
        this.itemModelOutput
                .accept(
                        crossbowItem,
                        ItemModelUtils.conditional(
                                ItemModelUtils.isUsingItem(),
                                ItemModelUtils.rangeSelect(
                                        new CrossbowPull(),
                                        itemmodel$unbaked1,
                                        ItemModelUtils.override(itemmodel$unbaked2, 0.58F),
                                        ItemModelUtils.override(itemmodel$unbaked3, 1.0F)
                                ),
                                ItemModelUtils.select(
                                        new ChargeExtend(),
                                        itemmodel$unbaked,
                                        ItemModelUtils.when(CrossbowItem$ChargeTypeExtend.ARROW, itemmodel$unbaked4),
                                        ItemModelUtils.when(CrossbowItem$ChargeTypeExtend.ROCKET, itemmodel$unbaked5),
                                        ItemModelUtils.when(CrossbowItem$ChargeTypeExtend.LEASH_ROPE_ARROW, itemmodel$unbaked6)
                                )
                        )
                );
    }
}
