local addonName, addon = ...
local childName = addon['childName']
local child = addon[childName]
local events = child['events']
local db, player
local L = LibStub("AceLocale-3.0"):GetLocale(childName, true)
LibStub("zzHelper"):Embed(child)
child['statusItem'] = {} 
local function prepareDB()
	db = addon['db']['profile'][childName]
	child['db'] = db
	addon['callbacks']:Fire(format("Init%s", childName))
end

local slotNames = {
	"HeadSlot","NeckSlot","ShoulderSlot","BackSlot","ChestSlot","ShirtSlot","TabardSlot",
	"WristSlot","HandsSlot","WaistSlot","LegsSlot","FeetSlot","Finger0Slot","Finger1Slot",
	"Trinket0Slot","Trinket1Slot","MainHandSlot","SecondaryHandSlot",
	"Bag0Slot","Bag1Slot","Bag2Slot","Bag3Slot" 
}
local function getGBLimit()
	local amount = GetGuildBankWithdrawMoney()
	local guildBankMoney = GetGuildBankMoney()
	if ( amount == -1 ) then
		amount = guildBankMoney
	else
		amount = min(amount, guildBankMoney)
	end
	return amount
end
function events:MERCHANT_SHOW()
	if(CanMerchantRepair()) then
		local m = GetMoney()
		local r = GetRepairAllCost()
		local gbr = false
		if(db['guildbankrepair']) then
			if(CanGuildBankRepair()) then
				gbr = true
			end
		end
		if(r > 0 and (m > r or gbr) and db['autorepair']) then
			if(gbr) then
				if(getGBLimit() >= r) then
					RepairAllItems(1)
					print(L['repaircostgb'](r))
				else
					if(m > r) then
						RepairAllItems()
						print(L['repaircost'](r))
					else
						print(child:colorize(L['Money low'],"ff0000"))
					end
				end
			else
				RepairAllItems()
				print(L['repaircost'](r))
			end
			db['repairstats'] = db['repairstats'] or {}
			db['repairstats'][player] = db['repairstats'][player] or 0
			db['repairstats'][player] = db['repairstats'][player] + r
		end
	end
end
function child:pickColor(v)
  local col = 'ffffff'
  if(v > 90) then
    col = '00ff00' -- grün
  elseif(v > 40) then
    col = 'ffff00' -- gelb
  elseif(v > 0) then
    col = 'ff9900' -- orange
  else
    col = 'ff0000' -- rot
  end
  return col
end
function events:UPDATE_INVENTORY_DURABILITY()
	local percent = 0
	local total = 0
	local cur = 0
	local lowest = 100
	
	local statusItem = child['statusItem']
	wipe(statusItem)
	for k,v in pairs(slotNames) do
		local current, maximum = GetInventoryItemDurability(GetInventorySlotInfo(v))
		if(maximum) then
			total = total + maximum
			cur = cur + current
			statusItem[v] = child:round(current / (maximum/100),0)
			lowest = min(statusItem[v], lowest)
		end
	end

	if(db['showlowest']) then
		percent = lowest
	else
		percent = child:round(cur / (total/100),0)
	end
	percent = child:colorize(percent, child:pickColor(percent))
	child:OnText(childName, format("%s %%", percent))
end
events.UPDATE_INVENTORY_ALERTS = events.UPDATE_INVENTORY_DURABILITY
events.UNIT_INVENTORY_CHANGED = events.UPDATE_INVENTORY_DURABILITY
child['specs']  = {
	['name'] = childName,
	['sv'] = true,
	['cfg'] = true,
	['cfgReset'] = prepareDB,
	['ldb'] = child['ldb'],
	['defaults'] = {
		['profile'] = {
			[childName] = {
				["autorepair"] = true,
				["listall"] = true,
				["showlowest"] = true,
			}
		}
	}
}
function events:PLAYER_ENTERING_WORLD(event)
	child:UnregisterEvent('PLAYER_ENTERING_WORLD')
	player = format("%s-%s", UnitFullName("player"))
	events.UPDATE_INVENTORY_DURABILITY()
end
child:NewAddOn(...)