--------------------------------------------------------------------------------------------------
----------------------------------   Deome's Chat Adverts   --------------------------------------
---------------------------   by Deome (@deome) - heydeome@gmail.com   ---------------------------
local									VERSION = "1.0.2"									    --
--																								--
--																								--
---------------------------------------   Deome's License   --------------------------------------
--																								--
-- 		Copyright (c) 2014, 2015, 2016 D. Deome (@deome) - heydeome@gmail.com					--
--																								--
-- 		This software is provided 'as-is', without any express or implied						--
-- 		warranty. In no event will the author(s) be held liable for any damages					--
-- 		arising from the use of this software. This software, and the ideas, processes, 		--
--		functions, and all other intellectual property contained within may not be modified,	--
--		distributed, or used in any other works without the written permission of the			--
--		author.																					--
--																								--
--		Any use with permission of author must include the above license and copyright,			--
--		and any other license and copyright notices noted within this software.			 		--
--																								--
-------------------------------------   ZO Obligatory Spam   -------------------------------------
--																								--
-- 		"This Add-on is not created by, affiliated with or sponsored by ZeniMax 		  		--
--		Media Inc. or its affiliates. The Elder Scrolls® and related logos are registered 	 	--
--		trademarks of ZeniMax Media Inc. in the United States and/or other countries. 			--
--		All rights reserved."																	--
--																								--
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------
----------------------------------------   Libraries   -------------------------------------------
--------------------------------------------------------------------------------------------------

local LIB_LAM2 = LibStub("LibAddonMenu-2.0")


--------------------------------------------------------------------------------------------------
----------------------------------------   Namespace   -------------------------------------------
--------------------------------------------------------------------------------------------------

ddChatAdverts = {
	["Name"] 					= "ddChatAdverts",
	["Version"] 				= VERSION,
	["SavedVarsVersion"] 		= 1,
	["Locale"] 					= GetCVar('Language.2') or "en",
	["Adverts"] 				= {},
	["Settings"] = {
		["Init"] 				= function() end,
		["Advert1"] 			= {},
		["Advert2"] 			= {},
		["Advert3"] 			= {},
		["Advert4"] 			= {},
		["Advert5"] 			= {},
		["Notify"] 				= {},
		["Debug"] 				= {},
	},
	["Init"] 					= function() end,
	["Boot"] 					= function() end,
	["AdvertCycle"]				= function() end,
	["PostAdvert"] 				= function() end,
	["mControls"] 				= function() end,
	["mPanel"] 					= function() end,
}


--------------------------------------------------------------------------------------------------
----------------------------------------   Constants   -------------------------------------------
--------------------------------------------------------------------------------------------------

local ADDON_NAME 	= ddChatAdverts.Name
local SV_VERSION	= ddChatAdverts.SavedVarsVersion
local LOCALE		= ddChatAdverts.Locale
local Settings		= ddChatAdverts.Settings

local ADVERT_CYCLE 	= 1
local BUFFER_TABLE 	= {}
local LAM_PANEL	= nil


--------------------------------------------------------------------------------------------------
-------------------------------   Modular Controls by @Deome   -----------------------------------
--																								--
--						 ----  For LibAddonMenu 2.0 by Seerah  ----								--
--				----   With gratitude and credit for this wonderful library   ----				--
--																								--
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------
--			    -- Modular Controls allow even more customization for LAM2 --					--
--					        -- (which already offers so much!) --								--
--		  -- and make plugins, updates, and common settings simple for everyone --				--
--------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------
------------------------------------   LAM2 Panel Layout   ---------------------------------------
--------------------------------------------------------------------------------------------------

function ddChatAdverts:mControls()
	local controls = {
		self.Settings.Advert1:Init(),
		self.Settings.Advert2:Init(),
		self.Settings.Advert3:Init(),
		self.Settings.Advert4:Init(),
		self.Settings.Advert5:Init(),
--		self.Settings.Notify:Init(),
--		self.Settings.Debug:Init(),
	}
	return controls
end

function ddChatAdverts:mPanel()
	local panel = {
		type = "panel",
		name = "Deome's Chat Adverts",
		displayName = "Deome's Chat Adverts",
		author = "D. Deome (@deome)",
		version = self.Version,
		registerForRefresh = true,
		registerForDefaults = true,
	}
	return panel
end


--------------------------------------------------------------------------------------------------
----------------------------------   LAM2 Modular Controls   -------------------------------------
--------------------------------------------------------------------------------------------------

function ddChatAdverts.Settings:Init()
	self.Advert1:Init()
	self.Advert2:Init()
	self.Advert3:Init()
	self.Advert4:Init()
	self.Advert5:Init()
end

function Settings.Advert1:Init()
	self.type = "editbox"
	self.name = GetString(DDCA_SETTINGS_ADVERT1_NAME)
	self.tooltip = ""
	self.width = "full"

	self.isMultiline = true
	self.default = GetString(DDCA_SETTINGS_SPAM_BOX)
	self.getFunc = function() if self.value == nil then self.value = self.default end return self.value end
	self.setFunc = function(text) self.value = text end
	return self
end

function Settings.Advert2:Init()
	self.type = "editbox"
	self.name = GetString(DDCA_SETTINGS_ADVERT2_NAME)
	self.tooltip = ""
	self.width = "full"

	self.isMultiline = true
	self.default = GetString(DDCA_SETTINGS_SPAM_BOX)
	self.getFunc = function() if self.value == nil then self.value = self.default end return self.value end
	self.setFunc = function(text) self.value = text end
	return self
end

function Settings.Advert3:Init()
	self.type = "editbox"
	self.name = GetString(DDCA_SETTINGS_ADVERT3_NAME)
	self.tooltip = ""
	self.width = "full"

	self.isMultiline = true
	self.default = GetString(DDCA_SETTINGS_SPAM_BOX)
	self.getFunc = function() if self.value == nil then self.value = self.default end return self.value end
	self.setFunc = function(text) self.value = text end
	return self
end

function Settings.Advert4:Init()
	self.type = "editbox"
	self.name = GetString(DDCA_SETTINGS_ADVERT4_NAME)
	self.tooltip = ""
	self.width = "full"

	self.isMultiline = true
	self.default = GetString(DDCA_SETTINGS_SPAM_BOX)
	self.getFunc = function() if self.value == nil then self.value = self.default end return self.value end
	self.setFunc = function(text) self.value = text end
	return self
end

function Settings.Advert5:Init()
	self.type = "editbox"
	self.name = GetString(DDCA_SETTINGS_ADVERT5_NAME)
	self.tooltip = ""
	self.width = "full"

	self.isMultiline = true
	self.default = GetString(DDCA_SETTINGS_SPAM_BOX)
	self.getFunc = function() if self.value == nil then self.value = self.default end return self.value end
	self.setFunc = function(text) self.value = text end
	return self
end


--------------------------------------------------------------------------------------------------
----------------------------------------   Functions   -------------------------------------------
--------------------------------------------------------------------------------------------------

function ddChatAdverts.KeybindAdvert(AdNum)
	ddChatAdverts:RunAdvert(AdNum)
end

function ddChatAdverts.KeybindSettings()
	LIB_LAM2:OpenToPanel(LAM_PANEL)
end

function ddChatAdverts:RunCycle()
	local ChatEditControl = CHAT_SYSTEM.textEntry.editControl
	local Adverts = {
		self.Settings.Advert1.getFunc(),
		self.Settings.Advert2.getFunc(),
		self.Settings.Advert3.getFunc(),
		self.Settings.Advert4.getFunc(),
		self.Settings.Advert5.getFunc(),
	}

	AdText = Adverts[ADVERT_CYCLE]

	if not AdText then return end
	if not ChatEditControl:HasFocus() then
		StartChatInput()
	end
	ChatEditControl:InsertText(AdText)

	if ADVERT_CYCLE >= #Adverts then
		ADVERT_CYCLE = 1
	else
		ADVERT_CYCLE = ADVERT_CYCLE + 1
	end
end

function ddChatAdverts:RunAdvert(AdNum)
	local ChatEditControl = CHAT_SYSTEM.textEntry.editControl
	local Advert1 = ddChatAdverts.Settings.Advert1.getFunc()
	local Advert2 = ddChatAdverts.Settings.Advert2.getFunc()
	local Advert3 = ddChatAdverts.Settings.Advert3.getFunc()
	local Advert4 = ddChatAdverts.Settings.Advert4.getFunc()
	local Advert5 = ddChatAdverts.Settings.Advert5.getFunc()

	if not ChatEditControl:HasFocus() then
		StartChatInput()
	end

	if AdNum == 1 then
		ChatEditControl:InsertText(Advert1)
	elseif AdNum == 2 then
		ChatEditControl:InsertText(Advert2)
	elseif AdNum == 3 then
		ChatEditControl:InsertText(Advert3)
	elseif AdNum == 4 then
		ChatEditControl:InsertText(Advert4)
	elseif AdNum == 5 then
		ChatEditControl:InsertText(Advert5)
	else
		return
	end
end

function ddChatAdverts.AdvertCycle()
	ddChatAdverts:RunCycle()
end

function ddChatAdverts.Boot(eventCode, addonName)
	if addonName ~= ADDON_NAME then
		return
	else
		local Settings = ddChatAdverts
		ddChatAdverts = ZO_SavedVars:NewAccountWide("ddCA_SV", SV_VERSION, nil, ddChatAdverts)

		ddChatAdverts.Settings:Init()
		LAM_PANEL = LIB_LAM2:RegisterAddonPanel("ddCASettings", ddChatAdverts:mPanel())
		LIB_LAM2:RegisterOptionControls("ddCASettings", ddChatAdverts:mControls())

		EVENT_MANAGER:UnregisterForEvent(ADDON_NAME, EVENT_ADD_ON_LOADED)
	end
end


EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_ADD_ON_LOADED, ddChatAdverts.Boot)
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------