package de.siphalor.was.content.quest;

import de.siphalor.was.content.product.Product;
import de.siphalor.was.content.product.ProductManager;
import de.siphalor.was.content.product.ProductType;
import de.siphalor.was.util.Util;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;

public class StaticQuestGenerator implements QuestGenerator {
	@NotNull
	private final List<Quest> quests;
	private int index = 0;

	public StaticQuestGenerator(@NotNull List<Quest> quests) {
		this.quests = quests;
	}

	@Nullable
	public static StaticQuestGenerator fromCsv(@NotNull InputStream inputStream, @NotNull ProductManager productManager) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		String line;

		List<Quest> quests = new ArrayList<>();

		try {
			while ((line = reader.readLine()) != null) {
				try {
					String[] parts = line.split(",");
					if (parts.length >= 3) {
						Quest.Type type = Quest.Type.valueOf(parts[0].toUpperCase(Locale.ENGLISH));
						ProductType<?> productType = productManager.get(parts[1]);
						if (productType != null) {
							int reward = Integer.parseInt(parts[2]);

							Product product = productType.getProduct(Arrays.copyOfRange(parts, 3, parts.length));

							if (product != null) {
								quests.add(new Quest(type, reward, product));
							} else {
								Util.LOGGER.log(Level.WARNING, "Invalid product in quest: " + line);
							}
						} else {
							Util.LOGGER.log(Level.WARNING, "Invalid product in quest: " + parts[2]);
						}
					} else {
						Util.LOGGER.log(Level.WARNING, "Invalid line in quests file: " + line);
					}
				} catch (Exception e) {
					Util.LOGGER.log(Level.WARNING, "Failed to load quest in csv file in line: " + line);
					e.printStackTrace();
				}
			}
			return new StaticQuestGenerator(quests);
		} catch (IOException e) {
			Util.LOGGER.log(Level.WARNING, "Failed to load quest csv file:");
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void restart() {
		index = 0;
	}

	@Override
	public Quest nextElement() {
		return quests.get(index++);
	}

	@Override
	public boolean hasMoreElements() {
		return index < quests.size();
	}
}
