local PA, ACL, ACH = unpack(_G.ProjectAzilroka)
if PA.Retail then return end

local LC = PA:NewModule('LootConfirm', 'AceEvent-3.0')
_G.LootConfirm, PA.LC = LC, LC

LC.Title, LC.Description, LC.Authors, LC.isEnabled = 'Loot Confirm', ACL['Confirms Loot for Solo/Groups (Need/Greed)'], 'Azilroka', false

local tonumber, strmatch = tonumber, strmatch
local GetLootRollItemInfo, GetLootRollItemLink, GetNumLootItems, ConfirmLootRoll, ConfirmLootSlot, RollOnLoot = GetLootRollItemInfo, GetLootRollItemLink, GetNumLootItems, ConfirmLootRoll, ConfirmLootSlot, RollOnLoot

function LC:Confirm(event, ...)
	if event == 'CONFIRM_LOOT_ROLL' then
		local arg1, arg2 = ...
		ConfirmLootRoll(arg1, arg2)
	elseif event == 'LOOT_OPENED' or event == 'LOOT_BIND_CONFIRM' then
		for slot = 1, GetNumLootItems() do
			ConfirmLootSlot(slot)
		end
	end
end

-- LOOT_ROLL_TYPE_PASS, LOOT_ROLL_TYPE_NEED
-- texture, item, quantity, currencyID, quality, locked, isQuestItem, questId, isActive = GetLootSlotInfo(slot)

function LC:START_LOOT_ROLL(_, rollID)
	if not (LC.db.Disenchant or LC.db.Greed) then return end

	local texture, name, _, quality, bop, canNeed, canGreed, canDisenchant = GetLootRollItemInfo(rollID)
	-- local itemLink = GetLootRollItemLink(rollID)
	-- local itemID = tonumber(strmatch(itemLink, 'item:(%d+)'))

	if canDisenchant and LC.db.Disenchant then
		RollOnLoot(rollID, _G.LOOT_ROLL_TYPE_DISENCHANT)
	elseif canGreed and LC.db.Greed then
		RollOnLoot(rollID, _G.LOOT_ROLL_TYPE_GREED)
	end
end

function LC:GetOptions()
	local LootConfirm = ACH:Group(LC.Title, LC.Description, nil, nil, function(info) return LC.db[info[#info]] end)
	PA.Options.args.LootConfirm = LootConfirm

	LootConfirm.args.Description = ACH:Description(LC.Description, 0)
	LootConfirm.args.Enable = ACH:Toggle(ACL['Enable'], nil, 1, nil, nil, nil, nil, function(info, value) LC.db[info[#info]] = value if not LC.isEnabled then LC:Initialize() else _G.StaticPopup_Show('PROJECTAZILROKA_RL') end end)

	LootConfirm.args.General = ACH:Group(ACL['General'], nil, 2)
	LootConfirm.args.General.inline = true

	LootConfirm.args.General.args.AutoRoll = ACH:Description(ACL['If Disenchant and Greed is selected. It will always try to Disenchant first.'], 0, 'large')
	LootConfirm.args.General.args.Disenchant = ACH:Toggle(ACL['Auto Disenchant'], nil, 1)
	LootConfirm.args.General.args.Greed = ACH:Toggle(ACL['Auto Greed'], nil, 2)

	LootConfirm.args.AuthorHeader = ACH:Header(ACL['Authors:'], -2)
	LootConfirm.args.Authors = ACH:Description(LC.Authors, -1, 'large')
end

function LC:BuildProfile()
	PA.Defaults.profile.LootConfirm = {
		Enable = false,
		Greed = false,
		Disenchant = false,
	}
end

function LC:UpdateSettings()
	LC.db = PA.db.LootConfirm
end

function LC:Initialize()
	if LC.db.Enable ~= true then
		return
	end

	LC.isEnabled = true

	_G.UIParent:UnregisterEvent('LOOT_BIND_CONFIRM')
	_G.UIParent:UnregisterEvent('CONFIRM_LOOT_ROLL')

	LC:RegisterEvent('CONFIRM_LOOT_ROLL', 'Confirm')
	LC:RegisterEvent('LOOT_OPENED', 'Confirm')
	LC:RegisterEvent('LOOT_BIND_CONFIRM', 'Confirm')
	LC:RegisterEvent('START_LOOT_ROLL')
end
