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





function CyborgMMO_LoadStrings(self)
--~ 	msg("LoadStrings("..self:GetName()..") = "..CyborgMMO_StringTable[self:GetName()])
	self:SetText(CyborgMMO_StringTable[self:GetName()]);
end

local CyborgMMO_VarsLoaded = false;
local CyborgMMO_EnteredWorld = false;
local CyborgMMO_LoadBinding = false;
local CyborgMMO_SaveName = GetRealmName().."_"..UnitName("player");
local CyborgMMO_Settings = nil;


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


function CyborgMMO_MiniMapButtonOnUpdate()

	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


	CyborgMMO_MiniMapButtonReposition(degrees)
end

function CyborgMMO_MouseModeChange(mode)
	local MiniMapTexture = getglobal("CyborgMMO_MiniMapFrameButton_Icon")
	local MiniMapGlowTexture = getglobal("CyborgMMO_MiniMapFrameButton_IconGlow")
	local OpenButtonTexture = getglobal("CyborgMMO_OpenButtonPage".."OpenMainForm"):GetNormalTexture();
	local OpenButtonGlowTexture = getglobal("CyborgMMO_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 CyborgMMO_LoadWowCommands()
	if (CyborgMMO_ProfileKeyBindings == nil) then
		CyborgMMO_ProfileKeyBindings = {}
		CyborgMMO_ProfileKeyBindings = CyborgMMO_DefaultWowCommands
	end
	CyborgMMO_WowCommands = CyborgMMO_ProfileKeyBindings 
end

function CyborgMMO_GetSaveData()
	if(CyborgMMO_VarsLoaded) then
		if (CyborgMMO7SaveData == nil) then
			CyborgMMO7SaveData = {};
			CyborgMMO7SaveData[CyborgMMO_SaveName] = {}
		end
		CyborgMMO_LoadWowCommands()
		return CyborgMMO7SaveData[CyborgMMO_SaveName];
	end
	return nil;
end

function CyborgMMO_SetSaveData(data, index)
	if(CyborgMMO_VarsLoaded) then
		CyborgMMO_GetSaveData()[index] = data
		CyborgMMO_ProfileKeyBindings = CyborgMMO_WowCommands
	end
end

function CyborgMMO_Event(self, event, ...)
    if(event == "VARIABLES_LOADED") then
		CyborgMMO_VarsLoaded = true;
    elseif(event == "PLAYER_ENTERING_WORLD") then
		CyborgMMO_EnteredWorld = true;
    elseif(event == "PLAYER_ENTER_COMBAT") then
		msg("PLAYER_ENTER_COMBAT");
		CyborgMMO_Close();
	elseif(event == nil) then
		msg("Event is nil");
	else
		msg("Event is " + event);
    end


	-- Fire Loading if and only if the player is in the world and vars are loaded
	if(false == CyborgMMO_LoadBinding) then
		if(CyborgMMO_VarsLoaded) then
			if(CyborgMMO_EnteredWorld) then
				local data = CyborgMMO_GetSaveData()

				CyborgMMO_RatPageModel.Instance().LoadData();
				CyborgMMO_LoadBinding = true;

				ShowMacroFrame();
				HideUIPanel(MacroFrame);


				CyborgMMO_SetupModeCallbacks(1);
				CyborgMMO_SetupModeCallbacks(2);
				CyborgMMO_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

				CyborgMMO_Settings = data["Settings"];

				CyborgMMO_OptionPageCyborgSizeSlider:SetValue(CyborgMMO_Settings["Cyborg"]);
				CyborgMMO_SetOpenButtonSize(CyborgMMO_Settings["Cyborg"])
				CyborgMMO_OptionPagePluginSizeSlider:SetValue(CyborgMMO_Settings["Plugin"]);
				CyborgMMO_SetMainPageSize(CyborgMMO_Settings["Plugin"]);
				
				CyborgMMO_SetMiniMapButton(CyborgMMO_Settings["MiniMapButton"])
				CyborgMMO_SetCyborgHeadButton(CyborgMMO_Settings["CyborgButton"])

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

function CyborgMMO_SetDefaultSettings()
	CyborgMMO_OpenButtonPageOpenMainForm:ClearAllPoints();
	CyborgMMO_MainPage:ClearAllPoints();
	CyborgMMO_OpenButtonPageOpenMainForm:SetPoint("LEFT", UIParent, "LEFT", 0, 0);
	CyborgMMO_MainPage:SetPoint("LEFT", UIParent, "LEFT", 0, 0);

	CyborgMMO_SetOpenButtonSize(0.75);
	CyborgMMO_SetMainPageSize(0.75);
	CyborgMMO_SetMiniMapButton(true);
	CyborgMMO_SetCyborgHeadButton(true);
end


function CyborgMMO_SetupModeCallbacks(modeNum)
	fn = function()
		CyborgMMO_MouseModeChange(modeNum);
		CyborgMMO_RatPageModel.Instance().SetMode(modeNum)
	end

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

function CyborgMMO_Loaded()
    CyborgMMO_MainPage:RegisterEvent("VARIABLES_LOADED");
		CyborgMMO_MainPage:RegisterEvent("PLAYER_ENTERING_WORLD");
    CyborgMMO_MainPage:RegisterEvent("PLAYER_ENTER_COMBAT");
end

function CyborgMMO_Close()
    CyborgMMO_MainPage:Hide();
	if(CyborgMMO_Settings["CyborgButton"] == true) then
		CyborgMMO_OpenButtonPage:Show();
	end
end

function CyborgMMO_Open()
	CyborgMMO_MainPage:Show();
	CyborgMMO_RatQuickPage:Hide();
	if(CyborgMMO_Settings["CyborgButton"] == true) then
		CyborgMMO_OpenButtonPage:Show();
	end
end

function CyborgMMO_IsOpen()
	if(CyborgMMO_MainPage:IsVisible() == 1) then
		return true;
	else
		return false;
	end
end

function CyborgMMO_Toggle()
	if(CyborgMMO_IsOpen()) then
		CyborgMMO_Close();
	else
		CyborgMMO_Open();
	end
end

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

function CyborgMMO_SetMainPageSize(percent)
	if(CyborgMMO_VarsLoaded) then
		if(CyborgMMO_EnteredWorld) then
			CyborgMMO_MainPage:SetScale(percent);
			CyborgMMO_Settings["Plugin"] = percent;
			local data = CyborgMMO_GetSaveData()
			data["Settings"] = CyborgMMO_Settings
			CyborgMMO_OptionPagePluginSizeSlider:SetValue(percent);
		end
	end
end

function CyborgMMO_SetOpenButtonSize(percent)
	if(CyborgMMO_VarsLoaded) then
		if(CyborgMMO_EnteredWorld) then
			CyborgMMO_OpenButtonPage:SetScale(percent)
			CyborgMMO_Settings["Cyborg"] = percent;
			local data = CyborgMMO_GetSaveData()
			data["Settings"] = CyborgMMO_Settings
			CyborgMMO_OptionPageCyborgSizeSlider:SetValue(percent);
		end
	end
end

function CyborgMMO_SetCyborgHeadButton(boolVal)
	CyborgMMO_Settings["CyborgButton"] = boolVal;
	CyborgMMO_OptionPageCyborgButton:SetChecked(boolVal);
	if(CyborgMMO_Settings["CyborgButton"] == true) then
		CyborgMMO_Close();
	else
		CyborgMMO_OpenButtonPage:Hide();
	end
	local data = CyborgMMO_GetSaveData()
	data["Settings"] = CyborgMMO_Settings
end

function CyborgMMO_SetMiniMapButton(boolVal)
	CyborgMMO_Settings["MiniMapButton"] = boolVal
	CyborgMMO_Settings["MiniMapButton"] = boolVal
	CyborgMMO_OptionPageMiniMapButton:SetChecked(boolVal)
	if(CyborgMMO_Settings["MiniMapButton"] == true) then
		CyborgMMO_MiniMapFrameButton:Show()
	else
		CyborgMMO_MiniMapFrameButton:Hide()
	end
	local data = CyborgMMO_GetSaveData()
	data["Settings"] = CyborgMMO_Settings
end

