package com.hypixel.hytale.builtin.hytalegenerator.assets.materialproviders;

import com.hypixel.hytale.builtin.hytalegenerator.material.Material;
import com.hypixel.hytale.builtin.hytalegenerator.materialproviders.DownwardDepthMaterialProvider;
import com.hypixel.hytale.builtin.hytalegenerator.materialproviders.MaterialProvider;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import javax.annotation.Nonnull;

public class DownwardDepthMaterialProviderAsset extends MaterialProviderAsset {
   public static final BuilderCodec<DownwardDepthMaterialProviderAsset> CODEC = BuilderCodec.builder(
         DownwardDepthMaterialProviderAsset.class, DownwardDepthMaterialProviderAsset::new, MaterialProviderAsset.ABSTRACT_CODEC
      )
      .append(new KeyedCodec<>("Depth", Codec.INTEGER, true), (t, k) -> t.depth = k, k -> k.depth)
      .add()
      .append(new KeyedCodec<>("Material", MaterialProviderAsset.CODEC, true), (t, k) -> t.materialProviderAsset = k, k -> k.materialProviderAsset)
      .add()
      .build();
   private int depth = 0;
   private MaterialProviderAsset materialProviderAsset = new ConstantMaterialProviderAsset();

   public DownwardDepthMaterialProviderAsset() {
   }

   @Nonnull
   @Override
   public MaterialProvider<Material> build(@Nonnull MaterialProviderAsset.Argument argument) {
      return (MaterialProvider<Material>)(super.skip()
         ? MaterialProvider.noMaterialProvider()
         : new DownwardDepthMaterialProvider<>(this.materialProviderAsset.build(argument), this.depth));
   }

   @Override
   public void cleanUp() {
      this.materialProviderAsset.cleanUp();
   }
}
