--~ Warcraft Plugin for Cyborg MMO7
--~ Filename: RatPageModel.lua
--~ Description: Code model of the MMO7 mouse
--~ 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.
-- Constants --

local RAT7 = {
	BUTTONS = 13,
	MODES = 3,
	SHIFT = 0,
}

local MIDDLEMOUSE = 1

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

local RatPageModel_methods = {}
local RatPageModel_mt = {__index=RatPageModel_methods}

local function RatPageModel()
	local self = {}
	self.mode = 1
	self.observers = {}
	self.data = {}

	for i=1,RAT7.MODES do
		self.data[i] = {}
		for j=1,RAT7.BUTTONS do
			self.data[i][j] = {}
		end
	end

	setmetatable(self, RatPageModel_mt)

	return self
end

function RatPageModel_methods:InitSaveData(data)
	for i=1,RAT7.MODES do
		if not data["Rat"][i] then
			data["Rat"][i] = {}
		end
		for j=1,RAT7.BUTTONS do
			if not data["Rat"][i][j] then
				data["Rat"][i][j] = {}
			end
		end
	end
end

function RatPageModel_methods:LoadData()
	CyborgMMO_DPrint("Loading...")
	local data = CyborgMMO_GetSaveData()

	if not data["Rat"] then
		data["Rat"] = {}
		self:InitSaveData(data)
	end

	self.data = data["Rat"]
	if data then
		for mode=1,RAT7.MODES do
			for button=1,RAT7.BUTTONS do
				if self.data[mode][button] then
					local object = CyborgMMO_CreateWowObject(self.data[mode][button].type, self.data[mode][button].detail, self.data[mode][button].subdetail)
					self:SetObjectOnButtonNoUpdate(button, mode, object)
				else
					local object = CyborgMMO_CreateWowObject()
					self:SetObjectOnButtonNoUpdate(button, mode, object)
					self.data[mode][button] = object
				end
			end
		end
		self:UpdateObservers()
	end
end

function RatPageModel_methods:SaveData()
	CyborgMMO_DPrint("Saving...")
	CyborgMMO_SetRatSaveData(self.data)
end

function RatPageModel_methods:SetMode(mode)
	self.mode = mode
	self:UpdateObservers()
end

function RatPageModel_methods:GetMode()
	return self.mode
end

function RatPageModel_methods:GetData()
	return self.data,self.mode
end

function RatPageModel_methods:GetObjectOnButton(button)
	if not self.data[self.mode][button] then
		return nil
	else
		return self.data[self.mode][button]
	end
end

function RatPageModel_methods:SetObjectOnButtonNoUpdate(button, mode, object)
--	CyborgMMO_DPrint("button = "..tostring(button).." mode = "..tostring(mode))
	self.data[mode][button] = object

	if object then
		object:SetBinding(CyborgMMO_ProfileKeyBindings[((mode-1)*RAT7.BUTTONS)+button])
		if "callback" == object.type then
			CyborgMMO_DPrint("trying to set texture")
			local slot = getglobal("CyborgMMO_MainPageSlotListSlot"..button)
			slot:SetNormalTexture(object.texture)
		end
	else
		CyborgMMO_DPrint("clearing "..button)
		CyborgMMO_ClearBinding(CyborgMMO_ProfileKeyBindings[((mode-1)*RAT7.BUTTONS)+button])
	end
end

function RatPageModel_methods:SetObjectOnButton(button, mode, object)
	self:SetObjectOnButtonNoUpdate(button, mode, object)
	self:UpdateObservers()
end

function RatPageModel_methods:AddObserver(view)
	table.insert(self.observers, view)
end

function RatPageModel_methods:GetAllObservers()
	return self.observers
end

function RatPageModel_methods:UpdateObservers()
	for i=1,#self.observers do
		self.observers[i].Update(self.data, self.mode)
	end
	self:SaveData()
end

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

CyborgMMO_RatPageModel = RatPageModel()

