package net.minecraft.util.datafix.fixes;

import com.mojang.datafixers.Typed;
import com.google.common.annotations.VisibleForTesting;
import java.util.List;
import java.util.Iterator;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.commons.lang3.math.NumberUtils;
import com.mojang.datafixers.DataFixUtils;
import com.mojang.serialization.Dynamic;
import java.util.function.Function;
import com.mojang.datafixers.DSL;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.schemas.Schema;
import com.google.common.base.Splitter;
import com.mojang.datafixers.DataFix;

public class LevelFlatGeneratorInfoFix extends DataFix {
    private static final Splitter SPLITTER;
    private static final Splitter LAYER_SPLITTER;
    private static final Splitter OLD_AMOUNT_SPLITTER;
    private static final Splitter AMOUNT_SPLITTER;
    private static final Splitter BLOCK_SPLITTER;
    
    public LevelFlatGeneratorInfoFix(final Schema schema, final boolean boolean2) {
        super(schema, boolean2);
    }
    
    public TypeRewriteRule makeRule() {
        return this.fixTypeEverywhereTyped("LevelFlatGeneratorInfoFix", this.getInputSchema().getType(References.LEVEL), typed -> typed.update(DSL.remainderFinder(), (Function)this::fix));
    }
    
    private Dynamic<?> fix(final Dynamic<?> dynamic) {
        if (dynamic.get("generatorName").asString("").equalsIgnoreCase("flat")) {
            return dynamic.update("generatorOptions", dynamic -> (Dynamic)DataFixUtils.orElse(((Dynamic)dynamic).asString().map((Function)this::fixString).map((Function)dynamic::createString).result(), dynamic));
        }
        return dynamic;
    }
    
    @VisibleForTesting
    String fixString(final String string) {
        if (string.isEmpty()) {
            return "minecraft:bedrock,2*minecraft:dirt,minecraft:grass_block;1;village";
        }
        final Iterator<String> iterator3 = LevelFlatGeneratorInfoFix.SPLITTER.split(string).iterator();
        final String string2 = iterator3.next();
        int integer5;
        String string3;
        if (iterator3.hasNext()) {
            integer5 = NumberUtils.toInt(string2, 0);
            string3 = iterator3.next();
        }
        else {
            integer5 = 0;
            string3 = string2;
        }
        if (integer5 < 0 || integer5 > 3) {
            return "minecraft:bedrock,2*minecraft:dirt,minecraft:grass_block;1;village";
        }
        final StringBuilder stringBuilder7 = new StringBuilder();
        final Splitter splitter8 = (integer5 < 3) ? LevelFlatGeneratorInfoFix.OLD_AMOUNT_SPLITTER : LevelFlatGeneratorInfoFix.AMOUNT_SPLITTER;
        final Splitter splitter9;
        final List<String> list6;
        int integer6;
        String string4;
        final List<String> list7;
        final int integer7;
        final String string5;
        final int n;
        int n2;
        final int integer8;
        final int integer9;
        final int integer10;
        final StringBuilder sb;
        String string6;
        stringBuilder7.append(StreamSupport.<String>stream(LevelFlatGeneratorInfoFix.LAYER_SPLITTER.split(string3).spliterator(), false).map(string -> {
            list6 = splitter9.splitToList(string);
            if (list6.size() == 2) {
                integer6 = NumberUtils.toInt((String)list6.get(0));
                string4 = list6.get(1);
            }
            else {
                integer6 = 1;
                string4 = list6.get(0);
            }
            list7 = LevelFlatGeneratorInfoFix.BLOCK_SPLITTER.splitToList(string4);
            integer7 = (list7.get(0).equals("minecraft") ? 1 : 0);
            string5 = list7.get(integer7);
            if (n == 3) {
                n2 = EntityBlockStateFix.getBlockId("minecraft:" + string5);
            }
            else {
                n2 = NumberUtils.toInt(string5, 0);
            }
            integer8 = n2;
            integer9 = integer7 + 1;
            integer10 = ((list7.size() > integer9) ? NumberUtils.toInt((String)list7.get(integer9), 0) : 0);
            sb = new StringBuilder();
            if (integer6 == 1) {
                string6 = "";
            }
            else {
                string6 = integer6 + "*";
            }
            return sb.append(string6).append(BlockStateData.getTag(integer8 << 4 | integer10).get("Name").asString("")).toString();
        }).collect(Collectors.joining(",")));
        while (iterator3.hasNext()) {
            stringBuilder7.append(';').append(iterator3.next());
        }
        return stringBuilder7.toString();
    }
    
    static {
        SPLITTER = Splitter.on(';').limit(5);
        LAYER_SPLITTER = Splitter.on(',');
        OLD_AMOUNT_SPLITTER = Splitter.on('x').limit(2);
        AMOUNT_SPLITTER = Splitter.on('*').limit(2);
        BLOCK_SPLITTER = Splitter.on(':').limit(3);
    }
}
