--[[--------------------------------------------------------------------
	PhanxChat
	Reduces chat frame clutter and enhances chat frame functionality.
	Copyright (c) 2006-2013 Phanx <addons@phanx.net>. All rights reserved.
	See the accompanying README and LICENSE files for more information.
	http://www.wowinterface.com/downloads/info6323-PhanxChat.html
	http://www.curse.com/addons/wow/phanxchat
----------------------------------------------------------------------]]

local _, PhanxChat = ...

local noop = function() end

------------------------------------------------------------------------

local function BottomButton_OnClick(self, button)
	PlaySound("igChatBottom")
	local frame = self:GetParent()
	if frame.ScrollToBottom then
		frame:ScrollToBottom()
	else
		frame:GetParent():ScrollToBottom()
	end
	_G[frame:GetName() .. "ButtonFrameBottomButton"]:Hide()
end

local function ChatFrame_OnShow(self)
	if not PhanxChat.db.HideButtons then return end
	if self:AtBottom() then
		_G[self:GetName() .. "ButtonFrameBottomButton"]:Hide()
	else
		_G[self:GetName() .. "ButtonFrameBottomButton"]:Show()
	end
end

function PhanxChat:HideButtons(frame)
	local name = frame:GetName()
	local buttonFrame = _G[name .. "ButtonFrame"]
	local upButton = _G[name .. "ButtonFrameUpButton"]
	local downButton = _G[name .. "ButtonFrameDownButton"]
	local bottomButton = _G[name .. "ButtonFrameBottomButton"]

	frame:HookScript("OnShow", ChatFrame_OnShow)

	if self.db.HideButtons then
		buttonFrame.Show = noop
		buttonFrame:Hide()

		upButton.Show = noop
		upButton:Hide()

		downButton.Show = noop
		downButton:Hide()

		bottomButton:ClearAllPoints()
		bottomButton:SetParent(frame)
		bottomButton:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, -4)
		bottomButton:SetAlpha(0.75)
		bottomButton:Hide()

		if not self.hooks[bottomButton] then
			self.hooks[bottomButton] = { }
		end
		if not self.hooks[bottomButton].OnClick then
			self.hooks[bottomButton].OnClick = bottomButton:GetScript("OnClick")
			bottomButton:SetScript("OnClick", BottomButton_OnClick)
		end

		if not InterfaceOptionsSocialPanelChatStyle.orig_value then
			InterfaceOptionsSocialPanelChatStyle.orig_value = GetCVar("chatMouseScroll")
			InterfaceOptionsSocialPanelChatStyle.orig_tooltip = InterfaceOptionsSocialPanelChatStyle.tooltip

			SetCVar("chatMouseScroll", "1")
			InterfaceOptionsSocialPanelChatMouseScroll_SetScrolling("1")
			InterfaceOptionsSocialPanelChatMouseScroll:Disable()
			InterfaceOptionsSocialPanelChatMouseScrollText:SetAlpha(0.5)
			InterfaceOptionsSocialPanelChatStyle.tooltip = string.format(PhanxChat.L["This option is locked by PhanxChat. If you wish to change it, you must first disable the \"%s\" option in PhanxChat."], PhanxChat.L["Hide buttons"])
		end
	else
		buttonFrame.Show = nil
		buttonFrame:Show()

		upButton.Show = nil
		upButton:Show()

		downButton.Show = nil
		downButton:Show()

		bottomButton:ClearAllPoints()
		bottomButton:SetParent(buttonFrame)
		bottomButton:SetPoint("BOTTOM", buttonFrame, "BOTTOM", 0, -7)
		bottomButton:SetAlpha(1)
		bottomButton:Show()

		if self.hooks[bottomButton] and self.hooks[bottomButton].OnClick then
			bottomButton:SetScript("OnClick", self.hooks[bottomButton].OnClick)
			self.hooks[bottomButton].OnClick = nil
		end

		if InterfaceOptionsSocialPanelChatStyle.value_orig then
			SetCVar("chatMouseScroll", InterfaceOptionsSocialPanelChatStyle.value_orig)
			InterfaceOptionsSocialPanelChatMouseScroll_SetScrolling(InterfaceOptionsSocialPanelChatStyle.value_orig)
			InterfaceOptionsSocialPanelChatMouseScroll:Enable()
			InterfaceOptionsSocialPanelChatMouseScrollText:SetAlpha(1)
			InterfaceOptionsSocialPanelChatStyle.tooltip = InterfaceOptionsSocialPanelChatStyle.tooltip_orig

			InterfaceOptionsSocialPanelChatStyle.value_orig = nil
			InterfaceOptionsSocialPanelChatStyle.tooltip_orig = nil
		end

		FCF_UpdateButtonSide(frame)
	end
end

------------------------------------------------------------------------

function PhanxChat:SetHideButtons(v)
	if self.debug then print("PhanxChat: SetHideButtons", v) end
	if type(v) == "boolean" then
		self.db.HideButtons = v
	end

	for frame in pairs(self.frames) do
		self:HideButtons(frame)
	end

	if self.db.HideButtons then
		ChatFrameMenuButton:SetScript("OnShow", ChatFrameMenuButton.Hide)
		ChatFrameMenuButton:Hide()

		FriendsMicroButton:SetScript("OnShow", FriendsMicroButton.Hide)
		FriendsMicroButton:Hide()

		if not self.hooks.BN_TOAST_LEFT_OFFSET then
			self.hooks.BN_TOAST_LEFT_OFFSET = BN_TOAST_LEFT_OFFSET
			BN_TOAST_LEFT_OFFSET = BN_TOAST_LEFT_OFFSET + ChatFrame1ButtonFrame:GetWidth() + 5
		end
	elseif not self.isLoading then
		ChatFrameMenuButton:SetScript("OnShow", nil)
		ChatFrameMenuButton:Show()

		FriendsMicroButton:SetScript("OnShow", nil)
		FriendsMicroButton:Show()

		if self.hooks.BN_TOAST_LEFT_OFFSET then
			BN_TOAST_LEFT_OFFSET = self.hooks.BN_TOAST_LEFT_OFFSET
			self.hooks.BN_TOAST_LEFT_OFFSET = nil
		end
	end
end

BNToastFrame:SetClampedToScreen(true)

table.insert(PhanxChat.RunOnLoad, PhanxChat.SetHideButtons)
table.insert(PhanxChat.RunOnProcessFrame, PhanxChat.HideButtons)