MacOptionsFrameCheckButtons = { };
MacOptionsFrameCheckButtons["MOVIE_RECORDING_ENABLE_GUI"] = { index = 1, cvar = "MovieRecordingGUI", tooltipText = MOVIE_RECORDING_ENABLE_GUI_TOOLTIP};
MacOptionsFrameCheckButtons["MOVIE_RECORDING_ENABLE_SOUND"] = { index = 2, cvar = "MovieRecordingSound", tooltipText = MOVIE_RECORDING_ENABLE_SOUND_TOOLTIP};
MacOptionsFrameCheckButtons["MOVIE_RECORDING_ENABLE_CURSOR"] = { index = 3, cvar = "MovieRecordingCursor", tooltipText = MOVIE_RECORDING_ENABLE_CURSOR_TOOLTIP};
MacOptionsFrameCheckButtons["MOVIE_RECORDING_ENABLE_ICON"] = { index = 4, cvar = "MovieRecordingIcon", tooltipText = MOVIE_RECORDING_ENABLE_ICON_TOOLTIP};
MacOptionsFrameCheckButtons["MOVIE_RECORDING_ENABLE_RECOVER"] = { index = 5, cvar = "MovieRecordingRecover", tooltipText = MOVIE_RECORDING_ENABLE_RECOVER_TOOLTIP};
MacOptionsFrameCheckButtons["MOVIE_RECORDING_ENABLE_COMPRESSION"] = { index = 6, cvar = "MovieRecordingAutoCompress", tooltipText = MOVIE_RECORDING_ENABLE_COMPRESSION_TOOLTIP};
MacOptionsFrameCheckButtons["ITUNES_SHOW_FEEDBACK"] = { index = 7, cvar = "iTunesRemoteFeedback", tooltipText = ITUNES_SHOW_FEEDBACK_TOOLTIP};
MacOptionsFrameCheckButtons["ITUNES_SHOW_ALL_TRACK_CHANGES"] = { index = 8, cvar = "iTunesTrackDisplay", tooltipText = ITUNES_SHOW_ALL_TRACK_CHANGES_TOOLTIP};
MacOptionsFrameCheckButtons["MAC_DISABLE_OS_SHORTCUTS"] = { index = 9, cvar = "MacDisableOsShortcuts", tooltipText = MAC_DISABLE_OS_SHORTCUTS_TOOLTIP};
MacOptionsFrameCheckButtons["MAC_USE_COMMAND_AS_CONTROL"] = { index = 10, cvar = "MacUseCommandAsControl", tooltipText = MAC_USE_COMMAND_AS_CONTROL_TOOLTIP};
MacOptionsFrameCheckButtons["MAC_USE_COMMAND_LEFT_CLICK_AS_RIGHT_CLICK"] = { index = 11, cvar = "MacUseCommandLeftClickAsRightClick", tooltipText = MAC_USE_COMMAND_LEFT_CLICK_AS_RIGHT_CLICK_TOOLTIP};

local function MovieRecordingSupported()
	if (not IsMacClient()) then
		return false;
	elseif (not MovieRecording_IsSupported()) then
		return false;
	else
		return true;
	end
end

function MacOptionsFrame_OnLoad(self)
	if(IsMacClient()) then
		self:RegisterEvent("CVAR_UPDATE");
	end
end

function MacOptionsFrame_OnEvent(self, event, ...)
	if ( event == "CVAR_UPDATE" ) then
		local arg1, arg2 = ...
		local info = MacOptionsFrameCheckButtons[arg1];
		if ( info ) then
			info.value = arg2;
		end
	end
end

function MacOptionsFrame_DisableText(text)
	text:SetVertexColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b);
end

function MacOptionsFrame_DisableSlider(slider)
	local name = slider:GetName();
	local value = _G[name.."Value"];
	_G[name.."Thumb"]:Hide();
	MacOptionsFrame_DisableText( _G[name.."Text"] );
	MacOptionsFrame_DisableText( _G[name.."Low"] );
	MacOptionsFrame_DisableText( _G[name.."High"] );
	slider:Disable();
	if ( value ) then
		value:SetVertexColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b);
	end
end

function MacOptionsFrame_Load()
	for index, value in pairs(MacOptionsFrameCheckButtons) do
		local button = _G["MacOptionsFrameCheckButton"..value.index];
		local string = _G["MacOptionsFrameCheckButton"..value.index.."Text"];
		local checked = GetCVar(value.cvar);
		button:SetChecked(checked and checked ~= "0");
		
		string:SetText(_G[index]);
		button.tooltipText = value.tooltipText;
		
		if(not MovieRecording_IsSupported() and (value.index < 7)) then
			MacOptionsFrame_DisableCheckBox(button);
		else
			MacOptionsFrame_EnableCheckBox(button);
		end
	end


	

	if(not MovieRecording_IsSupported()) then
		UIDropDownMenu_DisableDropDown(MacOptionsFrameResolutionDropDown);
		UIDropDownMenu_DisableDropDown(MacOptionsFrameFramerateDropDown);
		UIDropDownMenu_DisableDropDown(MacOptionsFrameCodecDropDown);
		MacOptionsFrame_DisableSlider(MacOptionsFrameQualitySlider);
		MacOptionsButtonCompress:Disable();

		-- disable frame text
		MacOptionsFrame_DisableText(MacOptionsFrameText1);
		MacOptionsFrame_DisableText(MacOptionsFrameText2);
		MacOptionsFrame_DisableText(MacOptionsFrameText3);
		MacOptionsFrame_DisableText(MacOptionsFrameText4);
 
	else
		MacOptionsFrameQualitySlider:SetValue(GetCVar("MovieRecordingQuality"));
		if GetCVar("MovieRecordingGUI") then
			MacOptionsFrame_EnableCheckBox(_G["MacOptionsFrameCheckButton3"]);
		else
			MacOptionsFrame_DisableCheckBox(_G["MacOptionsFrameCheckButton3"]);
		end
	end
	if(not MovieRecordingSupported() or not MovieRecording_IsCursorRecordingSupported()) then
		local button = _G["MacOptionsFrameCheckButton3"];
		button:SetChecked(false);
		MacOptionsFrame_DisableCheckBox(button);
	end

	-- make sure that if UI recording is not enabled, that we disable cursor recording (it's part of the UI)
	if ( not MacOptionsFrameCheckButton1:GetChecked() ) then
		MacOptionsFrameCheckButton3:SetChecked(false);
		MacOptionsFrame_DisableCheckBox(MacOptionsFrameCheckButton3);		
	end
	
	local disableOSShortcutsButton = _G["MacOptionsFrameCheckButton9"];
	disableOSShortcutsButton.setFunc = function(checked)
		if ( (not MacOptions_IsUniversalAccessEnabled()) and (checked == "1")  ) then
			StaticPopup_Show("MAC_OPEN_UNIVERSAL_ACCESS");
			HideUIPanel(MacOptionsFrame);
			HideUIPanel(GameMenuFrame);
			_G["MacOptionsFrameCheckButton9"]:SetChecked(false);
		end
	end;
	
	if ( (not MacOptions_IsUniversalAccessEnabled()) and disableOSShortcutsButton:GetChecked() ) then
		disableOSShortcutsButton:SetChecked(false);
		SetCVar("MacDisableOSShortcuts", "0");
	end
end

function MacOptionsFrame_Save()
	for index, value in pairs(MacOptionsFrameCheckButtons) do
		local button = _G["MacOptionsFrameCheckButton"..value.index];
		if ( button:GetChecked() ) then
			value.value = "1";
		else
			value.value = "0";
		end

		SetCVar(value.cvar, value.value, index);
	end
	
	if (not MovieRecordingSupported()) then
		return;
	end

	MovieRecording_SaveSelectedWidth();
	SetCVar("MovieRecordingFramerate", UIDropDownMenu_GetSelectedValue(MacOptionsFrameFramerateDropDown));
	SetCVar("MovieRecordingCompression", UIDropDownMenu_GetSelectedValue(MacOptionsFrameCodecDropDown));

	SetCVar("MovieRecordingQuality", MacOptionsFrameQualitySlider:GetValue());
end

function MacOptionsFrame_Cancel()
	PlaySound("gsTitleOptionExit");
	HideUIPanel(MacOptionsFrame);
end

function MacOptionsFrameResolutionDropDown_UpdateSelection(self)
	local ratio = MovieRecording_GetAspectRatio();
	local width = MovieRecording_GetSelectedWidth();
	UIDropDownMenu_SetSelectedValue(self, width.."x"..floor(width*ratio), 1);
	UIDropDownMenu_SetWidth(MacOptionsFrameResolutionDropDown, 110);
end

function MacOptionsFrameResolutionDropDown_OnLoad(self)
	if (not MovieRecordingSupported()) then
		return;
	end
	
	-- make sure we get display size change events, so that we
	-- can update the available resolution drop down if we resize our
	-- window or whatnot
	self:RegisterEvent("DISPLAY_SIZE_CHANGED");	
end

function MacOptionsFrameResolutionDropDown_OnShow(self)
	-- sync the selected width with the internal cvar
	MovieRecording_LoadSelectedWidth();
	
	-- update the drop down list
	UIDropDownMenu_Initialize(self, MacOptionsFrameResolutionDropDown_Initialize);
	MacOptionsFrameResolutionDropDown_UpdateSelection(self);
end

function MacOptionsFrameResolutionDropDown_OnEvent(self, event, ...)
	if ( event == "DISPLAY_SIZE_CHANGED" ) then
		-- user resized the window.  update our supported resolutions, etc.
		UIDropDownMenu_Initialize(self, MacOptionsFrameResolutionDropDown_Initialize);
		MacOptionsFrameResolutionDropDown_UpdateSelection(self);
	end
end

local function greaterThanTableSort(a, b) return a > b end 

function MacOptionsFrameResolutionDropDown_Initialize()
	local info = UIDropDownMenu_CreateInfo();

	local ratio = MovieRecording_GetAspectRatio();
	
	local fullWidth = MovieRecording_GetFullWidth();
	local halfWidth = MovieRecording_GetHalfWidth();
	local quarterWidth = MovieRecording_GetQuarterWidth();

	local widthCount = MovieRecording_GetWidthCount();

	for widthIndex = 0, (widthCount - 1), 1 do
		local value = MovieRecording_GetWidthAt(widthIndex);
		local height = floor(value * ratio);
		
		info.text = value.."x"..height;
		info.value = info.text;
		info.func = MacOptionsFrameResolutionButton_OnClick;
		info.checked = nil;
		if value == tonumber(fullWidth) then
			info.tooltipTitle = MOVIE_RECORDING_FULL_RESOLUTION;
		elseif value == tonumber(halfWidth) then
			info.tooltipTitle = "Half Resolution";
		elseif value == tonumber(quarterWidth) then
			info.tooltipTitle = "Quarter Resolution";
		else
			info.tooltipTitle = nil;
		end
		UIDropDownMenu_AddButton(info);
	end
end

function MacOptionsFrameResolutionButton_OnClick(self)
	UIDropDownMenu_SetSelectedValue(MacOptionsFrameResolutionDropDown, self.value);
	
	-- update selected value in code
	xIndex = strfind(self.value, "x");
	width = strsub(self.value, 1, xIndex-1);
	MovieRecording_SetSelectedWidth(width);

	MacOptionsFrame_UpdateTime();
end

function MacOptionsFrameFramerateDropDown_OnLoad(self)
	if ( not IsMacClient() ) then
		return;
	end
	
	UIDropDownMenu_Initialize(self, MacOptionsFrameFramerateDropDown_Initialize);
	UIDropDownMenu_SetSelectedValue(self, GetCVar("MovieRecordingFramerate"));
	UIDropDownMenu_SetWidth(self, 110);
end

function MacOptionsFrameFramerateDropDown_Initialize()
	local info = UIDropDownMenu_CreateInfo();
	info.func = MacOptionsFrameFramerateDropDown_OnClick;
	info.checked = nil;

	info.text = MOVIE_RECORDING_FPS_HALF;
	info.value = "2";
	info.checked = nil;
	UIDropDownMenu_AddButton(info);

	info.text = MOVIE_RECORDING_FPS_THIRD;
	info.value = "3";
	info.checked = nil;
	UIDropDownMenu_AddButton(info);

	info.text = MOVIE_RECORDING_FPS_FOURTH;
	info.value = "4";
	info.checked = nil;
	UIDropDownMenu_AddButton(info);
	
	local fps = { "100", "90", "80", "70", "60", "50", "40", "30", "29.97", "25", "23.98", "20", "15", "10" };
	
	for index, value in pairs(fps) do
		info.text = value;
		info.value = info.text;
		info.checked = nil;
		UIDropDownMenu_AddButton(info);
	end
end

function MacOptionsFrameCodecDropDown_OnClick(self)
	UIDropDownMenu_SetSelectedValue(MacOptionsFrameCodecDropDown, self.value);
	MacOptionsFrame_UpdateTime();
end

function MacOptionsFrameCodecDropDown_OnLoad(self)
	if ( not IsMacClient() ) then
		return;
	end
	
	UIDropDownMenu_Initialize(self, MacOptionsFrameCodecDropDown_Initialize);
	UIDropDownMenu_SetSelectedValue(self, tonumber(GetCVar("MovieRecordingCompression")));
	UIDropDownMenu_SetWidth(self, 110);
end

function MacOptionsFrameCodecDropDown_Initialize()
	if (not MovieRecordingSupported()) then
		return;
	end
	
	local info = UIDropDownMenu_CreateInfo();
	info.func = MacOptionsFrameCodecDropDown_OnClick;
	info.checked = nil;
	
	local codecType = { 1835692129, 
						1635148593, 
						1768124260,
						1836070006 };
	local codecName = { MOVIE_RECORDING_MJPEG,
						MOVIE_RECORDING_H264,
						MOVIE_RECORDING_AIC,
						MOVIE_RECORDING_MPEG4 };
	local codecTooltip = { 	MOVIE_RECORDING_MJPEG_TOOLTIP,
							MOVIE_RECORDING_H264_TOOLTIP,
							MOVIE_RECORDING_AIC_TOOLTIP,
							MOVIE_RECORDING_MPEG4_TOOLTIP };
						
	for index, value in pairs(codecType) do
		if ( MovieRecording_IsCodecSupported(value)) then
			info.text = codecName[index];
			info.value = value;
			info.checked = nil;
			info.tooltipTitle = codecName[index];
			info.tooltipText = codecTooltip[index];
			UIDropDownMenu_AddButton(info);
		end
	end
end

function MacOptionsFrameFramerateDropDown_OnClick(self)
	UIDropDownMenu_SetSelectedValue(MacOptionsFrameFramerateDropDown, self.value);
	MacOptionsFrame_UpdateTime();
end

function MacOptionsFrame_SetDefaults()
	local checkButton, slider;
	for index, value in pairs(MacOptionsFrameCheckButtons) do
		checkButton = _G["MacOptionsFrameCheckButton"..value.index];
		local checked = GetCVarDefault(value.cvar);
		checkButton:SetChecked(checked and checked ~= "0");
	end    
	if((not MovieRecordingSupported()) or (not MovieRecording_IsCursorRecordingSupported())) then
		local button = _G["MacOptionsFrameCheckButton3"];
		button:SetChecked(false);
		MacOptionsFrame_DisableCheckBox(button);
	else
		local button = _G["MacOptionsFrameCheckButton3"];
		MacOptionsFrame_EnableCheckBox(button);
	end

	if (MovieRecordingSupported()) then
		UIDropDownMenu_Initialize(MacOptionsFrameFramerateDropDown, MacOptionsFrameFramerateDropDown_Initialize);
		UIDropDownMenu_SetSelectedValue(MacOptionsFrameFramerateDropDown, "29.97");
		UIDropDownMenu_Initialize(MacOptionsFrameCodecDropDown, MacOptionsFrameCodecDropDown_Initialize);
		UIDropDownMenu_SetSelectedValue(MacOptionsFrameCodecDropDown, 1635148593);
		UIDropDownMenu_Initialize(MacOptionsFrameResolutionDropDown, MacOptionsFrameResolutionDropDown_Initialize);
		local ratio = MovieRecording_GetAspectRatio();
		UIDropDownMenu_SetSelectedValue(MacOptionsFrameResolutionDropDown, "640x"..floor(640*ratio));
		
		MacOptionsFrameQualitySlider:SetValue(2);
		MacOptionsFrame_UpdateTime();
	end
end

function MacOptionsFrame_DisableCheckBox(checkBox)
	--checkBox:SetChecked(false);
	checkBox:Disable();
	_G[checkBox:GetName().."Text"]:SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b);
end

function MacOptionsFrame_EnableCheckBox(checkBox, setChecked, checked, isWhite)
	if ( setChecked ) then
		checkBox:SetChecked(checked and checked ~= "0");
	end
	checkBox:Enable();
	if ( isWhite ) then
		_G[checkBox:GetName().."Text"]:SetTextColor(HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b);
	else
		_G[checkBox:GetName().."Text"]:SetTextColor(NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b);
	end
	
end

function MacOptionsFrame_UpdateTime()
	if(not MovieRecording_IsSupported()) then
		return;
	end
	local resolution, framerate, xIndex, width, sound;
	framerate = UIDropDownMenu_GetSelectedValue(MacOptionsFrameFramerateDropDown);
	if tonumber(framerate) >= 10 then
		resolution = UIDropDownMenu_GetSelectedValue(MacOptionsFrameResolutionDropDown);
		xIndex = strfind(resolution, "x");
		width = tonumber(strsub(resolution, 1, xIndex-1));
		if(MacOptionsFrameCheckButton2:GetChecked()) then
			sound = true;
		else
			sound = false;
		end
		
		MacOptionsFrameText2:SetText(MovieRecording_MaxLength(	tonumber(width), 
																tonumber(framerate),
																sound));

		local dataRate = MovieRecording_DataRate(	tonumber(width), 
													tonumber(UIDropDownMenu_GetSelectedValue(MacOptionsFrameFramerateDropDown)),
													sound)
		MacOptionsFrameText4:SetText(dataRate);
		MovieRecordingFrameTextTooltip1:SetWidth(MacOptionsFrameText1:GetWidth() + MacOptionsFrameText2:GetWidth() + 2);
		MovieRecordingFrameTextTooltip2:SetWidth(MacOptionsFrameText3:GetWidth() + MacOptionsFrameText4:GetWidth() + 2);
	else
		MacOptionsFrameText2:SetText("");
		MacOptionsFrameText4:SetText("");
	end
end

function MacOptionsCancelFrame_OnShow()
	MacOptionsCancelFrameFileName:SetText(MovieRecording_GetMovieFullPath());

	local fileNameWidth = MacOptionsCancelFrameFileName:GetWidth();
	local questionWidth = MacOptionsCancelFrameQuestion:GetWidth();
	local frameWidth = math.min(600, math.max(fileNameWidth, questionWidth));

	MacOptionsCancelFrame:SetWidth(frameWidth + 40);
	MacOptionsCancelFrameFileName:SetWidth(frameWidth);
	MacOptionsCancelFrameQuestion:SetWidth(frameWidth); 
end

function MacOptionsCompressFrame_OnShow()
	local fileNameWidth = MacOptionsCompressFrameFileName:GetWidth();
	local frameWidth = math.max(350, fileNameWidth);

	MacOptionsCompressFrame:SetWidth(frameWidth + 40);
	MacOptionsCompressFrameFileName:SetWidth(frameWidth);
end