--~ Warcraft Plugin for Cyborg MMO7 
--~ Filename: CyborgMMO7.lua
--~ Description: Plugin entry point, String tables and other generic crap that I could not think to put anywhere else.
--~ 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.

Mode = {
	["enUS"] = {
		"ALT-CTRL-E",
		"ALT-CTRL-F",
		"ALT-CTRL-["
	},
	["frFR"] = {
		"ALT-CTRL-E",
		"ALT-CTRL-F",
		"ALT-CTRL-^"
	},
	["deDE"] = {
		"ALT-CTRL-E",
		"ALT-CTRL-F",
		"ALT-CTRL-ü"
	},
	["zhCN"] = {
		"ALT-CTRL-E",
		"ALT-CTRL-F",
		"ALT-CTRL-["
	},
	["zhTW"] = {
		"ALT-CTRL-E",
		"ALT-CTRL-F",
		"ALT-CTRL-["
	},
	["esES"] = {
		"ALT-CTRL-E",
		"ALT-CTRL-F",
		"ALT-CTRL-'"
	},
	["esMX"] = {
		"ALT-CTRL-E",
		"ALT-CTRL-F",
		"ALT-CTRL-'"
	}
}

WowCommands = {
	["enUS"] = {
		"ALT-CTRL-NUMPAD1",
		"ALT-CTRL-NUMPAD2",
		"ALT-CTRL-NUMPAD3",
		"ALT-CTRL-NUMPAD4",
		"ALT-CTRL-NUMPAD5",
		"ALT-CTRL-NUMPAD6",
		"ALT-CTRL-NUMPAD7",
		"ALT-CTRL-NUMPAD8",
		"ALT-CTRL-NUMPAD9",
		"ALT-CTRL-NUMPAD0",
		"ALT-CTRL-1",
		"ALT-CTRL-2",
		"ALT-CTRL-3",
		"ALT-CTRL-4",
		"ALT-CTRL-5",
		"ALT-CTRL-6",
		"ALT-CTRL-7",
		"ALT-CTRL-8",
		"ALT-CTRL-9",
		"ALT-CTRL-0",
		"ALT-CTRL-F1",
		"ALT-CTRL-F2",
		"ALT-CTRL-F3",
		"ALT-CTRL-F4",
		"ALT-CTRL-F5",
		"ALT-CTRL-F6",
		"ALT-CTRL-F7",
		"ALT-CTRL-F8",
		"ALT-CTRL-F9",
		"ALT-CTRL-F10",
		"ALT-CTRL-F11",
		"ALT-CTRL-F12",
		"ALT-CTRL--",
		"ALT-CTRL-H",
		"ALT-CTRL-A",
		"ALT-CTRL-B",
		"ALT-CTRL-C",
		"ALT-CTRL-D",
		"ALT-CTRL-END"
	},
	["frFR"] = {
		"ALT-CTRL-NUMPAD1",
		"ALT-CTRL-NUMPAD2",
		"ALT-CTRL-NUMPAD3",
		"ALT-CTRL-NUMPAD4",
		"ALT-CTRL-NUMPAD5",
		"ALT-CTRL-NUMPAD6",
		"ALT-CTRL-NUMPAD7",
		"ALT-CTRL-NUMPAD8",
		"ALT-CTRL-NUMPAD9",
		"ALT-CTRL-NUMPAD0",
		"ALT-CTRL-1",
		"ALT-CTRL-2",
		"ALT-CTRL-3",
		"ALT-CTRL-4",
		"ALT-CTRL-5",
		"ALT-CTRL-6",
		"ALT-CTRL-7",
		"ALT-CTRL-8",
		"ALT-CTRL-9",
		"ALT-CTRL-0",
		"ALT-CTRL-F1",
		"ALT-CTRL-F2",
		"ALT-CTRL-F3",
		"ALT-CTRL-F4",
		"ALT-CTRL-F5",
		"ALT-CTRL-F6",
		"ALT-CTRL-F7",
		"ALT-CTRL-F8",
		"ALT-CTRL-F9",
		"ALT-CTRL-F10",
		"ALT-CTRL-F11",
		"ALT-CTRL-F12",
		"ALT-CTRL-)",
		"ALT-CTRL-H",
		"ALT-CTRL-Q",
		"ALT-CTRL-B",
		"ALT-CTRL-C",
		"ALT-CTRL-D",
		"ALT-CTRL-END"
	},
	["deDE"] = {
		"ALT-CTRL-NUMPAD1",
		"ALT-CTRL-NUMPAD2",
		"ALT-CTRL-NUMPAD3",
		"ALT-CTRL-NUMPAD4",
		"ALT-CTRL-NUMPAD5",
		"ALT-CTRL-NUMPAD6",
		"ALT-CTRL-NUMPAD7",
		"ALT-CTRL-NUMPAD8",
		"ALT-CTRL-NUMPAD9",
		"ALT-CTRL-NUMPAD0",
		"ALT-CTRL-1",
		"ALT-CTRL-2",
		"ALT-CTRL-3",
		"ALT-CTRL-4",
		"ALT-CTRL-5",
		"ALT-CTRL-6",
		"ALT-CTRL-7",
		"ALT-CTRL-8",
		"ALT-CTRL-9",
		"ALT-CTRL-0",
		"ALT-CTRL-F1",
		"ALT-CTRL-F2",
		"ALT-CTRL-F3",
		"ALT-CTRL-F4",
		"ALT-CTRL-F5",
		"ALT-CTRL-F6",
		"ALT-CTRL-F7",
		"ALT-CTRL-F8",
		"ALT-CTRL-F9",
		"ALT-CTRL-F10",
		"ALT-CTRL-F11",
		"ALT-CTRL-F12",
		"ALT-CTRL-ß",
		"ALT-CTRL-H",
		"ALT-CTRL-A",
		"ALT-CTRL-B",
		"ALT-CTRL-C",
		"ALT-CTRL-D",
		"ALT-CTRL-END"
	},
	["zhCN"] = {
		"ALT-CTRL-NUMPAD1",
		"ALT-CTRL-NUMPAD2",
		"ALT-CTRL-NUMPAD3",
		"ALT-CTRL-NUMPAD4",
		"ALT-CTRL-NUMPAD5",
		"ALT-CTRL-NUMPAD6",
		"ALT-CTRL-NUMPAD7",
		"ALT-CTRL-NUMPAD8",
		"ALT-CTRL-NUMPAD9",
		"ALT-CTRL-NUMPAD0",
		"ALT-CTRL-1",
		"ALT-CTRL-2",
		"ALT-CTRL-3",
		"ALT-CTRL-4",
		"ALT-CTRL-5",
		"ALT-CTRL-6",
		"ALT-CTRL-7",
		"ALT-CTRL-8",
		"ALT-CTRL-9",
		"ALT-CTRL-0",
		"ALT-CTRL-F1",
		"ALT-CTRL-F2",
		"ALT-CTRL-F3",
		"ALT-CTRL-F4",
		"ALT-CTRL-F5",
		"ALT-CTRL-F6",
		"ALT-CTRL-F7",
		"ALT-CTRL-F8",
		"ALT-CTRL-F9",
		"ALT-CTRL-F10",
		"ALT-CTRL-F11",
		"ALT-CTRL-F12",
		"ALT-CTRL--",
		"ALT-CTRL-H",
		"ALT-CTRL-A",
		"ALT-CTRL-B",
		"ALT-CTRL-C",
		"ALT-CTRL-D",
		"ALT-CTRL-END"
	},
	["zhTW"] = {
		"ALT-CTRL-NUMPAD1",
		"ALT-CTRL-NUMPAD2",
		"ALT-CTRL-NUMPAD3",
		"ALT-CTRL-NUMPAD4",
		"ALT-CTRL-NUMPAD5",
		"ALT-CTRL-NUMPAD6",
		"ALT-CTRL-NUMPAD7",
		"ALT-CTRL-NUMPAD8",
		"ALT-CTRL-NUMPAD9",
		"ALT-CTRL-NUMPAD0",
		"ALT-CTRL-1",
		"ALT-CTRL-2",
		"ALT-CTRL-3",
		"ALT-CTRL-4",
		"ALT-CTRL-5",
		"ALT-CTRL-6",
		"ALT-CTRL-7",
		"ALT-CTRL-8",
		"ALT-CTRL-9",
		"ALT-CTRL-0",
		"ALT-CTRL-F1",
		"ALT-CTRL-F2",
		"ALT-CTRL-F3",
		"ALT-CTRL-F4",
		"ALT-CTRL-F5",
		"ALT-CTRL-F6",
		"ALT-CTRL-F7",
		"ALT-CTRL-F8",
		"ALT-CTRL-F9",
		"ALT-CTRL-F10",
		"ALT-CTRL-F11",
		"ALT-CTRL-F12",
		"ALT-CTRL--",
		"ALT-CTRL-H",
		"ALT-CTRL-A",
		"ALT-CTRL-B",
		"ALT-CTRL-C",
		"ALT-CTRL-D",
		"ALT-CTRL-END"
	},
	["esES"] = {
		"ALT-CTRL-NUMPAD1",
		"ALT-CTRL-NUMPAD2",
		"ALT-CTRL-NUMPAD3",
		"ALT-CTRL-NUMPAD4",
		"ALT-CTRL-NUMPAD5",
		"ALT-CTRL-NUMPAD6",
		"ALT-CTRL-NUMPAD7",
		"ALT-CTRL-NUMPAD8",
		"ALT-CTRL-NUMPAD9",
		"ALT-CTRL-NUMPAD0",
		"ALT-CTRL-1",
		"ALT-CTRL-2",
		"ALT-CTRL-3",
		"ALT-CTRL-4",
		"ALT-CTRL-5",
		"ALT-CTRL-6",
		"ALT-CTRL-7",
		"ALT-CTRL-8",
		"ALT-CTRL-9",
		"ALT-CTRL-0",
		"ALT-CTRL-F1",
		"ALT-CTRL-F2",
		"ALT-CTRL-F3",
		"ALT-CTRL-F4",
		"ALT-CTRL-F5",
		"ALT-CTRL-F6",
		"ALT-CTRL-F7",
		"ALT-CTRL-F8",
		"ALT-CTRL-F9",
		"ALT-CTRL-F10",
		"ALT-CTRL-F11",
		"ALT-CTRL-F12",
		"ALT-CTRL-'",
		"ALT-CTRL-H",
		"ALT-CTRL-A",
		"ALT-CTRL-B",
		"ALT-CTRL-C",
		"ALT-CTRL-D",
		"ALT-CTRL-END"
	},
	["esMX"] = {
		"ALT-CTRL-NUMPAD1",
		"ALT-CTRL-NUMPAD2",
		"ALT-CTRL-NUMPAD3",
		"ALT-CTRL-NUMPAD4",
		"ALT-CTRL-NUMPAD5",
		"ALT-CTRL-NUMPAD6",
		"ALT-CTRL-NUMPAD7",
		"ALT-CTRL-NUMPAD8",
		"ALT-CTRL-NUMPAD9",
		"ALT-CTRL-NUMPAD0",
		"ALT-CTRL-1",
		"ALT-CTRL-2",
		"ALT-CTRL-3",
		"ALT-CTRL-4",
		"ALT-CTRL-5",
		"ALT-CTRL-6",
		"ALT-CTRL-7",
		"ALT-CTRL-8",
		"ALT-CTRL-9",
		"ALT-CTRL-0",
		"ALT-CTRL-F1",
		"ALT-CTRL-F2",
		"ALT-CTRL-F3",
		"ALT-CTRL-F4",
		"ALT-CTRL-F5",
		"ALT-CTRL-F6",
		"ALT-CTRL-F7",
		"ALT-CTRL-F8",
		"ALT-CTRL-F9",
		"ALT-CTRL-F10",
		"ALT-CTRL-F11",
		"ALT-CTRL-F12",
		"ALT-CTRL-'",
		"ALT-CTRL-H",
		"ALT-CTRL-A",
		"ALT-CTRL-B",
		"ALT-CTRL-C",
		"ALT-CTRL-D",
		"ALT-CTRL-END"
	}
}


StringTable = {
	["enUS"] = {
		CyborgButtonTitle="Cyborg Button",
		MiniMapButtonTitle="MiniMap Button",
		OptionPageTitle="Cyborg MMO7",
		OptionPageSetDefaults="Default",
		CyborgSizeSliderTitle="Cyborg Head Size",
		PluginSizeSliderTitle="Interface Window Size"
	},
	["frFR"] = {
		CyborgButtonTitle="Bouton Cyborg",
		MiniMapButtonTitle="Bouton minimap",
		OptionPageTitle="Cyborg MMO7",
		OptionPageSetDefaults="Par défaut",
		CyborgSizeSliderTitle="Dimension de la tête Cyborg",
		PluginSizeSliderTitle="Dimension de la fenêtre d'interface"
	},
	["deDE"] = {
		CyborgButtonTitle="Cyborg-Taste",
		MiniMapButtonTitle="MiniMap-Taste",
		OptionPageTitle="Cyborg MMO7",
		OptionPageSetDefaults="Standard",
		CyborgSizeSliderTitle="Größe des Cyborg-Kopfes",
		PluginSizeSliderTitle="Größe des Schnittstellenfensters"
	},
	["zhCN"] = {
		CyborgButtonTitle="Cyborg 按钮",
		MiniMapButtonTitle="“迷你地图”按钮",
		OptionPageTitle="Cyborg MMO7",
		OptionPageSetDefaults="默认",
		CyborgSizeSliderTitle="Cyborg 标题大小",
		PluginSizeSliderTitle="界面窗口大小"
	},
	["zhTW"] = {
		CyborgButtonTitle="Cyborg 按鈕",
		MiniMapButtonTitle="MiniMap 按鈕",
		OptionPageTitle="Cyborg MMO7",
		OptionPageSetDefaults="預設",
		CyborgSizeSliderTitle="Cyborg 頭大小",
		PluginSizeSliderTitle="介面視窗大小"
	},
	["esES"] = {
		CyborgButtonTitle="Botón Cyborg",
		MiniMapButtonTitle="Botón Minimapa",
		OptionPageTitle="Cyborg MMO7",
		OptionPageSetDefaults="Predeterminado",
		CyborgSizeSliderTitle="Tamaño de la cabeza de Cyborg",
		PluginSizeSliderTitle="Tamaño de la ventana de la interfaz"
	},
	["esMX"] = {
		CyborgButtonTitle="Botón de Cyborg",
		MiniMapButtonTitle="Botón de Minimapa",
		OptionPageTitle="Cyborg MMO7",
		OptionPageSetDefaults="Modo predeterminado",
		CyborgSizeSliderTitle="Tamaño de la cabeza de Cyborg",
		PluginSizeSliderTitle="Tamaño de la ventana de interfaz"
	}
}

function LoadStrings(self)
	self:SetText(StringTable[GetLocale()][self:GetName()]);
end

local VarsLoaded = false;
local EnteredWorld = false;
local LoadBinding = false;
local SaveName = GetRealmName().."_"..UnitName("player");
local Settings = nil;



function CyborgMiniMapButton_Reposition(pos)
	CyborgMiniMapFrame:SetPoint("TOPLEFT",
		"Minimap",
		"TOPLEFT",
		52-(80*cos(pos)),
		(80*sin(pos))-52)
end


function CyborgMiniMapButton_OnUpdate()

	local xpos,ypos = GetCursorPosition()
	local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom()

	xpos = (xmin)-(xpos / UIParent:GetScale())
	ypos = (ypos/ UIParent:GetScale())-(ymin)

	local degrees = math.deg(math.atan2(ypos,xpos));

	while(degrees < 0) do
		degrees = degrees + 360;
	end


	CyborgMiniMapButton_Reposition(degrees)
end

function MouseModeChange(mode)
	local MiniMapTexture = getglobal("CyborgMiniMapButton_Icon")
	local MiniMapGlowTexture = getglobal("CyborgMiniMapButton_IconGlow")
	local OpenButtonTexture = getglobal("OpenButtonPage".."OpenMainForm"):GetNormalTexture();
	local OpenButtonGlowTexture = getglobal("OpenButtonPage".."OpenMainForm"):GetHighlightTexture();
	if(1==mode) then
		MiniMapTexture:SetVertexColor(1,0,0,1)
		--MiniMapGlowTexture:SetVertexColor(1,0.26,0.26,.75);
		OpenButtonTexture:SetVertexColor(1,0,0,0.75);
		--OpenButtonGlowTexture:SetVertexColor(1,0.26,0.26,0.50);
	elseif(2==mode) then
		MiniMapTexture:SetVertexColor(0.07,0.22,1,1)
		MiniMapGlowTexture:SetVertexColor(0.13,0.56,1,.75);
		OpenButtonTexture:SetVertexColor(0.07,0.22,1,0.75);
		OpenButtonGlowTexture:SetVertexColor(0.13,0.56,1,0.5);
	elseif(3==mode) then
		MiniMapTexture:SetVertexColor(0.52,0.08,0.89,1)
		MiniMapGlowTexture:SetVertexColor(0.67,0.31,0.85,.75);
		OpenButtonTexture:SetVertexColor(0.52,0.08,0.89,0.75);
		OpenButtonGlowTexture:SetVertexColor(0.67,0.31,0.85,0.5);
	end
end

function GetSaveData()
	if(VarsLoaded) then
		if (CyborgMMO7SaveData == nil) then
			CyborgMMO7SaveData = {};
			CyborgMMO7SaveData[SaveName] = {}
		end
		return CyborgMMO7SaveData[SaveName];
	end
	return nil;
end

function SetSaveData(data, index)
	if(VarsLoaded) then
		GetSaveData()[index] = data
	end
end

function Event(self, event, ...)
    if(event == "VARIABLES_LOADED") then
		VarsLoaded = true;
    elseif(event == "PLAYER_ENTERING_WORLD") then
		EnteredWorld = true;
    elseif(event == "PLAYER_ENTER_COMBAT") then
		msg("PLAYER_ENTER_COMBAT");
		Close();
	elseif(event == nil) then
		msg("Event is nil");
	else
		msg("Event is not nil");
    end


	-- Fire Loading if and only if the player is in the world and vars are loaded
	if(false == LoadBinding) then
		if(VarsLoaded) then
			if(EnteredWorld) then
				local data = GetSaveData()

				RatPageModel.Instance().LoadData();
				LoadBinding = true;

				ShowMacroFrame();
				HideUIPanel(MacroFrame);


				SetupModeCallbacks(1);
				SetupModeCallbacks(2);
				SetupModeCallbacks(3);


				--Reload Slider values:
				if(nil == data["Settings"]) then
					data["Settings"] = {}
					data["Settings"]["Cyborg"] = 0.75;
					data["Settings"]["Plugin"] = 0.75;
					data["Settings"]["MiniMapButton"] = true;
					data["Settings"]["CyborgButton"] = true;
				end

				Settings = data["Settings"];

				CyborgSizeSlider:SetValue(Settings["Cyborg"]);
				SetOpenButtonSize(Settings["Cyborg"])
				PluginSizeSlider:SetValue(Settings["Plugin"]);
				SetMainPageSize(Settings["Plugin"]);

				if(Settings["MiniMapButton"] == false) then
					CyborgMiniMapButton:Hide();
				end

				local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom()
				CyborgMiniMapButton_Reposition(math.deg(math.atan2(ymin,xmin)))
				-- Close the main window for now
				Close();
			end
		end
	end
end

function SetDefaultSettings()
	OpenButtonPageOpenMainForm:ClearAllPoints();
	MainPage:ClearAllPoints();
	OpenButtonPageOpenMainForm:SetPoint("LEFT", UIParent, "LEFT", 0, 0);
	MainPage:SetPoint("LEFT", UIParent, "LEFT", 0, 0);

	SetOpenButtonSize(0.75);
	SetMainPageSize(0.75);
	MiniMapButton:SetChecked();
	SetMiniMapButton(true);
	CyborgButton:SetChecked();
	SetCyborgHeadButton(true);
end

function SetupModeCallbacks(modeNum)

	fn = function()
		MouseModeChange(modeNum);
		RatPageModel.Instance().SetMode(modeNum)
	end

	local buttonFrame, parentFrame, name = CallbackFactory.Instance().AddCallback(fn);
	if(1 ~= SetOverrideBindingClick(parentFrame, true, Mode[GetLocale()][modeNum], name, "LeftButton")) then
		msg("Failed to Bind modeChange");
	end
end

function Loaded()
    MainPage:RegisterEvent("VARIABLES_LOADED");
	MainPage:RegisterEvent("PLAYER_ENTERING_WORLD");
    MainPage:RegisterEvent("PLAYER_ENTER_COMBAT");
end

function Close()
    MainPage:Hide();
	if(Settings["CyborgButton"] == true) then
		OpenButtonPage:Show();
	end
end

function Open()
	MainPage:Show();
	RatQuickPage:Hide();
	if(Settings["CyborgButton"] == true) then
		OpenButtonPage:Show();
	end
end

function IsOpen()
	if(MainPage:IsVisible() == 1) then
		return true;
	else
		return false;
	end
end

function Toggle()
	if(IsOpen()) then
		Close();
	else
		Open();
	end
end

function msg(m)
	local id, name = GetChannelName("Debug");
	SendChatMessage(m, "CHANNEL", nil, id);
end

function SetMainPageSize(percent)
	if(VarsLoaded) then
		if(EnteredWorld) then
			MainPage:SetScale(percent);
			Settings["Plugin"] = percent;
			local data = GetSaveData()
			data["Settings"] = Settings
			PluginSizeSlider:SetValue(percent);
		end
	end
end

function SetOpenButtonSize(percent)
	if(VarsLoaded) then
		if(EnteredWorld) then
			OpenButtonPage:SetScale(percent)
			Settings["Cyborg"] = percent;
			local data = GetSaveData()
			data["Settings"] = Settings
			CyborgSizeSlider:SetValue(percent);
		end
	end
end

function SetCyborgHeadButton(boolVal)
	Settings["CyborgButton"] = boolVal;
	if(Settings["CyborgButton"] == true) then
		Close();
	else
		OpenButtonPage:Hide();
	end
	local data = GetSaveData()
	data["Settings"] = Settings
end

function SetMiniMapButton(boolVal)
	Settings["MiniMapButton"] = boolVal;
	if(Settings["MiniMapButton"] == true) then
		CyborgMiniMapButton:Show();
	else
		CyborgMiniMapButton:Hide();
	end
	local data = GetSaveData()
	data["Settings"] = Settings
end

