local bs = AceLibrary("Babble-Spell-2.2")
local AceAddon = AceLibrary("AceAddon-2.0")
local Tablet = AceLibrary("Tablet-2.0")
local BO = AceLibrary("Babble-Ore-2.2")
local Talismonger = AceLibrary("Talismonger-3.0")

local L = AceLibrary("AceLocale-2.2"):new("Cartographer_Mining")
L:RegisterTranslations("enUS", function() return {
	["Filter"] = true,
	["Filter out minerals"] = true,
  	["Module description"] = "Mining mapnotes for Cartographer (based on PinPoint by Ammo)",
	["Select all"] = true,
	["Select none"] = true,
	["Icon alpha"] = true,
	["Alpha transparency of the icon"] = true,
	["Icon size"] = true,
	["Size of the icons on the map"] = true,
	["Show minimap icons"] = true,
	["Show mining icons on the minimap"] = true,
} end)

L:RegisterTranslations("deDE", function() return {
	["Filter"] = "Filter",
	["Filter out minerals"] = "Filtere Mineralien",
  	["Module description"] = "Bergbau-Kartennotizen f\195\188r Cartographer (basierend auf PinPoint von Ammo)",
	["Select all"] = "Alle ausw\195\164hlen",
	["Select none"] = "Nichts ausw\195\164hlen",
  	["Icon alpha"] = "Transparenz",
	["Alpha transparency of the icon"] = "Transparenz der Symbole.",
	["Icon size"] = "Symbolgr\195\182\195\159e",
	["Size of the icons on the map"] = "Gr\195\182\195\159e der Symbole auf der Weltkarte.",
  	["Show minimap icons"] = "Minimap-Symbole anzeigen",
	["Show mining icons on the minimap"] = "Erzsymbole auf der Minimap anzeigen.",
} end)

L:RegisterTranslations("frFR", function() return {
	["Filter"] = "Filtrer",
	["Filter out minerals"] = "Filtrer les minerais",
	["Module description"] = "Notes de minage pour Cartographer (bas\195\169 sur PinPoint par Ammo)",
	["Select all"] = "Tout s\195\169lectionner",
	["Select none"] = "Ne rien s\195\169lectionner",
	["Icon alpha"] = "Transparence des ic\195\180nes",
	["Alpha transparency of the icon"] = "D\195\169termine la transparence des ic\195\180nes.",
	["Icon size"] = "Taille des ic\195\180nes",
	["Size of the icons on the map"] = "D\195\169termine la taille des ic\195\180nes sur la carte.",
	["Show minimap icons"] = "Afficher les ic\195\180nes sur la minicarte",
	["Show mining icons on the minimap"] = "Affiche les ic\195\180nes des mines sur la minicarte.",
} end)

L:RegisterTranslations("esES", function() return {
	["Filter"] = "Filtro",
	["Filter out minerals"] = "Filtrar minerales",
  	["Module description"] = "Notas de mapa de Miner\195\173a para Cartographer (basado en PinPoint por Ammo)",
	["Select all"] = "Seleccionar todo",
	["Select none"] = "No seleccionar nada",
	["Icon alpha"] = "Transparencia de icono",
	["Alpha transparency of the icon"] = "Transparencia de los iconos",
	["Icon size"] = "Tama\195\177o de icono",
	["Size of the icons on the map"] = "Tama\195\177o de los iconos en el mapa",
    ["Show minimap icons"] = "Mostrar iconos en minimapa",
    ["Show mining icons on the minimap"] = "Muestra los iconos de miner\195\173a en el minimapa",
} end)

L:RegisterTranslations("koKR", function() return {
	["Filter"] = "항목 분류",
	["Filter out minerals"] = "채광(광맥)을 항목에 따라 분류하여 표시합니다.",
  	["Module description"] = "맵노트에 채광(광맥)을 표시합니다.",
	["Select all"] = "모두 선택",
	["Select none"] = "선택 해제",
	["Icon alpha"] = "아이콘 투명도",
	["Alpha transparency of the icon"] = "아이콘의 투명도를 조절합니다.",
	["Icon size"] = "아이콘 크기",
	["Size of the icons on the map"] = "아이콘의 크기를 조절합니다.",
	["Show minimap icons"] = "미니맵 아이콘 보기",
	["Show mining icons on the minimap"] = "미니맵에 광맥 아이콘을 표시합니다.",
} end)

L:RegisterTranslations("zhTW", function() return {
	["Filter"] = "篩檢",
	["Filter out minerals"] = "篩檢礦石",
	["Select all"] = "全部選擇",
	["Select none"] = "全部取消",

} end)

L:RegisterTranslations("zhCN", function() return {
--	["Filter"] = true,
--	["Filter out minerals"] = true,

--	["Select all"] = true,
--	["Select none"] = true,

--	["Iron Deposit"] = "??石",
--	["Mithril Deposit"] = "秘???",
--	["Truesilver Deposit"] = "???石",
--	["Small Thorium Vein"] = "瑟???",
--	["Hakkari Thorium Vein"] = "哈??瑟???",
--	["Rich Thorium Vein"] = "富瑟??",
} end)

local mod = Cartographer:NewModule("Mining", "AceConsole-2.0", "AceEvent-2.0")
local icon_path = "Interface\\AddOns\\Cartographer_Mining\\Artwork\\"
local icon_list = {
	["Copper Vein"] = "INV_Ore_Copper_01",
	["Tin Vein"] = "INV_Ore_Tin_01",
	["Iron Deposit"] = "INV_Ore_Iron_01",
	["Silver Vein"] = "INV_Stone_16",
	["Ooze Covered Silver Vein"] = "INV_Stone_16",
	["Gold Vein"] = "INV_Ore_Gold_01",
	["Ooze Covered Gold Vein"] = "INV_Ore_Gold_01",
	["Mithril Deposit"] = "INV_Ore_Mithril_02",
	["Ooze Covered Mithril Deposit"] = "INV_Ore_Mithril_02",
	["Truesilver Deposit"] = "INV_Ore_TrueSilver_01",
	["Ooze Covered Truesilver Deposit"] = "INV_Ore_TrueSilver_01",
	["Small Thorium Vein"] = "INV_Ore_Thorium_02",
	["Ooze Covered Thorium Vein"] = "INV_Ore_Thorium_02",
	["Hakkari Thorium Vein"] = "INV_Ore_RichThorium_01",
	["Rich Thorium Vein"] = "INV_Ore_RichThorium_01",
	["Ooze Covered Rich Thorium Vein"] = "INV_Ore_RichThorium_01",
	["Dark Iron Deposit"] = "INV_Ore_Mithril_01",
	["Indurium Mineral Vein"] = "INV_Ore_Mithril_01",
	["Lesser Bloodstone Deposit"] = "INV_Ore_Thorium_01",
	["Incendicite Mineral Vein"] = "INV_Ore_Thorium_01",
	["Fel Iron Deposit"] = "INV_Ore_FelIron",
	["Adamantite Deposit"] = "INV_Ore_Adamantium",
	["Rich Adamantite Deposit"] = "INV_Ore_Adamantium_01",
	["Khorium Vein"] = "INV_Ore_Khorium",
}
local reverse_list = {
	["Copper"] = "Copper Vein",
	["Tin"] = "Tin Vein",
	["Iron"] = "Iron Deposit",
	["Silver"] = "Silver Vein",
	["Gold"] = "Gold Vein",
	["Mithril"] = "Mithril Deposit",
	["Truesilver"] = "Truesilver Deposit",
	["Thorium"] = "Small Thorium Vein",
	["Rich Thorium"] = "Rich Thorium Vein",
	["Dark Iron"] = "Dark Iron Deposit",
	["Indurium"] = "Indurium Mineral Vein",
	["Lesser Bloodstone"] = "Lesser Bloodstone Deposit",
	["Incendicite"] = "Incendicite Mineral Vein",
	["Fel Iron"] = "Fel Iron Deposit",
	["Adamantite"] = "Adamantite Deposit",
	["Rich Adamantite"] = "Rich Adamantite Deposit",
	["Khorium"] = "Khorium Vein",
}
mod.icon = {}
function mod:OnInitialize()
	self.db = Cartographer:AcquireDBNamespace("Mining")
	Cartographer:RegisterDefaults("Mining", "profile", {
		filter = {
			['*'] = true,
		},
		iconAlpha = 1,
		iconScale = 1,
		minicons = true,
	})

	local aceopts = {}
	aceopts.toggle = {
		name = AceLibrary("AceLocale-2.2"):new("Cartographer")["Enabled"],
		desc = AceLibrary("AceLocale-2.2"):new("Cartographer")["Suspend/resume this module."],
		type  = 'toggle',
		order = -1,
		get   = function() return Cartographer:IsModuleActive(self) end,
		set   = function() Cartographer:ToggleModuleActive(self) end,
	}
	aceopts.trans = {
		name = L["Icon alpha"],
		desc = L["Alpha transparency of the icon"],
		type = 'range',
		min = 0.1,
		max = 1,
		step = 0.05,
		isPercent = true,
		get = "GetIconAlpha",
		set = "SetIconAlpha",
		order = 1
	}
	aceopts.scal = {
		name = L["Icon size"],
		desc = L["Size of the icons on the map"],
		type = 'range',
		min = 0.5,
		max = 2,
		step = 0.05,
		isPercent = true,
		get = "GetIconScale",
		set = "SetIconScale",
		order = 1
	}
	aceopts.minicons = {
		name = L["Show minimap icons"],
		desc = L["Show mining icons on the minimap"],
		type = 'toggle',
		set = "ToggleShowingMinimapIcons",
		get = "IsShowingMinimapIcons",
		order = 1,
	}

	aceopts.filter = {
		name = L["Filter"],
		desc = L["Filter out minerals"],
		type = 'group',
		args = {
			all = {
				name = L["Select all"],
				desc = L["Select all"],
				type = 'execute',
				func = function()
					for k in pairs(self.icon) do
						self:ToggleShowingMineral(k, true)
					end
				end,
				order = 1,
			},
			none = {
				name = L["Select none"],
				desc = L["Select none"],
				type = 'execute',
				func = function()
					for k in pairs(self.icon) do
						self:ToggleShowingMineral(k, false)
					end
				end,
				order = 2,
			},
			blank = {
				type = 'header',
				order = 3,
			}
		}
	}
	for k,v in pairs(icon_list) do
		mod.icon[k] = {
			text = BO[k],
			path = icon_path..v,
			width = 14,
			height = 14,
		}
		local k = k
		aceopts.filter.args[k] = {
			name = BO[k],
			desc = BO[k],
			type = 'toggle',
			get = function()
				return self:IsShowingMineral(k)
			end,
			set = function(value)
				return self:ToggleShowingMineral(k, value)
			end,
		}
		Talismonger:TrackNode(BO[k])
	end
	
	Cartographer.options.args["Mining"] = {
		name = bs["Mining"],
		desc = L["Module description"],
		type = 'group',
		args = aceopts,
		handler = self,
	}
	Cartographer:GetModule('Professions').addons[bs["Mining"]] = self
	AceLibrary("AceConsole-2.0"):InjectAceOptionsTable(self, Cartographer.options.args["Mining"])

	if not Cartographer_MiningDB then
		Cartographer_MiningDB = {}
	else
		local db_version = Cartographer_MiningDB.version
		for _, zone in pairs(Cartographer_MiningDB) do
			if type(zone) == "table" then
				for id, data in pairs(zone) do
					if reverse_list[data] then
						zone[id] = reverse_list[data]
					end
					if BO:HasReverseTranslation(data) and reverse_list[BO.reverse[data]] then
						zone[id] = reverse_list[BO.reverse[data]]
					end
				end
			end
		end
	end
end

function mod:OnEnable()
	if Cartographer_Notes then
		if not self.iconsregistered then
			for k,v in pairs(self.icon) do
				Cartographer_Notes:RegisterIcon(k, v)
			end
			self.iconsregistered = true
		end

		Cartographer_Notes:RegisterNotesDatabase('Mining', Cartographer_MiningDB, self)
	else
		Cartographer:ToggleModuleActive(self, false)
	end
	self:RegisterEvent("Talismonger_Node")
end

function mod:OnDisable()
	self:UnregisterAllEvents()
	if Cartographer_Notes then
		Cartographer_Notes:UnregisterNotesDatabase('Mining')
	end
end

function mod:IsRareSpawn(node)
	return (node == "Silver Vein" or node == "Truesilver Deposit" or node == "Gold Vein" or node == "Ooze Covered Truesilver Deposit" or node == "Ooze Covered Silver Vein" or node == "Ooze Covered Gold Vein" or node == "Khorium Vein")
end
function mod:SpawnsRare(newNode, oldNode)
	if  newNode == "Silver Vein" and oldNode == "Tin Vein" then
		return true
	end
	if newNode == "Truesilver Deposit" and (oldNode == "Mithril Deposit" or oldNode == "Small Thorium Vein" or oldNode == "Rich Thorium Vein") then
		return true
	end
	if newNode == "Gold Vein" and (oldNode == "Mithril Deposit" or oldNode == "Iron Deposit") then
		return true
	end
	if (newNode == "Ooze Covered Truesilver Deposit" or newNode == "Ooze Covered Silver Vein" or newNode == "Ooze Covered Gold Vein") and (oldNode == "Ooze Covered Thorium Vein" or oldNode == "Ooze Covered Rich Thorium Vein") then
		return true
	end
	if newNode == "Khorium Vein" and (oldNode == "Adamantite" or oldNode == "Fel Iron" or oldNode == "Rich Adamantite") then
		return true
	end 
end

function mod:SetNote(what)
	local x, y = GetPlayerMapPosition("player")
	if x == 0 and y == 0 then return end
	local zone = GetRealZoneText()
	local addNode = true
	for zone, x_p, y_p, icon, creator, data in Cartographer_Notes:IterateNearbyNotes(zone, x, y, 10, "Mining", nil, true) do
		if icon == what then
			Cartographer_Notes:DeleteNote(zone, x_p, y_p)
		elseif self:SpawnsRare(what,icon) then
			Cartographer_Notes:DeleteNote(zone, x_p, y_p)
		elseif self:IsRareSpawn(icon) and not self:IsRareSpawn(what) then
			addNode = false
		end
	end
	if addNode then
		Cartographer_Notes:SetNote(zone, x, y, what, "Mining")
	end
end

function mod:OnNoteTooltipRequest(zone, id, mineral, minimap)
	if not BO:HasTranslation(mineral) then
		if reverse_list[mineral] then
			mineral = reverse_list[mineral]
		end
	end
	Tablet:SetTitle(BO[mineral])
	Tablet:SetTitleColor(0, 0.8, 0)
	if Cartographer_Notes:IsShowingCreator() and not minimap then
		Tablet:AddCategory(
			'columns', 2,
			'hideBlankLine', true
		):AddLine(
			'text', AceLibrary("AceLocale-2.2"):new("Cartographer-Notes")["Created by"],
			'text2', bs["Mining"]
		)
	end
end

function mod:RegisterOre(what)
	if what and what:len() > 0 then
		self:SetNote(BO.reverse[what])
	end
end

function mod:Talismonger_Node(spell,what)
	if spell == "Mining" then
		self:RegisterOre(what)
	end
end

function mod:IsNoteHidden(zone, id, mineral)
	return not self.db.profile.filter[mineral]
end

function mod:IsShowingMineral(mineral)
	return self.db.profile.filter[mineral]
end

function mod:ToggleShowingMineral(mineral, value)
	if value == nil then
		value = not self.db.profile.filter[mineral]
	end
	self.db.profile.filter[mineral] = value

	Cartographer_Notes:RefreshMap()
end
function mod:GetIconScale()
	return self.db.profile.iconScale
end

function mod:SetIconScale(value)
	self.db.profile.iconScale = value
	Cartographer_Notes:RefreshMap()
end
function mod:GetIconAlpha()
	return self.db.profile.iconAlpha
end

function mod:SetIconAlpha(value)
	self.db.profile.iconAlpha = value
	Cartographer_Notes:RefreshMap()
end

function mod:GetNoteTransparency(zone,id,data)
	return self.db.profile.iconAlpha
end
function mod:GetNoteScaling(zone,id,data)
	return self.db.profile.iconScale
end
function mod:IsShowingMinimapIcons()
	return self.db.profile.minicons
end
function mod:ToggleShowingMinimapIcons()
	self.db.profile.minicons = not self.db.profile.minicons
end
function mod:IsMiniNoteHidden(zone,id,data)
	return not self.db.profile.minicons
end
