--~ Warcraft Plugin for Cyborg MMO7 
--~ Filename: OptionView.lua
--~ Description: The code for the Option page in the UI, not much here because we dont have many options. Probably could refactor.
--~ 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.

OptionView = {
	new = function(self)
        self.name = "Cyborg MMO7 Plugin"
        InterfaceOptions_AddCategory(self);
		return self

	end
}

local lastButton = nil

function BindButton(name)
	lastButton = name
	local index = GetButtonIndex(name)
	local mode = 1
	while(index > 13) do
		mode = mode + 1
		index = index - 13
	end
	local buttonStr =  StringTable[("MouseRow"..index.."Name")]
	
	getglobal("BindingFrameButtonName"):SetText(buttonStr.." Mode "..mode)
	getglobal("BindingFrameKey"):SetText(StringTable["CurrentBinding"].." "..WowCommands[GetButtonIndex(lastButton)])
	BindingFrame:Show()
	
end

function GetBindingButtonText(name)
	if(nil == WowCommands) then
		LoadWowCommands();
	end
	
	local binding = WowCommands[GetButtonIndex(name)]
	getglobal(name):SetText(binding)
end

function GetButtonIndex(name)
	local row,mode = string.find(name,"Mode")
	local modeStr = string.sub(name, mode +1,mode+2)
	local rowStr = string.sub(name, row-1,row-1)
	return (GetNumberFromHexLetter(rowStr) + ((GetNumberFromHexLetter(modeStr) - 1) * 13))
end

function GetNumberFromHexLetter(str)
	local number = 0
	if("A" == str) then
		number = 10
	elseif("B" == str) then
		number = 11
	elseif("C" == str) then
		number = 12
	elseif("D" == str) then
		number = 13
	elseif("E" == str) then
		number = 14
	elseif("F" == str) then
		number = 15
	else 
	 number = tonumber(str)
	end
	return number
end

function SetNewKeybind(keyOrButton)
	local previous = WowCommands[GetButtonIndex(lastButton)]
	WowCommands[GetButtonIndex(lastButton)] = keyOrButton;
	GetBindingButtonText(lastButton);
	BindingFrame:Hide()
	RatPageModel.Instance().LoadData()
end

function BindingFrame_OnKeyDown(self, keyOrButton)
   if keyOrButton=="ESCAPE" then
      BindingFrame:Hide()
      return
   end
   
   if ( GetBindingFromClick(keyOrButton) == "SCREENSHOT" ) then
      RunBinding("SCREENSHOT");
      return;
   end
   
   local keyPressed = keyOrButton;
   
   if ( keyPressed == "UNKNOWN" ) then
      return;
   end
   
   -- Convert the mouse button names
   if ( keyPressed == "LeftButton" ) then
      keyPressed = "BUTTON1";
   elseif ( keyPressed == "RightButton" ) then
      keyPressed = "BUTTON2";
   elseif ( keyPressed == "MiddleButton" ) then
      keyPressed = "BUTTON3";
   elseif ( keyPressed == "Button4" ) then
      keyPressed = "BUTTON4"
   elseif ( keyOrButton == "Button5" ) then
      keyPressed = "BUTTON5"
   elseif ( keyPressed == "Button6" ) then
      keyPressed = "BUTTON6"
   elseif ( keyOrButton == "Button7" ) then
      keyPressed = "BUTTON7"
   elseif ( keyPressed == "Button8" ) then
      keyPressed = "BUTTON8"
   elseif ( keyOrButton == "Button9" ) then
      keyPressed = "BUTTON9"
   elseif ( keyPressed == "Button10" ) then
      keyPressed = "BUTTON10"
   elseif ( keyOrButton == "Button11" ) then
      keyPressed = "BUTTON11"
   elseif ( keyPressed == "Button12" ) then
      keyPressed = "BUTTON12"
   elseif ( keyOrButton == "Button13" ) then
      keyPressed = "BUTTON13"
   elseif ( keyPressed == "Button14" ) then
      keyPressed = "BUTTON14"
   elseif ( keyOrButton == "Button15" ) then
      keyPressed = "BUTTON15"
   elseif ( keyPressed == "Button16" ) then
      keyPressed = "BUTTON16"
   elseif ( keyOrButton == "Button17" ) then
      keyPressed = "BUTTON17"
   elseif ( keyPressed == "Button18" ) then
      keyPressed = "BUTTON18"
   elseif ( keyOrButton == "Button19" ) then
      keyPressed = "BUTTON19"
   elseif ( keyPressed == "Button20" ) then
      keyPressed = "BUTTON20"
   elseif ( keyOrButton == "Button21" ) then
      keyPressed = "BUTTON21"
   elseif ( keyPressed == "Button22" ) then
      keyPressed = "BUTTON22"
   elseif ( keyOrButton == "Button23" ) then
      keyPressed = "BUTTON23"
   elseif ( keyPressed == "Button24" ) then
      keyPressed = "BUTTON24"
   elseif ( keyOrButton == "Button25" ) then
      keyPressed = "BUTTON25"
   elseif ( keyPressed == "Button26" ) then
      keyPressed = "BUTTON26"
   elseif ( keyOrButton == "Button27" ) then
      keyPressed = "BUTTON27"
   elseif ( keyPressed == "Button28" ) then
      keyPressed = "BUTTON28"
   elseif ( keyOrButton == "Button29" ) then
      keyPressed = "BUTTON29"
   elseif ( keyPressed == "Button30" ) then
      keyPressed = "BUTTON30"
   elseif ( keyOrButton == "Button31" ) then
      keyPressed = "BUTTON31"
   end
   
   if ( keyPressed == "LSHIFT" or
	keyPressed == "RSHIFT" or
	keyPressed == "LCTRL" or
	keyPressed == "RCTRL" or
	keyPressed == "LALT" or
	keyPressed == "RALT" ) then
      return;
   end
   if ( IsShiftKeyDown() ) then
      keyPressed = "SHIFT-"..keyPressed
   end
   if ( IsControlKeyDown() ) then
      keyPressed = "CTRL-"..keyPressed
   end
   if ( IsAltKeyDown() ) then
      keyPressed = "ALT-"..keyPressed
   end
   if ( keyPressed == "BUTTON1" or keyPressed == "BUTTON2" ) then
      return;
   end
	 
	 SetNewKeybind(keyPressed)
	 
end
