--~ Warcraft Plugin for Cyborg MMO7
--~ Filename: WowObjects.lua
--~ Description: Warcraft in game object models
--~ Copyright (C) 2012 Mad Catz Inc.
--~ Author: Christopher Hooks

--~ This program is free software; you can redistribute it and/or
--~ modify it under the terms of the GNU General Public License
--~ as published by the Free Software Foundation; either version 2
--~ of the License, or (at your option) any later version.

--~ This program is distributed in the hope that it will be useful,
--~ but WITHOUT ANY WARRANTY; without even the implied warranty of
--~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--~ GNU General Public License for more details.

--~ You should have received a copy of the GNU General Public License
--~ along with this program; if not, write to the Free Software
--~ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

------------------------------------------------------------------------------

local WowObject_methods = {}
local WowObject_mt = {__index=WowObject_methods}

local function WowObject(type, detail, subdetail)
	local self = {}

	self.Texture = nil
	self.Name = "NoName"
	self.Type = type
	self.Detail = detail
	self.Subdetail = subdetail

	setmetatable(self, WowObject_mt)

	return self
end

function WowObject_methods:DoAction()
	CyborgMMO_DPrint("Nothing To Do")
end

function WowObject_methods:Pickup()
	CyborgMMO_DPrint("Pick up Item")
end

function WowObject_methods:SetBinding(key)
end

function WowObject_methods:PlaySound()
	PlaySound("igAbilityIconDrop")
end

------------------------------------------------------------------------------

local WowCallback_methods = setmetatable({}, {__index=WowObject_methods})
local WowCallback_mt = {__index=WowCallback_methods}

local function WowCallback(callbackName)
	local self = WowObject("callback", callbackName, "")

	self.CallbackName = callbackName
	self.Texture = "Interface\\AddOns\\CyborgMMO7\\Graphics\\"..self.CallbackName.."Unselected.tga"

	setmetatable(self, WowCallback_mt)

	return self
end

function WowCallback_methods:SetTextures(buttonFrame)
	CyborgMMO_DPrint("TextureName = "..self.CallbackName)
	buttonFrame:SetNormalTexture("Interface\\AddOns\\CyborgMMO7\\Graphics\\"..self.CallbackName.."Unselected.tga")
	buttonFrame:SetPushedTexture("Interface\\AddOns\\CyborgMMO7\\Graphics\\"..self.CallbackName.."Down.tga")
	buttonFrame:SetHighlightTexture("Interface\\AddOns\\CyborgMMO7\\Graphics\\"..self.CallbackName.."Over.tga")
end

function WowCallback_methods:DoAction()
	local action = CyborgMMO_CallbackFactory:GetCallback(self.CallbackName)
	CyborgMMO_DPrint("calling callback:- "..self.CallbackName)
	action()
end

function WowCallback_methods:PickupCallback()
	local slot = nil
	local observers = CyborgMMO_RatPageModel:GetAllObservers()
	for i=1,#observers do
		if MouseIsOver(observers[i]) then
			slot = observers[i]
			break
		end
	end
	slot:SetNormalTexture(slot.UnCheckedTexture)
end

function WowCallback_methods:Pickup()
	self:PlaySound()
	ClearCursor()
	self:PickupCallback()
end

function WowCallback_methods:SetBinding(key)
	local buttonFrame,parentFrame,name = CyborgMMO_CallbackFactory:AddCallback(function(...) return self:DoAction(...) end)
	SetOverrideBindingClick(CyborgMMO_CallbackFactory.Frame, true, key, name, "LeftButton")
end

------------------------------------------------------------------------------

local WowItem_methods = setmetatable({}, {__index=WowObject_methods})
local WowItem_mt = {__index=WowItem_methods}

local function WowItem(number, itemID)
	local self = WowObject("item", number, itemID)

	self.Name,
	self.Link,
	self.Rarity,
	self.Level,
	self.MinLevel,
	self.Type,
	self.SubType,
	self.StackCount,
	self.EquipLoc,
	self.Texture,
	self.SellPrice = GetItemInfo(itemID)

	setmetatable(self, WowItem_mt)

	return self
end

function WowItem_methods:DoAction()
	CyborgMMO_DPrint("Use Item")
end

function WowItem_methods:Pickup()
	self:PlaySound()
	ClearCursor()
--	SetCursor(self.Texture)
	return PickupItem(self.Link)
end

function WowItem_methods:SetBinding(key)
	SetOverrideBinding(CyborgMMO_CallbackFactory.Frame, true, key, "ITEM "..self.Name)
end

------------------------------------------------------------------------------

local WowSpell_methods = setmetatable({}, {__index=WowObject_methods})
local WowSpell_mt = {__index=WowSpell_methods}

local function WowSpell(type, spellbookID, spellbook)
	local self = WowObject(type, spellbookID, spellbook)

	self.SpellbookID = spellbookID
	self.Spellbook = spellbook
	self.Name,self.Rank = GetSpellBookItemName(spellbookID, spellbook)
	self.Texture = GetSpellBookItemTexture(spellbookID, spellbook)
	self.Type = type

	setmetatable(self, WowSpell_mt)

	return self
end

function WowSpell_methods:DoAction()
	CyborgMMO_DPrint("Cast Spell")
end

function WowSpell_methods:Pickup()
	self:PlaySound()
	ClearCursor()
--	SetCursor(self.Texture)
	return PickupSpellBookItem(self.SpellbookID, self.Spellbook)
end

function WowSpell_methods:SetBinding(key)
	CyborgMMO_DPrint("Binding to key "..key)
	self.Key = key
	SetOverrideBinding(CyborgMMO_CallbackFactory.Frame, true, self.Key, self.Type.." "..self.Name)
end

------------------------------------------------------------------------------

local WowMacro_methods = setmetatable({}, {__index=WowObject_methods})
local WowMacro_mt = {__index=WowMacro_methods}

local function WowMacro(index)
	local self = WowObject("macro", index, nil)

	self.Name,
	self.Texture,
	self.Body,
	self.isLocal = GetMacroInfo(index)
	self.Index = index

	setmetatable(self, WowMacro_mt)

	return self
end

function WowMacro_methods:DoAction()
	CyborgMMO_DPrint("Use Item")
end

function WowMacro_methods:Pickup()
	self:PlaySound()
	ClearCursor()
--	SetCursor(self.Texture)
	return PickupMacro(self.Index)
end

function WowMacro_methods:SetBinding(key)
	self.Key = key
	SetOverrideBinding(CyborgMMO_CallbackFactory.Frame, true, key, "MACRO "..self.Index)
end

------------------------------------------------------------------------------

local WowCompanion_methods = setmetatable({}, {__index=WowObject_methods})
local WowCompanion_mt = {__index=WowCompanion_methods}

local function WowCompanion(index, SubType)
	local self = WowObject("companion", index, SubType)

	self.Id,
	self.Name,
	self.SpellId,
	self.Texture,
	self.isSummoned = GetCompanionInfo(SubType, index)
	self.SubType = SubType
	self.index = index

	setmetatable(self, WowCompanion_mt)

	return self
end

function WowCompanion_methods:DoAction()
	if self.SubType == "MOUNT" and IsMounted() then
		Dismount()
	else
		CallCompanion(self.SubType, self.index)
	end
end

function WowCompanion_methods:Pickup()
	self:PlaySound()
	return PickupCompanion(self.SubType, self.index)
end

function WowCompanion_methods:SetBinding(key)
	self.Key = key
	local buttonFrame,parentFrame,name = CyborgMMO_CallbackFactory:AddCallback(function() self:DoAction() end)
	SetOverrideBindingClick(parentFrame, true, key, name, "LeftButton")
--	SetOverrideBinding(hiddenModeChanger, true, key, "MACRO "..self.Index)
end

------------------------------------------------------------------------------

local WowMerchant_methods = setmetatable({}, {__index=WowObject_methods})
local WowMerchant_mt = {__index=WowMerchant_methods}

local function WowMerchant(index)
	local self = WowObject("merchant", index, nil)

	self.Name,
	self.Texture,
	self.Price,
	self.Quantity,
	self.NumAvailable,
	self.IsUsable,
	self.ExtendedCost = GetMerchantItemInfo(index)
	self.Index = index

	setmetatable(self, WowMerchant_mt)

	return self
end

function WowMerchant_methods:DoAction()
	CyborgMMO_DPrint("Use Item")
end

function WowMerchant_methods:Pickup()
	self:PlaySound()
	ClearCursor()
--	SetCursor(self.Texture)
	return PickupMerchantItem(self.Index)
end

function WowMerchant_methods:SetBinding(key)
	self.Key = key
	SetOverrideBinding(CyborgMMO_CallbackFactory.Frame, true, key, "MERCHANT "..self.Index)
end

------------------------------------------------------------------------------

local WowEquipmentSet_methods = setmetatable({}, {__index=WowObject_methods})
local WowEquipmentSet_mt = {__index=WowEquipmentSet_methods}

local function WowEquipmentSet(objectType, name, index)
	local self = WowObject(objectType, name, index)

	local texture,lessIndex = GetEquipmentSetInfoByName(name)
	self.Texture = "Interface\\Icons\\"..texture
	self.Name = name
	self.Index = lessIndex + 1

	setmetatable(self, WowEquipmentSet_mt)

	return self
end

function WowEquipmentSet_methods:DoAction()
	UseEquipmentSet(self.Name)
end

function WowEquipmentSet_methods:Pickup()
	self:PlaySound()
	ClearCursor()
--	SetCursor(self.Texture)
	return PickupEquipmentSetByName(self.Name)
end

function WowEquipmentSet_methods:SetBinding(key)
	self.Key = key
	local buttonFrame,parentFrame,name = CyborgMMO_CallbackFactory:AddCallback(function() self:DoAction() end);
	SetOverrideBindingClick(parentFrame, true, key, name, "LeftButton")
end

------------------------------------------------------------------------------

-- this class is used by pre-defined icons in the corner of the Rat page
CyborgMMO_CallbackIcons = {
	new = function(self)
		self.point,
		self.relativeTo,
		self.relativePoint,
		self.xOfs,
		self.yOfs = self:GetPoint()
	--	self:SetPoint(self.point, self.relativeTo, self.relativePoint, self.xOfs, self.yOfs)
		self.strata = self:GetFrameStrata()
		self.wowObject = WowCallback(string.gsub(self:GetName(), self:GetParent():GetName(), "",1))
		self.wowObject:SetTextures(self)
		self:RegisterForDrag("LeftButton","RightButton")
		self:SetResizable(false)

		self.OnClick = function()
			self.wowObject:DoAction()
		end

		self.DragStart = function()
			self:SetMovable(true)
			self:StartMoving()
			self.isMoving = true
			self:SetFrameStrata("TOOLTIP")
		end

		self.DragStop = function()
			self:SetFrameStrata(self.strata)
			self.isMoving = false
			self:SetMovable(false)
			self:StopMovingOrSizing()

			self:ClearAllPoints()
			self:SetPoint(self.point, self.relativeTo, self.relativePoint, self.xOfs, self.yOfs)
			local x,y = GetCursorPosition()
			CyborgMMO_RatPageController:CallbackDropped(self)
		end

		return self
	end,
}

------------------------------------------------------------------------------

function CyborgMMO_CreateWowObject(type, ...)
	local object

	if type == "item" then
		object = WowItem(...)
	elseif type == "macro" then
		object = WowMacro(...)
	elseif type == "spell" then
		object = WowSpell(type, ...)
	elseif type == "petaction" then
		object = WowSpell(type, ...)
	elseif type == "merchant" then
		object = WowMerchant(...)
	elseif type == "companion" then
		object = WowCompanion(...)
	elseif type == "equipmentset" then
		object = WowEquipmentSet(type, ...)
	elseif type == "callback" then
		object = WowCallback(...)
	else
		object = WowObject(type, ...)
	end

	return object
end

function CyborgMMO_ClearBinding(key)
	SetOverrideBinding(CyborgMMO_CallbackFactory.Frame, true, key, nil)
end

