local EsoPL = {}
EsoPL.Flags = { "en", "pl", "de", "fr", "es", "ru", "fx" }
EsoPL.Version = "4.093" --RuESO
EsoPL.API = 101050
EsoPL.Name = "EsoPL"
EsoPL.DropdownParameters = {
	["pl"] = "Tylko polski",
	["plen"] = "Polski+angielski",
	["enpl"] = "Angielski+polski",
	["en"] = "Tylko angielski",
}
EsoPL.StringsBackup = {
	["SI_ABILITY_NAME_AND_RANK"] = GetString(SI_ABILITY_NAME_AND_RANK),
	["SI_ABILITY_TOOLTIP_NAME"] = GetString(SI_ABILITY_TOOLTIP_NAME),
	["SI_ITEM_FORMAT_STR_ENCHANT_HEADER_NAMED"] = GetString(SI_ITEM_FORMAT_STR_ENCHANT_HEADER_NAMED),
	["SI_ITEM_FORMAT_STR_ITEM_TRAIT_HEADER"] = GetString(SI_ITEM_FORMAT_STR_ITEM_TRAIT_HEADER),
	["SI_ITEM_FORMAT_STR_ITEM_TRAIT_WITH_ICON_HEADER"] = GetString(SI_ITEM_FORMAT_STR_ITEM_TRAIT_WITH_ICON_HEADER),
	["SI_ITEM_FORMAT_STR_SET_NAME"] = GetString(SI_ITEM_FORMAT_STR_SET_NAME),
	["SI_TOOLTIP_ITEM_NAME"] = GetString(SI_TOOLTIP_ITEM_NAME),
	["SI_ITEM_FORMAT_STR_SET_NAME_NO_COUNT"] = GetString(SI_ITEM_FORMAT_STR_SET_NAME_NO_COUNT),
}

EsoPL.Defaults = {
	Anchor = { BOTTOMRIGHT, BOTTOMRIGHT, 0, 7 },
	-- Misc
	ShowNPC = "plen",
	ShowLocations = "plen",
	ShowCraft = "plen",
	-- Abilities
	ShowAbilitiesMenu = "pl",
	ShowAbilitiesTooltip = "plen",
	-- Champion
	ShowChampionTooltip = "plen",
	-- Items
	ShowItemsNamesTooltip = "plen",
	ShowItemsEnchantsTooltip = "plen",
	ShowItemsTraitsTooltip = "plen",
	ShowItemsSetsTooltip = "plen",
	IsUpdateNeeded = true,
	-- Collections
	EnglishSearch = true,
	ShowCollectionsSetsMenu = "plen",
	--ShowTributeCards = "pl",
	LastSession = true,
	Data = {
		ApiVersion = 0,
		AddonVersion = "",
		Abilities = {},
		Items = {},
		Sets = {},
		SetsNames = {},
		Traits = {},
		Potions = {},
		Locations = {},
		CraftAbilities = {},
		Parts = {},
		Prefixes = {},
		Affixes = {},
		EnchantPrefixes = {},
		--TributeCards = {},
	}
}
EsoPL.DefaultsCharacter = {
	IsFirstLaunch = true
}
EsoPL.Settings = EsoPL.Defaults
EsoPL.SettingsCharacter = EsoPL.DefaultsCharacter

function EsoPL_Change(lang)
	if GetCVar("language.2") ~= lang then
		SetCVar("language.2", lang)
	else
		ZO_Alert(UI_ALERT_CATEGORY_ERROR, SOUNDS.NEGATIVE_CLICK, "Jesteś już przełączony na ten język.")
	end
end

function EsoPL:RefreshUI()
	local flagControl
	local count = 0
	local flagTexture
	for _, flagCode in pairs(self.Flags) do
		flagTexture = "EsoPL/textures/"..flagCode..".dds"
		flagControl = GetControl("EsoPL_FlagControl_"..tostring(flagCode))
		if flagControl == nil then
			flagControl = CreateControlFromVirtual("EsoPL_FlagControl_", EsoPLUI, "EsoPL_FlagControl", tostring(flagCode))
			GetControl("EsoPL_FlagControl_"..flagCode.."Texture"):SetTexture(flagTexture)
			if self:GetLanguage() ~= flagCode then
				flagControl:SetAlpha(0.3)
				if flagControl:GetHandler("OnMouseDown") == nil then flagControl:SetHandler("OnMouseDown", function() EsoPL_Change(flagCode) end) end
			end
		end
		flagControl:ClearAnchors()
		flagControl:SetAnchor(LEFT, EsoPLUI, LEFT, 14 +count*34, 0)
		count = count + 1
	end
	EsoPLUI:SetDimensions(25 +count*34, 50)
	EsoPLUI:SetMouseEnabled(true)
end

function EsoPL:GetLanguage()
	local lang = GetCVar("language.2")
	
	if lang == "pl" or lang == "de" or lang == "fr" or lang == "es" or lang == "ru" or lang == "fx"  then return lang end
	return "en"
	
end

function EsoPL:StartupMessage()
	if self.Settings.IsUpdateNeeded and self:IsDBOld() then
		self:ShowMsgBox("Wymagana aktualizacja bazy danych", "\n\n\n|ac|t256:256:EsoPL/Textures/logo.dds|t\n\n\n\n\n|alBaza danych oryginalnych nazw jest nieaktualna. Aktualizacja zajmie nie więcej niż kilka minut, a gra może się na chwilę zawiesić. Jeśli nie chcesz teraz aktualizować bazy danych, w przyszłości można to zrobić w menu: \nESC -> Ustawienia -> Dodatki -> EsoPL\n\n W tej samej sekcji możesz zmienić ustawienia. Teraz add-on umożliwia wyświetlanie oryginalnych nazw lokalizacji, postaci, informacji o przedmiotach (nazwy, zaklęcia, cechy i zestawy) oraz nazwy umiejętności.", 1)
	end
	
	if self:IsDBOld() and not self.Settings.IsUpdateNeeded then
		d("Baza danych EsoPL jest nieaktualna. Można ją zaktualizować w ustawieniach modyfikacji: ESC-> Ustawienia-> Dodatki-> EsoPL")
	end

	self:Check()
	
	EVENT_MANAGER:UnregisterForEvent("EsoPL_StartupMessage", EVENT_PLAYER_ACTIVATED)
end

function EsoPL:MapNameStyle()		
	if self.Settings.ShowLocations == "plen" or self.Settings.ShowLocations == "enpl" then
		ZO_WorldMapCornerTitle:SetFont("ZoFontWinH3")
	else
		ZO_WorldMapCornerTitle:SetFont("ZoFontWinH1")
	end
	
	local scrollData = ZO_ScrollList_GetDataList(ZO_WorldMapLocationsList)
    ZO_ClearNumericallyIndexedTable(scrollData)
	WORLD_MAP_LOCATIONS_DATA:RefreshLocationList()
	WORLD_MAP_LOCATIONS:BuildLocationList()
end

function EsoPL:OnInit(eventCode, addOnName)	
	if zo_strlower(addOnName) ~= zo_strlower(self.Name) then return end
	EVENT_MANAGER:UnregisterForEvent("EsoPL_OnAddOnLoaded", EVENT_ADD_ON_LOADED)
	
	
	self.Settings = ZO_SavedVars:NewAccountWide("EsoPLVariables", 1, nil, self.Defaults)
	self.SettingsCharacter = ZO_SavedVars:New("EsoPLVariables", 1, nil, self.DefaultsCharacter)
	
	if self.SettingsCharacter.IsFirstLaunch == true then
		SetSetting(SETTING_TYPE_SUBTITLES, SUBTITLE_SETTING_ENABLED_FOR_NPCS, "true")
		SetSetting(SETTING_TYPE_SUBTITLES, SUBTITLE_SETTING_ENABLED_FOR_VIDEOS, "true")
		self.SettingsCharacter.IsFirstLaunch = false
	end
	
	for _, flagCode in pairs(self.Flags) do
		ZO_CreateStringId("SI_BINDING_NAME_"..string.upper(flagCode), string.upper(flagCode))
	end

	self:RefreshUI()
	
	EsoPLUI:ClearAnchors()
	EsoPLUI:SetAnchor(self.Settings.Anchor[1], GuiRoot, self.Settings.Anchor[2], self.Settings.Anchor[3], self.Settings.Anchor[4])
	
	self.LAM = ESOPL_SETTINGS:New(self)
	
	ZO_CreateStringId("SI_BINDING_NAME_ESOPL_EN", "Język angielski")
	ZO_CreateStringId("SI_BINDING_NAME_ESOPL_PL", "Język polski")
	
	if self:GetLanguage() == "pl" then
		EsoPL_init()
	end
	
	function ZO_GameMenu_OnShow(control)
		if control.OnShow then
			control.OnShow(control.gameMenu)
			EsoPLUI:SetHidden(hidden)
		end
	end
	
	function ZO_GameMenu_OnHide(control)
		if control.OnHide then
			control.OnHide(control.gameMenu)
			EsoPLUI:SetHidden(not hidden)
		end
	end
end

function EsoPL:IsDBOld()
	local rsv = self.Settings.Data
	if not rsv.ApiVersion or not rsv.AddonVersion or (rsv.ApiVersion ~= GetAPIVersion()) or (rsv.AddonVersion ~= self.Version) then
		return true
	else
		return false
	end
end

function EsoPL:CloseMsgBox()
	ZO_Dialogs_ReleaseDialog("EsoPLDialog", false)
end

function EsoPL:ShowMsgBox(title, msg, typ)

	local callback = {}

	callback = {
		[1] = 
		{
			keybind = "DIALOG_PRIMARY",
			text = "Zaktualizuj bazę",
			callback =
				function ()
					EsoPL_Dump()
				end,
            clickSound = SOUNDS.DIALOG_ACCEPT,
		},
		[2] =
		{
			keybind = "DIALOG_NEGATIVE",
            text = "Anuluj", 
			callback =
				function ()
					self.Settings.IsUpdateNeeded = false
				end,
            clickSound = SOUNDS.DIALOG_DECLINE,
		},
	}
	
	local confirmDialog = 
	{
		canQueue = true,
		onlyQueueOnce = true,
		gamepadInfo = { dialogType = GAMEPAD_DIALOGS.BASIC },
		title = { text = title },
		mainText = { text = msg },
		buttons = callback
	}
	
	ZO_Dialogs_RegisterCustomDialog("EsoPLDialog", confirmDialog)
	self:CloseMsgBox()
	
	--if IsInGamepadPreferredMode() then
	--	zo_callLater(function()
	--		ZO_Dialogs_ShowGamepadDialog("EsoPLDialog")
	--	end, 500)
	--else
		ZO_Dialogs_ShowDialog("EsoPLDialog")
	--end
end

function EsoPL_init()
	
	if not EsoPL:IsDBOld() then
		
		if EsoPL.Settings.ShowLocations ~= "pl" and EsoPL_doubleNamesLocations then
			LFGDoubleNames(EsoPL)
			EsoPL_doubleNamesLocations(EsoPL)
		end
		
		if EsoPL.Settings.ShowNPC ~= "pl" and EsoPL_doubleNamesNPC then
			EsoPL_doubleNamesNPC(EsoPL)
		end
		
		if EsoPL.Settings.ShowCraft ~= "pl" and EsoPL_doubleNamesBoth then
			EsoPL_doubleNamesBoth(EsoPL)
		end
		
		if (EsoPL.Settings.ShowAbilitiesMenu ~= "pl" or EsoPL.Settings.ShowAbilitiesTooltip ~= "pl") and EsoPL_doubleNamesAbilities then
			EsoPL_doubleNamesAbilities(EsoPL)
		end
		
		if (EsoPL.Settings.ShowChampionMenu ~= "pl" or EsoPL.Settings.ShowChampionTooltip ~= "pl") and EsoPL_doubleNamesChampion then
			EsoPL_doubleNamesChampion(EsoPL)
		end
		
		if (EsoPL.Settings.ShowItemsNamesTooltip ~= "pl" or EsoPL.Settings.ShowItemsEnchantsTooltip ~= "pl" or EsoPL.Settings.ShowItemsTraitsTooltip ~= "pl" or EsoPL.Settings.ShowItemsSetsTooltip ~= "pl") and EsoPL_doubleNamesItems then
			EsoPL_doubleNamesItems(EsoPL)
		end
		
		if (EsoPL.Settings.EnglishSearch or EsoPL.Settings.ShowCollectionsSetsMenu ~= "pl") and EsoPL_doubleNamesCollections then
			EsoPL_doubleNamesCollections(EsoPL)
			ITEM_SET_COLLECTIONS_DATA_MANAGER:SortTopLevelCategories()
			ITEM_SET_COLLECTIONS_DATA_MANAGER:FireCallbacks("CollectionsUpdated")
		end
	end
end

function EsoPL_SaveAnchor()
	local isValidAnchor, point, relativeTo, relativePoint, offsetX, offsetY = EsoPLUI:GetAnchor()
	if isValidAnchor then
		EsoPL.Settings.Anchor = { point, relativePoint, offsetX, offsetY }
	end
end

function EsoPL:MagicReplace(str, what, with)
    what = zo_strgsub(what, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1")
    with = zo_strgsub(with, "[%%]", "%%%%")
    return zo_strgsub(str, what, with)
end

function EsoPL:DumpPl()
	local rsv = EsoPL.Settings.Data
	
	for i = 1, 300000 do

		local hasSet, setName = GetItemLinkSetInfo(string.format("|H1:item:%d:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", i)) 
		
		if hasSet then
			rsv.SetsNames[ZO_CachedStrFormat("<<z:1>>", setName)] = i
		end
	end
	
	for i = 1, #esoplLinks do
		rsv.Potions[zo_strlower(GetItemLinkName(string.format("|H1:item:%s:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", esoplLinks[i])))] = esoplLinks[i]
	end
	
	for i = 1, #esoplParts do
		rsv.Parts[ZO_CachedStrFormat("<<z:1>>", GetItemLinkName(string.format("|H1:item:%d:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", esoplParts[i])))] = esoplParts[i]
	end
	
	for i = 1, #esoplEnchantPrefixes do
		rsv.EnchantPrefixes[EsoPL:MagicReplace(zo_strlower(GetItemLinkName(string.format("|H1:item:%s:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", esoplEnchantPrefixes[i]))), " " .. GetItemLinkName("|H1:item:5364:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h"), "")] = esoplEnchantPrefixes[i]
	end
	
	for i = 1, #esoplPrefixes do
		local str = EsoPL:MagicReplace(ZO_CachedStrFormat("<<z:1>>", GetItemLinkName(string.format("|H1:item:%s:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", esoplPrefixes[i]))), " " .. ZO_CachedStrFormat("<<z:1>>", GetItemLinkName("|H1:item:" .. string.match(esoplPrefixes[i], "^(%d+):") .. ":0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h")), "")
		rsv.Prefixes[str:sub(1, #str - 4)] = esoplPrefixes[i]
	end
	
	for i = 1, #esoplAffixes do
		rsv.Affixes[EsoPL:MagicReplace(ZO_CachedStrFormat("<<z:1>>", GetItemLinkName(string.format("|H1:item:43533:0:0:%d:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", esoplAffixes[i]))), ZO_CachedStrFormat("<<z:1>>", GetItemLinkName("|H1:item:43533:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h")) .. " ", "")] = esoplAffixes[i]
	end
	
	-- Locations
	
	local backupLocations = EsoPL.Settings.ShowLocations
	EsoPL.Settings.ShowLocations = "pl"
	
	local zonesCount = GetNumZones()
	for i = 1, zonesCount do
		local locationName = ZO_CachedStrFormat("<<z:1>>", GetZoneNameByIndex(i))
		if locationName then
			rsv.Locations[locationName] = string.format("zone:%d:0", i)
		end
		
		local POIsCount = GetNumPOIs(i)
		for j = 1, POIsCount do
			local locationName = ZO_CachedStrFormat("<<z:1>>", GetPOIInfo(i, j))
			if locationName then
				rsv.Locations[locationName] = string.format("poi:%d:%d", i, j)
			end
		end
	end
	
	local fastTravelNodesCount = GetNumFastTravelNodes()
	for i = 1, fastTravelNodesCount do
		local _, locationName = GetFastTravelNodeInfo(i)
		if locationName then
			rsv.Locations[ZO_CachedStrFormat("<<z:1>>", locationName)] = string.format("ft:%d:0", i)
		end
	end
	
	for i = 1, 1000 do
		local locationName = ZO_CachedStrFormat("<<z:1>>", GetKeepName(i))
		if locationName then
			rsv.Locations[locationName] = string.format("keep:%d:0", i)
		end
	end
	
	EsoPL.Settings.ShowLocations = backupLocations
	
	EsoPL.Settings["enDump"] = true
	SetCVar("language.2", "en")
end

function EsoPL:DumpEn()
	local rsv = EsoPL.Settings.Data
	
	-- Items
	
	for i = 1, 300000 do
		local itemName = GetItemLinkName(string.format("|H1:item:%d:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", i))
		
		if itemName and itemName ~= "" and not string.match(itemName, "_") then
			rsv.Items[i] = ZO_CachedStrFormat(SI_TOOLTIP_ITEM_NAME, itemName)
		end

		local hasSet, setName, _, _, _, setId = GetItemLinkSetInfo(string.format("|H1:item:%d:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", i))
		
		if hasSet then
			rsv.Sets[setId] = setName
		end

		--[[local _, enchantHeader = GetItemLinkEnchantInfo("|H1:item:" .. i .. ":0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h")
		local enchantId = GetItemLinkFinalEnchantId("|H1:item:" .. i .. ":0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h")

		if enchantHeader and enchantHeader ~= "" and enchantHeader ~= "Enchantment" then
			rsv.Enchants[enchantId] = EsoPL:MagicReplace(enchantHeader, " Enchantment", "")
		end]]
	end
	
	-- Tribute Cards
	
	--[[for i = 1, 1000 do
		local cardName = GetTributeCardName(i)
		
		if cardName and cardName ~= "" then
			rsv.TributeCards[i] = ZO_CachedStrFormat(SI_TOOLTIP_ITEM_NAME, cardName)
		end
	end]]
	
	-- Set Names
	
	for index,value in pairs(rsv.SetsNames) do
		local hasSet, setName = GetItemLinkSetInfo(string.format("|H1:item:%d:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", value))
		rsv.SetsNames[index] = ZO_CachedStrFormat(SI_TOOLTIP_ITEM_NAME, setName)
	end
	
	-- Alchemy
	
	for index,value in pairs(rsv.Potions) do
		rsv.Potions[index] = ZO_CachedStrFormat(SI_TOOLTIP_ITEM_NAME, GetItemLinkName(string.format("|H1:item:%s:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", value)))
	end
	
	-- Item Parts
	
	for index,value in pairs(rsv.Parts) do
		rsv.Parts[index] = ZO_CachedStrFormat(SI_TOOLTIP_ITEM_NAME, GetItemLinkName(string.format("|H1:item:%d:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", value)))
	end
	
	-- Enchantment Prefixes
	
	for index,value in pairs(rsv.EnchantPrefixes) do
		rsv.EnchantPrefixes[index] = ZO_CachedStrFormat(SI_TOOLTIP_ITEM_NAME, EsoPL:MagicReplace(GetItemLinkName(string.format("|H1:item:%s:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", value)), " " .. GetItemLinkName("|H1:item:5364:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h"), ""))
	end
	
	-- Item Prefixes
	
	for index,value in pairs(rsv.Prefixes) do
		rsv.Prefixes[index] = EsoPL:MagicReplace(ZO_CachedStrFormat(SI_TOOLTIP_ITEM_NAME, GetItemLinkName(string.format("|H1:item:%s:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", value))), " " .. ZO_CachedStrFormat(SI_TOOLTIP_ITEM_NAME, GetItemLinkName("|H1:item:" .. string.match(value, "^(%d+):") .. ":0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h")), "")
	end
	
	-- Item Affixes
	
	for index,value in pairs(rsv.Affixes) do
		rsv.Affixes[index] = EsoPL:MagicReplace(ZO_CachedStrFormat(SI_TOOLTIP_ITEM_NAME, GetItemLinkName(string.format("|H1:item:43533:0:0:%d:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h", value))), ZO_CachedStrFormat(SI_TOOLTIP_ITEM_NAME, GetItemLinkName("|H1:item:43533:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0|h|h")) .. " ", "")
	end
	
	-- Locations
	
	for index,value in pairs(rsv.Locations) do
		local locType, locId, locSubId = string.match(value, "^(.*):(%d+):(%d+)$")
		
		if locType and locId and locSubId then
			if locType == "zone" then
				rsv.Locations[index] = ZO_CachedStrFormat(SI_ZONE_NAME, GetZoneNameByIndex(locId))
			elseif locType == "poi" then
				rsv.Locations[index] = ZO_CachedStrFormat(SI_ZONE_NAME, GetPOIInfo(locId, locSubId))
			elseif locType == "keep" then
				rsv.Locations[index] = ZO_CachedStrFormat(SI_ZONE_NAME, GetKeepName(locId))
			elseif locType == "ft" then
				local _, locationName = GetFastTravelNodeInfo(locId)
				rsv.Locations[index] = ZO_CachedStrFormat(SI_ZONE_NAME, locationName)
			end
		end
	end
	
	-- Traits
	
	for i = 1, 100 do
		local traitName = GetString("SI_ITEMTRAITTYPE", i)
		
		if traitName and traitName ~= "" then
			rsv.Traits[i] = traitName
		end
	end
	
	-- Abilities
	
	local numSkillTypes = GetNumSkillTypes()
	
	for i = 1, numSkillTypes do
		local numSkillLines = GetNumSkillLines(i)
		
		for j = 1, numSkillLines do
			local numSkillAbilities = GetNumSkillAbilities(i, j)
			
			for k = 1, numSkillAbilities do
				
				local _, _, _, passive = GetSkillAbilityInfo(i, j, k)
				
				if passive then
					for l = 1,GetNumPassiveSkillRanks(i, j, k) do
						local currentMorphId = GetSpecificSkillAbilityInfo(i, j, k, 0, l)
						rsv.Abilities[currentMorphId] = GetAbilityName(currentMorphId)
					end
				else
					local currentMorphId = GetSpecificSkillAbilityInfo(i, j, k, 0, 1)
					rsv.Abilities[currentMorphId] = GetAbilityName(currentMorphId)
					
					local currentMorphId = GetSpecificSkillAbilityInfo(i, j, k, 1, 1)
					rsv.Abilities[currentMorphId] = GetAbilityName(currentMorphId)
					
					local currentMorphId = GetSpecificSkillAbilityInfo(i, j, k, 2, 1)
					rsv.Abilities[currentMorphId] = GetAbilityName(currentMorphId)
				end
			end
		end
	end
	
	for key,value in pairs(esoplCompanionAbilities) do
		rsv.Abilities[key] = GetAbilityName(key)
	end

	    -- Companion abilities z API
		
    local numSkillTypes = GetNumSkillTypes()
    for skillType = 1, numSkillTypes do
        local numLines = GetNumCompanionSkillLines(skillType) or 0
        for lineIndex = 1, numLines do
            local skillLineId = GetCompanionSkillLineId(skillType, lineIndex)
            if skillLineId then
                local numAbilities = GetNumAbilitiesInCompanionSkillLine(skillLineId) or 0
                for abilityIndex = 1, numAbilities do
                    local abilityId = GetCompanionAbilityId(skillLineId, abilityIndex)
                    if abilityId and abilityId > 0 then
                        rsv.Abilities[abilityId] = GetAbilityName(abilityId)
                    end
                end
            end
        end
    end

	for i = 1, GetNumChampionDisciplines() do
		for j = 1, GetNumChampionDisciplineSkills(i) do
			rsv.Abilities[GetChampionAbilityId(GetChampionSkillId(i, j))] = GetChampionSkillName(GetChampionSkillId(i, j))
		end
	end
	
	EsoPL.Settings["enDump"] = nil
	EsoPL.Settings["success"] = true
	
	rsv.ApiVersion = GetAPIVersion()
	rsv.AddonVersion = EsoPL.Version
	EsoPL.Settings.IsUpdateNeeded = true,
	
	SetCVar("language.2", "pl")
end

function EsoPL:Check()

	local rsv = EsoPL.Settings

	if GetCVar("language.2") == "en" and rsv["enDump"] ~= nil then
		EsoPL:DumpEn()
	end
	
	if GetCVar("language.2") == "pl" and rsv["plDump"] ~= nil then
		rsv["plDump"] = nil
		EsoPL:DumpPl()
	end
	
	if rsv["success"] ~= nil then
		rsv["success"] = nil
	end
end

function EsoPL:IsAddonRunning(addonName)
    local manager = GetAddOnManager()
    for i = 1, manager:GetNumAddOns() do
        local name, _, _, _, _, state = manager:GetAddOnInfo(i)
        if name == addonName and state == ADDON_STATE_ENABLED then
            return true
        end
    end
    return false
end

function EsoPL_Dump()
	local rsd = EsoPL.Settings.Data
	rsd.Sets = {}
	rsd.SetsNames = {}
	rsd.Potions = {}
	rsd.Traits = {}
	rsd.Abilities = {}
	rsd.Items = {}
	rsd.Parts = {}
	rsd.Prefixes = {}
	rsd.Affixes = {}
	rsd.EnchantPrefixes = {}
	rsd.Locations = EsoPL_getLocations()
	rsd.CraftAbilities = EsoPL_getCraftAbilities()
	
	if GetCVar("language.2") == "pl" then
		EsoPL:DumpPl()
	else
		EsoPL.Settings["plDump"] = true
		SetCVar("language.2", "pl")
	end
end

--[[ function EsoPL_Dump_Dev()
	local rsd = EsoPL.Settings.Data
	
	if not rsd.Companions then
		rsd.Companions = {}
	end
	
	local numSkillTypes = GetNumSkillTypes()
	
	for i = 1, numSkillTypes do
		local numSkillLines = GetNumCompanionSkillLines(i)
		
		for j = 1, numSkillLines do
			local skillLineId = GetCompanionSkillLineId(i, j)
			local numSkillAbilities = GetNumAbilitiesInCompanionSkillLine(skillLineId)
			
			for k = 1, numSkillAbilities do
				local currentAbilityId = GetCompanionAbilityId(skillLineId, k)
				rsd.Companions[currentAbilityId] = GetAbilityName(currentAbilityId)
			end
		end
	end
end ]]

EVENT_MANAGER:RegisterForEvent("EsoPL_OnAddOnLoaded", EVENT_ADD_ON_LOADED, function(_event, _name) EsoPL:OnInit(_event, _name) end)
EVENT_MANAGER:RegisterForEvent("EsoPL_StartupMessage", EVENT_PLAYER_ACTIVATED, function(...) EsoPL:StartupMessage() end)