--[[
	
	sUmzieh-O-Mat (GUI Version)
	Copyright (c) 2008, Shantalya
	All rights reserved.
	
]]

--[[ KONFIGURATION - START ]]--

local angle = 137; -- x grad drehen. Rechts mittig = 0, Oben mittig = 90 (Bei der Runden BlizzardMinimap zumind. =))
local x = 76; -- x Pixel vom 0 Punkt entfernt (Exakt mittig auf der Minimap)
local y = 76; -- x Pixel vom 0 Punkt entfernt (Exakt mittig auf der Minimap)

local statusMessage = true; -- Statusnachrichten ausgeben. z.B. Set gespeichert

--[[ KONFIGURATION - ENDE ]]--


-- 137 Grad drehen ist bei der standard Minimap genau auf der Weltkarte, daher verstecken =) 
-- Da ich die Minimap nutze und genau da mein Button haben will...eine simple Funktion :)
if ( MiniMapWorldMapButton:IsShown() and angle == 137 ) then
	MiniMapWorldMapButton:Hide();
end
local playerRealm = GetRealmName();
local playerName = UnitName("player");
local frame = CreateFrame("Button", "sUmzieh-O-Mat", Minimap, "MiniMapButtonTemplate");
frame:SetWidth(33);
frame:SetHeight(33);
frame:SetPoint("CENTER", Minimap, "CENTER", math.cos(math.rad(angle))*x, math.sin(math.rad(angle))*y)
frame:SetFrameLevel(Minimap:GetFrameLevel()+3)
frame:RegisterEvent("VARIABLES_LOADED");
frame:SetScript("OnEvent", function(self, event, ...)
	self:UnregisterEvent(event);
	if ( not klamotten ) then
		klamotten = {};
	end
	if ( not klamotten[playerRealm] ) then
		klamotten[playerRealm] = {};
	end
	if ( not klamotten[playerRealm][playerName] ) then
		klamotten[playerRealm][playerName] = {};
	end
end);
frame:RegisterForClicks("anyUp");
frame:SetScript("OnClick", function(self, button)
	ToggleDropDownMenu(1, nil, sUmziehOMatDropdown, getglobal("sUmzieh-O-Mat"), -5, 5);
	PlaySound("igMainMenuOptionCheckBoxOn");
end);
frame:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight");

frame.icon = frame:CreateTexture(frame:GetName() .. "Icon", "BORDER");
frame.icon:SetWidth(19);
frame.icon:SetHeight(19);
frame.icon:SetTexture("Interface\\Icons\\Ability_DualWield");
frame.icon:SetPoint("CENTER", frame, -2, 2);

frame.border = frame:CreateTexture(frame:GetName() .. "Border", "OVERLAY");
frame.border:SetWidth(52);
frame.border:SetHeight(52);
frame.border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder");
frame.border:SetPoint("TOPLEFT", frame);


function sUmziehOMat_Print(msg)
	if ( statusMessage ) then
		DEFAULT_CHAT_FRAME:AddMessage(msg, ChatTypeInfo["SYSTEM"].r, ChatTypeInfo["SYSTEM"].g, ChatTypeInfo["SYSTEM"].b);
	end
end

function sUmziehOMat_Scan(setname)
	if ( klamotten[playerRealm][playerName][setname] ) then
		local maxCount = 0;
		for index, value in pairs(klamotten[playerRealm][playerName][setname]) do
			if ( string.sub(index, -4) == "Slot" ) then
				maxCount = maxCount+1;
			end
		end
		
		local count = 0;
		for bag=0, 4, 1 do
			for slot=1, GetContainerNumSlots(bag), 1 do
				local itemLink = GetContainerItemLink(bag, slot);
				if ( itemLink ) then
					for index, value in pairs(klamotten[playerRealm][playerName][setname]) do
						if ( string.find(itemLink, value, 1, true) and string.sub(index, -4) == "Slot" ) then
							count = count+1;
						end
					end
				end
			end
		end
		for index, value in pairs({"HeadSlot","NeckSlot","ShoulderSlot","ShirtSlot","ChestSlot","WaistSlot","LegsSlot","FeetSlot","WristSlot","HandsSlot","Finger0Slot","Finger1Slot","Trinket0Slot","Trinket1Slot","BackSlot","MainHandSlot","SecondaryHandSlot","RangedSlot","TabardSlot"}) do
			local itemLink = GetInventoryItemLink("player", GetInventorySlotInfo(value))
			if ( itemLink ) then
				for index, value in pairs(klamotten[playerRealm][playerName][setname]) do
					if ( string.find(itemLink, value, 1, true) and string.sub(index, -4) == "Slot" ) then
						count = count+1;
					end
				end
			end
		end
		if ( count == maxCount ) then
			return true;
		end
		return false;
	end
end

function sUmziehOMat_Load(self)
	if ( klamotten[playerRealm][playerName][self.value] ) then
		for bag=0, 4, 1 do
			for slot=1, GetContainerNumSlots(bag), 1 do
				local itemLink = GetContainerItemLink(bag, slot);
				if ( itemLink ) then
					for index, value in pairs(klamotten[playerRealm][playerName][self.value]) do
						if ( string.find(itemLink, value, 1, true) and string.sub(index, -4) == "Slot" ) then
							PickupContainerItem(bag, slot);
							PickupInventoryItem(GetInventorySlotInfo(index));
						end
					end
				end
			end
		end
		ShowHelm(klamotten[playerRealm][playerName][self.value]["ShowingHelm"]);
		ShowCloak(klamotten[playerRealm][playerName][self.value]["ShowingCloak"]);
		sUmziehOMat_Print("Set \"" .. self.value .. "\" geladen.");
	end
end

function sUmziehOMat_Popup()
	if ( not StaticPopupDialogs["UMZIEHOMAT_SAVE"] ) then
		StaticPopupDialogs["UMZIEHOMAT_SAVE"] = {
			text = "Ausr\195\188stung speichern.\nSpeichert deine aktuell angelegten Gegenst\195\164nde",
			button1 = ACCEPT_ALT,
			button2 = CANCEL,
			hasEditBox = 1,
			maxLetters = 31,
			whileDead = 1,
			OnHide = function(self)
				if ( ChatFrameEditBox:IsShown() ) then
					ChatFrameEditBox:SetFocus();
				end
				self.editBox:SetText("");
			end,
			OnAccept = function(self)
				local setname = self.editBox:GetText();
				sUmziehOMat_Save(setname);
				self.editBox:SetText("");
			end,
			timeout = 0,
			EditBoxOnEnterPressed = function(self)
				local parent = self:GetParent();
				local editBox = parent.editBox
				local setname = editBox:GetText();
				sUmziehOMat_Save(setname);
				editBox:SetText("");
				parent:Hide();
			end,
			EditBoxOnEscapePressed = function(self)
				self:GetParent():Hide();
			end,
			hideOnEscape = 1
		};
	end
	StaticPopup_Show("UMZIEHOMAT_SAVE");
end

function sUmziehOMat_Save(setname)
	local setname = setname or self.value;
	if ( type(setname) == "table" ) then
		setname = setname["value"];
	end
	klamotten[playerRealm][playerName][setname] = {
		["ShowingHelm"] 		= tostring(ShowingHelm()),
		["ShowingCloak"] 		= tostring(ShowingCloak()),
		["HeadSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("HeadSlot")),
		["NeckSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("NeckSlot")),
		["ShoulderSlot"]		= GetInventoryItemLink("player", GetInventorySlotInfo("ShoulderSlot")),
		["ShirtSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("ShirtSlot")),
		["ChestSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("ChestSlot")),
		["WaistSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("WaistSlot")),
		["LegsSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("LegsSlot")),
		["FeetSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("FeetSlot")),
		["WristSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("WristSlot")),
		["HandsSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("HandsSlot")),
		["Finger0Slot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("Finger0Slot")),
		["Finger1Slot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("Finger1Slot")),
		["Trinket0Slot"]		= GetInventoryItemLink("player", GetInventorySlotInfo("Trinket0Slot")),
		["Trinket1Slot"]		= GetInventoryItemLink("player", GetInventorySlotInfo("Trinket1Slot")),
		["BackSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("BackSlot")),
		["MainHandSlot"]		= GetInventoryItemLink("player", GetInventorySlotInfo("MainHandSlot")),
		["SecondaryHandSlot"]	= GetInventoryItemLink("player", GetInventorySlotInfo("SecondaryHandSlot")),
		["RangedSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("RangedSlot")),
		["TabardSlot"]			= GetInventoryItemLink("player", GetInventorySlotInfo("TabardSlot")),
	};
	sUmziehOMat_Print("Set \"" .. setname .. "\" gespeichert.");
	ToggleDropDownMenu(1, nil, sUmziehOMatDropdown, getglobal("sUmzieh-O-Mat"), -5, 5);
	ToggleDropDownMenu(1, nil, sUmziehOMatDropdown, getglobal("sUmzieh-O-Mat"), -5, 5);
end

function sUmziehOMat_Delete(self)
	if ( klamotten[playerRealm][playerName][self.value] ) then
		klamotten[playerRealm][playerName][self.value] = nil;
		sUmziehOMat_Print("Set \"" .. self.value .. "\" gel\195\182scht.");
	end
	ToggleDropDownMenu(1, nil, sUmziehOMatDropdown, getglobal("sUmzieh-O-Mat"), -5, 5);
	ToggleDropDownMenu(1, nil, sUmziehOMatDropdown, getglobal("sUmzieh-O-Mat"), -5, 5);
end

local sUmziehOMatDropdown = CreateFrame("Frame", "sUmziehOMatDropdown", UIParent, "UIDropDownMenuTemplate");
function sUmziehOMatDropdown_Initialize()
	if ( UIDROPDOWNMENU_MENU_LEVEL == 1 ) then
		info = UIDropDownMenu_CreateInfo();
		info.text = "Umzieh-O-Mat";
		info.isTitle = true;
		UIDropDownMenu_AddButton(info);
		
		info = UIDropDownMenu_CreateInfo();
		info.text = "Ausr\195\188stung speichern";
		info.func = sUmziehOMat_Popup;
		UIDropDownMenu_AddButton(info);
		
		if ( klamotten and klamotten[playerRealm][playerName] ) then
			for index, value in pairs(klamotten[playerRealm][playerName]) do
				info = UIDropDownMenu_CreateInfo();
				info.text = index;
				info.value = index;
				if ( not sUmziehOMat_Scan(index) ) then
					info.colorCode = "|cFFFF0000";
				end
				info.hasArrow = 1;
				info.func = sUmziehOMat_Load;
				UIDropDownMenu_AddButton(info);
			end
		end
	elseif ( UIDROPDOWNMENU_MENU_LEVEL == 2 ) then
		for index, value in pairs(klamotten[playerRealm][playerName]) do
			if ( UIDROPDOWNMENU_MENU_VALUE == index ) then
				info = UIDropDownMenu_CreateInfo();
				info.text = "\195\156ber speichern";
				info.value = index;
				info.func = sUmziehOMat_Save;
				UIDropDownMenu_AddButton(info, UIDROPDOWNMENU_MENU_LEVEL);
				
				info = UIDropDownMenu_CreateInfo();
				info.text = "L\195\182schen";
				info.value = index;
				info.func = sUmziehOMat_Delete;
				UIDropDownMenu_AddButton(info, UIDROPDOWNMENU_MENU_LEVEL);
			end
		end
	end
end
UIDropDownMenu_Initialize(sUmziehOMatDropdown, sUmziehOMatDropdown_Initialize, "MENU");
