package com.zivilon.cinder_loe;

import lotr.common.LOTRLevelData;
import lotr.common.entity.LOTREntities;
import lotr.common.entity.npc.LOTRBannerBearer;
import lotr.common.entity.npc.LOTRHireableBase;
import lotr.common.entity.npc.LOTRUnitTradeEntry;
import lotr.common.fac.LOTRFaction;
import lotr.common.item.LOTRItemCoin;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.config.Configuration;

import java.io.File;

public class CinderUnitTradeEntry extends LOTRUnitTradeEntry {

    public Class entityClass;
    private String name;
    private PledgeType pledgeType = PledgeType.NONE;
    private String objectivename;
    private static  Configuration config;
    private String extraInfo;

    static {
        config = new Configuration(new File("CinderLoE.cfg"));
        config.load();
    }

    public CinderUnitTradeEntry(Class c, int cost, float alignment) {
        super(c, cost, alignment);
        this.entityClass = c;  // Set entityClass to the passed class

        if (LOTRBannerBearer.class.isAssignableFrom(this.entityClass)) {
            this.setExtraInfo("Banner");
        }
    }

    public CinderUnitTradeEntry(Class c, Class c1, String s, int cost, float alignment) {
        super(c, cost, alignment);
        this.entityClass = c;  // Set entityClass to the passed class
        this.mountClass = c1;
        this.name = s;
    }

    public boolean isObjectiveComplete(String objective) {
        return config.getBoolean(objective, Configuration.CATEGORY_GENERAL, false, "Set true if " + objective + " is complete");
    }

    @Override
    public boolean hasRequiredCostAndAlignment(EntityPlayer entityplayer, LOTRHireableBase trader) {
        int coins = LOTRItemCoin.getInventoryValue(entityplayer, false);
        if (coins < this.getCost(entityplayer, trader)) {
            return false;
        }
        LOTRFaction fac = trader.getFaction();
        if (!this.pledgeType.canAcceptPlayer(entityplayer, fac)) {
            return false;
        }
        if (objectivename != null && !isObjectiveComplete(objectivename)) {
            return false;
        }
        float alignment = LOTRLevelData.getData(entityplayer).getAlignment(fac);
        return alignment >= this.alignmentRequired;
    }

    public CinderUnitTradeEntry setObjective(String objective) {
        this.objectivename = objective;
        return this;
    }

    public String getFormattedExtraInfo() {
        return StatCollector.translateToLocal((String) "lotr.unitinfo." + this.objectivename);
    }

    public LOTRUnitTradeEntry setExtraInfo(String s) {
        this.extraInfo = s;
        return this;
    }

    public boolean hasExtraInfo() {
        return this.extraInfo != null;
    }

    public String getUnitTradeName() {
        if (this.mountClass == null) {
            String entityName = LOTREntities.getStringFromClass(this.entityClass);
            return StatCollector.translateToLocal((String)("entity." + entityName + ".name"));
        }
        return StatCollector.translateToLocal((String)("lotr.unit." + this.name));
    }

}
