local tablet = AceLibrary("Tablet-2.0")

Auto_LootFu = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0", "AceConsole-2.0", "AceDB-2.0", "FuBarPlugin-2.0")

local files = {
	iconautoloot = "Interface\\AddOns\\FuBar_Auto_LootFu\\autoloot.tga",
	iconnoautoloot = "Interface\\AddOns\\FuBar_Auto_LootFu\\noautoloot.tga",
}


Auto_LootFu.hasIcon = files.iconautoloot
Auto_LootFu.defaultPosition = "LEFT"
Auto_LootFu:RegisterDB("Auto_LootFuDB")

function Auto_LootFu_OnLoad()
	this:RegisterEvent("PARTY_MEMBERS_CHANGED")
	this:RegisterEvent("VARIABLES_LOADED")
end

function Auto_LootFu:OnInitialize()
if(GetAutoLootDefault() == 1) then 
Auto_LootFu:SetIcon(files.iconautoloot)
else
Auto_LootFu:SetIcon(files.iconnoautoloot)
end
Auto_LootFu:UpdateText()
end

Auto_LootFu.OnMenuRequest = {
    type = 'group',
    args = {
        AutoLoot_On = {
            type = "toggle",
            name = "AutoLoot",
            desc = "Turn autolooting on or off",
            get = function() 
            return Auto_LootFu.db.profile.AutoLoot_On end,
            set = function() 
            	Auto_LootFu.db.profile.AutoLoot_On = not Auto_LootFu.db.profile.AutoLoot_On
				Auto_LootFu:SwapLoot()
                Auto_LootFu:Update() 
			end,       
		},
        AutoParty_On = {
            type = "toggle",
            name = "Off in Party/Raid",
            desc = "Turn autolooting in Party/Raid on or off",
            get = function() 
            return Auto_LootFu.db.profile.AutoParty_On end,
            set = function() 
            	Auto_LootFu.db.profile.AutoParty_On = not Auto_LootFu.db.profile.AutoParty_On
				Auto_LootFu:Update() 
			end,  
            disabled = function()
               if (GetAutoLootDefault() == 1) then return false
               else return true end
            end,            
		},
        AutoKeyBind = {
			type = 'text',
			name = "Set Toggle Key",
			desc = "Set up the key to switch loot modes temporarily" ,
			get = "GetBindKey",
			set = "SetBindKey",
            validate = { "SHIFT", "CTRL", "ALT" },
		},
    }
}

function Auto_LootFu:UpdateText()
	if(GetAutoLootDefault() == 1) then AutoLootMode="|cFF00FF00AutoLoot On"
	else
		AutoLootMode="AutoLoot Off"
	end
	Auto_LootFu:SetText(AutoLootMode)
end

function Auto_LootFu:SwapLoot()
	    if(GetAutoLootDefault() == 1) then 
        SetAutoLootDefault(0)
        AUTO_LOOT_DEFAULT=0
        Auto_LootFu:SetIcon(files.iconnoautoloot)
        DEFAULT_CHAT_FRAME:AddMessage("Autoloot set to Off")
        Auto_LootFu.db.profile.AutoLoot_On = false
	else
		SetAutoLootDefault(1)
        AUTO_LOOT_DEFAULT=1
        Auto_LootFu:SetIcon(files.iconautoloot)
        DEFAULT_CHAT_FRAME:AddMessage("Autoloot set to On")
        Auto_LootFu.db.profile.AutoLoot_On = true
	end
	Auto_LootFu:UpdateText()
end

function Auto_LootFu:OnTooltipUpdate()
    if(GetAutoLootDefault() == 1) then AutoLootMode="AutoLoot: On"
	else
		AutoLootMode="AutoLoot: Off"
	end
    local cat=tablet:AddCategory("columns",1)
    cat:AddLine('text', "")
    cat:AddLine('text', AutoLootMode)
    cat:AddLine('text', "")
    cat:AddLine('text', "Toggle Key: "..GetAutoLootToggleKey())
    tablet:SetHint("Click to toggle AutoLoot on and off.")
end

function Auto_LootFu:OnClick()
	Auto_LootFu:SwapLoot()
end

function Auto_LootFu:GetBindKey()
	return GetAutoLootToggleKey()
end

function Auto_LootFu:SetBindKey(name)
	SetAutoLootToggleKey(name)
    AUTO_LOOT_KEY = name
    Auto_LootFu.db.profile.AutoLoot_Name = name
    if(name=="SHIFT") then 
        DEFAULT_CHAT_FRAME:AddMessage("Autoloot toggle key set to SHIFT")
    elseif(name=="CTRL") then
        DEFAULT_CHAT_FRAME:AddMessage("Autoloot toggle key set to CTRL")
    else
        DEFAULT_CHAT_FRAME:AddMessage("Autoloot toggle key set to ALT")
    end
end

function Auto_LootFu_OnEvent()
if event == "PARTY_MEMBERS_CHANGED" or event == "VARIABLES_LOADED" then
		if GetNumRaidMembers() > 0 and Auto_LootFu.db.profile.AutoParty_On == true then
			SetAutoLootDefault(0)
            Auto_LootFu:SetIcon(files.iconnoautoloot)
		elseif GetNumPartyMembers() > 0 and Auto_LootFu.db.profile.AutoParty_On == true then
			SetAutoLootDefault(0)
            Auto_LootFu:SetIcon(files.iconnoautoloot)
		elseif Auto_LootFu.db.profile.AutoLoot_On == true then
			SetAutoLootDefault(1)
            Auto_LootFu:SetIcon(files.iconautoloot)
		end
       Auto_LootFu:UpdateText() 
	end
    	
end
