--~ 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.

CyborgMMO_WowObject = {
	new = function(type, detail, subdetail)
		local self = {}
		self.Texture = nil
		self.Name = "NoName"
		self.Type = type
		self.Detail = detail
		self.Subdetail = subdetail

		-- Methods --
		self.DoAction = function()
			CyborgMMO_DPrint("Nothing To Do")
		end

		self.Pickup = function()
			CyborgMMO_DPrint("Pick up Item")
		end

		self.SetBinding = function(key)
		end

		self.PlaySound = function()
			PlaySound("igAbilityIconDrop")
		end

		return self
	end,

	ClearBinding = function(key)
		local buttonFrame,parentFrame,name = CyborgMMO_CallbackFactory.Instance().AddCallback(CyborgMMO_WowObject.DoNothing)
		SetOverrideBindingClick(parentFrame, true, key, name, "LeftButton")
	end,

	DoNothing = function()
	end,

	-- Static Methods --
	Create = function(objectType, detail, subdetail)
		local object
		if objectType == "item" then
			object = CyborgMMO_WowItem.new(detail, subdetail)
		elseif objectType == "macro" then
			object = CyborgMMO_WowMacro.new(detail)
		elseif objectType == "spell" then
			object = CyborgMMO_WowSpell.new(objectType, detail, subdetail)
		elseif objectType == "petaction" then
			object = CyborgMMO_WowSpell.new(objectType, detail, subdetail)
		elseif objectType == "merchant" then
			object = CyborgMMO_SlotMerchant.new(detail, subdetail)
		elseif objectType == "companion" then
			object = CyborgMMO_WowCompanion.new(detail, subdetail)
		elseif objectType == "equipmentset" then
			object = CyborgMMO_WowEquipmentSet.new(objectType, detail, subdetail)
		elseif objectType == "callback" then
			object = CyborgMMO_WowCallback.new(detail)
		else
			object = CyborgMMO_WowObject.new(objectType, detail, subdetail)
		end

		return object
	end,
}

local CallbackCursor = nil

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 = CyborgMMO_WowCallback.new(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.Instance().CallbackDropped(self)
		end

		return self
	end,
}

CyborgMMO_WowCallback = {
	new = function(callbackName)
		local self = CyborgMMO_WowObject.new("callback", callbackName, "")
		self.CallbackName = callbackName
		self.Texture = "Interface\\AddOns\\CyborgMMO7\\Graphics\\"..self.CallbackName.."Unselected.tga"

		self.SetTextures = function(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

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

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

		self.ClickHandler = function(self, button, down)
			CyborgMMO_DPrint("click handler")
			CallbackCursor:StopMoving()
			CallbackCursor:Hide()
		end

		self.Pickup = function()
			self.PlaySound()
			ClearCursor()
			self.PickupCallback()
		end

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

		return self
	end,
}

-- WowItem Class --
CyborgMMO_WowItem = {
	new = function(number, itemID)
		local self = CyborgMMO_WowObject.new("item", number, itemID) -- base class
		-- Set all the item info. --
		self.Name,
		self.Link,
		self.Rarity,
		self.Level,
		self.MinLevel,
		self.Type,
		self.SubType,
		self.StackCount,
		self.EquipLoc,
		self.Texture,
		self.SellPrice = GetItemInfo(itemID)

		-- override method --
		self.DoAction = function()
			CyborgMMO_DPrint("Use Item")
		end

		-- override method --
		self.Pickup = function()
			self.PlaySound()
			ClearCursor()
		--	SetCursor(self.Texture)
			return PickupItem(self.Link)
		end

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

		return self
	end,
}

-- WowSpell Class --
CyborgMMO_WowSpell = {
	new = function(type, spellbookID, spellbook)
		local self = CyborgMMO_WowObject.new(type, spellbookID, spellbook) -- base class
		self.SpellbookID = spellbookID
		self.Spellbook = spellbook
		self.Name,self.Rank = GetSpellBookItemName(spellbookID, spellbook)
		self.Texture = GetSpellBookItemTexture(spellbookID, spellbook)
		self.Type = type


		-- override method --
		self.DoAction = function()
			CyborgMMO_DPrint("Cast Spell")
		end

		-- override method --
		self.Pickup = function()
			self.PlaySound()
			ClearCursor()
		--	SetCursor(self.Texture)
			return PickupSpellBookItem(self.SpellbookID, self.Spellbook)
		end

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

		return self
	end,
}

-- WowMacro Class --
CyborgMMO_WowMacro = {
	new = function(index)
		local self = CyborgMMO_WowObject.new("macro", index, nil) -- base class
		-- Set all the item info. --
		self.Name,
		self.Texture,
		self.Body,
		self.isLocal = GetMacroInfo(index)
		self.Index = index

		-- override method --
		self.DoAction = function()
			CyborgMMO_DPrint("Use Item")
		end

		-- override method --
		self.Pickup = function()
			self.PlaySound()
			ClearCursor()
		--	SetCursor(self.Texture)
			return PickupMacro(self.Index)
		end

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

		return self
	end,
}

-- WowCompanion Class --
CyborgMMO_WowCompanion = {
	new = function(index, SubType)
		local self = CyborgMMO_WowObject.new("companion", index, SubType) -- base class
		-- Set all the item info. --
		self.Id,
		self.Name,
		self.SpellId,
		self.Texture,
		self.isSummoned = GetCompanionInfo(SubType, index)
		self.SubType = SubType
		self.index = index
		-- override method --
		self.DoAction = function()
			if self.SubType == "MOUNT" and IsMounted() then
				Dismount()
			else
				CallCompanion(self.SubType, self.index)
			end
		end

		-- override method --
		self.Pickup = function()
			self.PlaySound()
			return PickupCompanion(self.SubType, self.index)
		end

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

		return self
	end,
}

-- WowMerchant Class --
CyborgMMO_WowMerchant = {
	new = function(index)
		local self = CyborgMMO_WowObject.new("macro", index, nil) -- base class
		-- Set all the item info. --
		self.Name,
		self.Texture,
		self.Price,
		self.Quantity,
		self.NumAvailable,
		self.IsUsable,
		self.ExtendedCost = GetMerchantItemInfo(index)
		self.Index = index

		-- override method --
		self.DoAction = function()
			CyborgMMO_DPrint("Use Item")
		end

		-- override method --
		self.Pickup = function()
			self.PlaySound()
			ClearCursor()
		--	SetCursor(self.Texture)
			return PickupMerchantItem(self.Index)
		end

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

		return self
	end,
}

-- WowEquipmentSet Class --
CyborgMMO_WowEquipmentSet = {
	new = function(objectType, name, index)
		local self = CyborgMMO_WowObject.new(objectType, name, index) -- base class
		-- Set all the item info. --
		local texture,lessIndex = GetEquipmentSetInfoByName(name)
		self.Texture = "Interface\\Icons\\"..texture
		self.Name = name
		self.Index = lessIndex + 1

		-- override method --
		self.DoAction = function()
			UseEquipmentSet(self.Name)
		end

		-- override method --
		self.Pickup = function()
			self.PlaySound()
			ClearCursor()
		--	SetCursor(self.Texture)
			return PickupEquipmentSetByName(self.Name)
		end

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

		return self
	end,
}

-- End Of WowObjects --
