package dev.crucifixbeta.core.impl;

import dev.crucifixbeta.core.Manager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.item.Items;
import org.apache.commons.io.IOUtils;

@Environment(EnvType.CLIENT)
public class TradeManager extends Manager {
   private final ArrayList<String> list = new ArrayList<>();

   public TradeManager() {
      this.read();
   }

   public ArrayList<String> getList() {
      return this.list;
   }

   public void clear() {
      this.list.clear();
   }

   public boolean inWhitelist(String name) {
      return this.list.contains(name)
         || this.list
            .contains(
               name.replace("block.minecraft.", "").replace("item.minecraft.", "")
            );
   }

   public void remove(String name) {
      name = name.replace("block.minecraft.", "").replace("item.minecraft.", "");
      this.list.remove(name);
   }

   public void add(String name) {
      if (!this.list
         .contains(
            name = name.replace("block.minecraft.", "").replace("item.minecraft.", "")
         )) {
         this.list.add(name);
      }
   }

   public void read() {
      try {
         File var1 = getFile("trades.txt");
         if (!var1.exists()) {
            this.add(Items.ENCHANTED_BOOK.getTranslationKey());
            this.add(Items.DIAMOND_BLOCK.getTranslationKey());
            return;
         }

         for (String var4 : IOUtils.readLines(new FileInputStream(var1), StandardCharsets.UTF_8)) {
            this.add(var4);
         }
      } catch (Exception var5) {
         var5.printStackTrace();
      }
   }

   public void save() {
      try {
         File var1 = getFile("trades.txt");
         PrintWriter var2 = new PrintWriter(new OutputStreamWriter(new FileOutputStream(var1), StandardCharsets.UTF_8));

         for (String var4 : this.list) {
            var2.println(var4);
         }

         var2.close();
      } catch (Exception var5) {
         var5.printStackTrace();
      }
   }
}
