require('NPCs/MainCreationMethods');
require('OptionScreens/CharacterCreationProfession');
require('XpSystem/XpUpdate');
require"ISUI/ISPanel"
require"ISUI/ISButton"
require"ISUI/ISInventoryPane"
require"ISUI/ISResizeWidget"
require"ISUI/ISMouseDrag"
require"defines"
require "Insurgent/ProfessionExclusiveTrait"

local function tableContains(t, e)
    for _, value in pairs(t) do
        if value == e then
            return true
        end
    end
    return false
end

local function tableContainsTrait(t, e)
    for _, value in pairs(t) do
        if value.item:getLabel() == e then
            print("Found value in table, returning " .. value.item:getLabel())
            return true
        end
    end
    return false
end

local function filterProfTraits(self, profession)
    print("Attempting to filter traits")
    local addTraitToNew = false;

    --Go through exclusive traits, if the current prof is used for the trait and not in the listbox, add it, otherwise, remove it
    for i,traitToCheck in ipairs(ProfessionExclusiveTrait.List) do
        addTraitToNew = false;
        for k,prof in ipairs(traitToCheck.exclusiveProf) do
            print("checking Prof = " .. prof)
            if prof == nil then break end
            if profession == prof then
                print("profName = " .. prof)
                print("traitName = " .. traitToCheck.traitName)
                addTraitToNew = true;
                break
            else
                addTraitToNew = false;
            end
        end 
        if addTraitToNew and not tableContainsTrait(self.listboxTrait.items, TraitFactory.getTrait(traitToCheck.traitName):getLabel()) and not tableContainsTrait(self.listboxTraitSelected.items, TraitFactory.getTrait(traitToCheck.traitName):getLabel()) and TraitFactory.getTrait(traitToCheck.traitName):getCost() > 0 then
            
            local newTrait = self.listboxTrait:addItem(TraitFactory.getTrait(traitToCheck.traitName):getLabel(), TraitFactory.getTrait(traitToCheck.traitName));
            newTrait.tooltip = TraitFactory.getTrait(traitToCheck.traitName):getDescription();

        elseif not addTraitToNew and tableContainsTrait(self.listboxTrait.items, TraitFactory.getTrait(traitToCheck.traitName):getLabel()) and TraitFactory.getTrait(traitToCheck.traitName):getCost() > 0 then
            
            self.listboxTrait:removeItem(TraitFactory.getTrait(traitToCheck.traitName):getLabel());
            self.listboxTrait.selected = -1;

        elseif addTraitToNew and not tableContainsTrait(self.listboxBadTrait.items, TraitFactory.getTrait(traitToCheck.traitName):getLabel()) and not tableContainsTrait(self.listboxTraitSelected.items, TraitFactory.getTrait(traitToCheck.traitName):getLabel()) and TraitFactory.getTrait(traitToCheck.traitName):getCost() < 0 then
            
            local newTrait = self.listboxBadTrait:addItem(TraitFactory.getTrait(traitToCheck.traitName):getLabel(), TraitFactory.getTrait(traitToCheck.traitName));
            newTrait.tooltip = TraitFactory.getTrait(traitToCheck.traitName):getDescription();

        elseif not addTraitToNew and tableContainsTrait(self.listboxBadTrait.items, TraitFactory.getTrait(traitToCheck.traitName):getLabel()) and  TraitFactory.getTrait(traitToCheck.traitName):getCost() < 0 then
            
            self.listboxBadTrait:removeItem(TraitFactory.getTrait(traitToCheck.traitName):getLabel());
            self.listboxBadTrait.selected = -1;

        end
        if not addTraitToNew and tableContainsTrait(self.listboxTraitSelected.items, TraitFactory.getTrait(traitToCheck.traitName):getLabel()) and not TraitFactory.getTrait(traitToCheck.traitName):isFree() then
            
            self.listboxTraitSelected:removeItem(TraitFactory.getTrait(traitToCheck.traitName):getLabel());
            self.listboxTraitSelected.selected = -1;
            self.pointToSpend = self.pointToSpend + TraitFactory.getTrait(traitToCheck.traitName):getCost();
        end
    end
end

local original_onSelectProf = CharacterCreationProfession.onSelectProf
function CharacterCreationProfession:onSelectProf(item)
    original_onSelectProf(self, item)

	local prof = self.profession:getType();
    filterProfTraits(self, prof);
    CharacterCreationMain.sort(self.listboxTrait.items);
    CharacterCreationMain.invertSort(self.listboxBadTrait.items);
    CharacterCreationMain.sort(self.listboxTraitSelected.items);
end