--[[ CustomChat ]]--
local ccVersion = "1.3"

--
-- Settings
--

local CustomChat_Defaults = {
	["options"] = {
		["editboxtop"] = true,
		["mousewheel"] = true,
		["timestamp"] = true,
		["timestampformat"] = "[%H:%M:%S]",
		["chatbuttons"] = false,
		["chatfade"] = false,
		["chatfadetime"] = 120,
		["stickyList"] = {
			["SAY"] = 1,
			["YELL"] = 0,
			["EMOTE"] = 0,
			["PARTY"] = 1,
			["RAID"] = 1,
			["GUILD"] = 1,
			["OFFICER"] = 1,
			["WHISPER"] = 0,
			["CHANNEL"] = 1,
			["SYSTEM"] = 0,
		},
	},
};

--
-- Command Binginds
--

SLASH_CUSTOMCHAT1 = "/cc"
SLASH_CUSTOMCHAT2 = "/customchat"
SlashCmdList["CUSTOMCHAT"] = function ( msg )
	cc_ToggleMenu();
end

--
-- Hooks
--

local Original_ChatFrame_OnEvent;
Original_ChatFrame_OnEvent = ChatFrame_OnEvent;

--
-- Code
--

function cc_ToggleMenu()
	if( ccMenu:IsVisible() ) then
		ccMenu:Hide();
	else
		ccMenu:Show();
	end
end

function cc_CheckSavedVars()
	if (not CustomChat) then
		CustomChat = CustomChat_Defaults;
		DEFAULT_CHAT_FRAME:AddMessage("CustomChat: First time running the addon. Using Defaults");
	end
	DEFAULT_CHAT_FRAME:AddMessage("CustomChat: version "..ccVersion.." Loaded!",0,0.8,1.0);
end

function cc_GetOption(name)
	return CustomChat["options"][name];
end

function cc_SetOption(name,value)
	CustomChat["options"][name] = value;
end

function cc_SetOptionSticky(name,value)
	CustomChat["options"]["stickyList"][name] = value;
end

function cc_MenuUpdate()
	ccMenu_editboxtop:SetChecked(CustomChat["options"]["editboxtop"]);
	ccMenu_mousewheel:SetChecked(CustomChat["options"]["mousewheel"]);
	ccMenu_timestamp:SetChecked(CustomChat["options"]["timestamp"]);
	ccMenu_timestampformat:SetText(CustomChat["options"]["timestampformat"]);
	ccMenu_chatbuttons:SetChecked(CustomChat["options"]["chatbuttons"]);
	ccMenu_chatfade:SetChecked(CustomChat["options"]["chatfade"]);
	ccMenu_chatfadetime:SetText(CustomChat["options"]["chatfadetime"]);
	ccMenu_stickysay:SetChecked(CustomChat["options"]["stickyList"]["SAY"]);
	ccMenu_stickyyell:SetChecked(CustomChat["options"]["stickyList"]["YELL"]);
	ccMenu_stickyemote:SetChecked(CustomChat["options"]["stickyList"]["EMOTE"]);
	ccMenu_stickyparty:SetChecked(CustomChat["options"]["stickyList"]["PARTY"]);
	ccMenu_stickyraid:SetChecked(CustomChat["options"]["stickyList"]["RAID"]);
	ccMenu_stickyguild:SetChecked(CustomChat["options"]["stickyList"]["GUILD"]);
	ccMenu_stickyofficer:SetChecked(CustomChat["options"]["stickyList"]["OFFICER"]);
	ccMenu_stickywhisper:SetChecked(CustomChat["options"]["stickyList"]["WHISPER"]);
	ccMenu_stickychannel:SetChecked(CustomChat["options"]["stickyList"]["CHANNEL"]);
	ccMenu_stickysystem:SetChecked(CustomChat["options"]["stickyList"]["SYSTEM"]);
end

local OnMouseWheel = function()
	if IsShiftKeyDown() then
		if (arg1 > 0) then this:PageUp()
		elseif (arg1 < 0) then this:PageDown()
		end
	elseif IsControlKeyDown() then
		if (arg1 > 0) then this:ScrollToTop()
		elseif (arg1 < 0) then this:ScrollToBottom()
		end
	else
		if (arg1 > 0) then this:ScrollUp()
		elseif (arg1 < 0) then this:ScrollDown()
		end
	end
end

function cc_ChatButtons()
	for i = 1, 7 do
		local chatframe = getglobal("ChatFrame"..i);
		local up = getglobal("ChatFrame"..i.."UpButton");
		local down = getglobal("ChatFrame"..i.."DownButton");
		local bottom = getglobal("ChatFrame"..i.."BottomButton");
		if (CustomChat["options"]["chatbuttons"] == true) then
			up:Show();
			down:Show();
			bottom:Show();
			if i == 1 then
				ChatFrameMenuButton:Show();
			end
		else
			up:Hide();
			down:Hide();
			bottom:Hide();
			if (i == 1) then
				ChatFrameMenuButton:Hide();
			end
		end
	end
end

function cc_ChatFade()
	for i = 1, 7 do
		local chatframe = getglobal("ChatFrame"..i)
		chatframe:SetFading(CustomChat["options"]["chatfade"])
	end
end

function cc_ChatFadeTime()
	for i = 1, 7 do
		local chatframe = getglobal("ChatFrame"..i)
		chatframe:SetTimeVisible(CustomChat["options"]["chatfadetime"])
	end
end

function cc_ChatTimestamp()
	if (CustomChat["options"]["timestamp"] == true) then
		ChatFrame_OnEvent = cc_ChatTimestamp_ChatFrame_OnEvent;
	else
		ChatFrame_OnEvent = Original_ChatFrame_OnEvent;
	end
end

function cc_ChatTimestamp_ChatFrame_OnEvent(event)
	Original_ChatFrame_OnEvent(event);
	if (not this.Original_AddMessage) then
		this.Original_AddMessage = this.AddMessage;
		this.AddMessage = cc_ChatTimestamp_AddMessage;
	end
end

function cc_ChatTimestamp_AddMessage(this, msg, r, g, b, id)
	if (not msg) then
		msg = "";
	end
	local stamp = date(CustomChat["options"]["timestampformat"]);
	local newmsg = stamp.." "..msg;
	this:Original_AddMessage(newmsg, r, g, b, id); --call the real AddMessage function
end

function cc_EditBoxTop()
	ChatFrameEditBox:ClearAllPoints()
	if (CustomChat["options"]["editboxtop"] == true) then
		ChatFrameEditBox:SetPoint("BOTTOMLEFT", ChatFrame1, "TOPLEFT");
		ChatFrameEditBox:SetPoint("BOTTOMRIGHT", ChatFrame1, "TOPRIGHT");
	else
		ChatFrameEditBox:SetPoint("BOTTOMLEFT", ChatFrame1, "BOTTOMLEFT");
		ChatFrameEditBox:SetPoint("BOTTOMRIGHT", ChatFrame1, "BOTTOMRIGHT");
	end
end

function cc_MouseWheel()
	if (CustomChat["options"]["mousewheel"] == true) then
		for i=1,9 do
			local x = getglobal("ChatFrame"..i)
			if (x) then
				x:EnableMouseWheel(true);
				x:SetScript("OnMouseWheel", OnMouseWheel);
			end
		end
	else
		for i=1,9 do
			local x = getglobal("ChatFrame"..i)
			if (x) then
				x:EnableMouseWheel(false);
			end
		end
	end
end

function cc_StickyChat()
	for i, v in pairs(CustomChat["options"]["stickyList"]) do
		ChatTypeInfo[i].sticky = v
	end
end

function cc_Update()
	cc_MenuUpdate();
	cc_EditBoxTop();
	cc_MouseWheel();
	cc_ChatTimestamp();
	cc_ChatButtons();
	cc_ChatFade();
	cc_StickyChat();
	cc_ChatFadeTime();
end
