local E, L, V, P, G = unpack(ElvUI);
local mPlugin = "mMediaTag"
local mMT = E:NewModule(mPlugin, "AceHook-3.0", "AceEvent-3.0", "AceTimer-3.0");
local EP = LibStub("LibElvUIPlugin-1.0")
local addon, ns = ...
local Version = GetAddOnMetadata(addon, "Version")

local _G = _G
local ReloadUI = ReloadUI

P[mPlugin] = {
	["mMsg"] 				= true,
	["mTIcon"] 				= true,
	["mTIconSize"] 			= 32,
	["mProfUp"] 			= false,
	["mProfIcon"] 			= true,
	["mMenuUp"] 			= false,
	["mMenuColor"] 			= true,
	["mClassColorHover"] 	= true,
	["mHoverTexture"] 		= "|CFF8E44ADm|r|CFF2ECC71Media|r |CFF3498DB4a|r",
	["mPluginVersion"] 		= 0,
	["mClassNameplate"] 	= false,
	["mBackup"] 			= false,
	["mBackupHoverCb"] 		= 0,
	["mBackupHoverCg"] 		= 0,
	["mBackupHoverCr"] 		= 0,
	["mBackupHoverBb"] 		= 0,
	["mBackupHoverBg"] 		= 0,
	["mBackupHoverBr"] 		= 0,
	["mBackupHoverBorder"] 	= 0,
	["InstancInfoToolTip"]	= true,
	["InstancInfoName"]		= true,
	["SArchievement10"]		= false,
	["SArchievement15"]		= false,
	["DArchievement10"]		= false,
	["DArchievement15"]		= false,
	["SAffix"]				= false,
	
}

ns.mName 	= "|CFF8E44ADm|r|CFF2ECC71Media|r|CFF3498DBTag|r"
ns.mVersion = Version
ns.mColor1 	= "|CFFFFFFFF"	-- white
ns.mColor2 	= "|CFFF7DC6F" 	-- yellow
ns.mColor3 	= "|CFF8E44AD"	-- purple
ns.mColor4 	= "|CFFFE7B2C" 	-- orange
ns.mColor5 	= "|CFFE74C3C" 	-- red
ns.mColor6 	= "|CFF58D68D" 	-- green
ns.mColor7 	= "|CFF3498DB" 	-- blue
ns.mColor8 	= "|CFFB2BABB" 	-- gray
ns.normal	= "|CFF82E0AA"	-- nomral
ns.Heroic	= "|CFF85C1E9 "	-- HC
ns.Mythic 	= "|CFFBB8FCE"	-- Mythic

local function mVersionCheck()
	local mElvVer = tonumber(E.version)
	local mMinVer = 11.49
	local mErrorText = format(L["%sYour ElvUI (Ver. %s) version is no longer up to date, please update it to Ver.|r %s%s|r, %sto avoid problems with|r %s|r%s.|r"],ns.mColor5, mElvVer, ns.mColor6, mMinVer, ns.mColor5, ns.mName, ns.mColor5)
	
	if mElvVer < mMinVer then
		print (mErrorText)
		
		StaticPopupDialogs["mERROR"] = {
		text = mErrorText,
		button1 = "OK",
		timeout = 120,
		whileDead = true,
		hideOnEscape = true,
		preferredIndex = 3,
		}

		StaticPopup_Show ("mERROR")
	end
end

local function mBackupSettings(setting)
	if setting == "backup" then
		if not E.db[mPlugin].mBackup then
			E.db[mPlugin].mBackupHoverCb = E.db["nameplates"]["colors"]["glowColor"]["b"]
			E.db[mPlugin].mBackupHoverCg = E.db["nameplates"]["colors"]["glowColor"]["r"]
			E.db[mPlugin].mBackupHoverCr = E.db["nameplates"]["colors"]["glowColor"]["g"]
			E.db[mPlugin].mBackupHoverBb = E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["borderColor"]["b"]
			E.db[mPlugin].mBackupHoverBg = E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["borderColor"]["g"]
			E.db[mPlugin].mBackupHoverBr = E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["borderColor"]["r"]
			E.db[mPlugin].mBackupHoverBorder = E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["border"]
		end
	elseif setting == "restore" then
		if E.db[mPlugin].mBackup then
			E.db["nameplates"]["colors"]["glowColor"]["b"] = E.db[mPlugin].mBackupHoverCb
			E.db["nameplates"]["colors"]["glowColor"]["r"] = E.db[mPlugin].mBackupHoverCg
			E.db["nameplates"]["colors"]["glowColor"]["g"] = E.db[mPlugin].mBackupHoverCr
			E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["borderColor"]["b"] = E.db[mPlugin].mBackupHoverBb
			E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["borderColor"]["g"] = E.db[mPlugin].mBackupHoverBg
			E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["borderColor"]["r"] = E.db[mPlugin].mBackupHoverBr
			E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["border"] = E.db[mPlugin].mBackupHoverBorder
		end
	end
end

local function mMisc()
	local classColor
	classColor = E:ClassColor(E.myclass, true)
	mBackupSettings("backup")
	if E.global["nameplate"]["filters"]["ElvUI_Target"] then
		E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["borderColor"]["b"] = classColor.b
		E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["borderColor"]["g"] = classColor.g
		E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["borderColor"]["r"] = classColor.r
		E.global["nameplate"]["filters"]["ElvUI_Target"]["actions"]["color"]["border"] = true
	end

	E.db["nameplates"]["colors"]["glowColor"]["b"] = classColor.b
	E.db["nameplates"]["colors"]["glowColor"]["r"] = classColor.r
	E.db["nameplates"]["colors"]["glowColor"]["g"] = classColor.g
end

function mMT:StartTimer(mTime)
  self:ScheduleTimer("TimerFeedback", mTime)
end

function mMT:TimerFeedback()
  print("5 seconds passed")
end

function mMT:InsertOptions()
	E.Options.args.mPlugin = {
		order = 100,
		type = "group",
		name = ns.mName,
		args = {
			header1 = {
				order = 1,
				type = "header",
				name = ns.mName,
			},
			logo = {
				order = 2,
				type = 'description',
				name = "",
				fontSize = 'medium',
				image = function() return 'Interface\\AddOns\\ElvUI_mMediaTag\\media\\textures\\logo.tga', 320, 80 end,
			},
			header2 = {
				order = 3,
				type = "header",
				name = "|CFF8E44ADby|r |CFF2ECC71Blin|r|CFF3498DBkii|r",
			},
			description1 = {
				order = 4,
				type = "description",
				name = ns.mName .. L[" is an addon for ElvUI with additional Statusbar textures, Tags, a game menu and professional menu for the datatext bars."],
			},
			spacer1 = {
				order = 5,
				type = "description",
				name = "\n\n\n",
			},
			header3 = {
				order = 6,
				type = "header",
				name = L["Configuration"],
			},
			setting1 = {
				order = 7,
				type = "group",
				name = L["General"],
				args = {
					mTGeneral = {
						order = 1,
						type = 'toggle',
						name = L["Greeting text at start"],
						desc = L["Enables or disables the welcome text, at startup."],
						get = function(info)
							return E.db[mPlugin].mMsg
						end,
						set = function(info, value)
							E.db[mPlugin].mMsg = value
						end,
					},
					mTColor = {
						order = 2,
						type = 'toggle',
						name = L["Class colered hover texture"],
						desc = L["Enables or disables Class colered hover texture."],
						get = function(info)
							return E.db[mPlugin].mClassColorHover
						end,
						set = function(info, value)
							E.db[mPlugin].mClassColorHover = value
							E:StaticPopup_Show("CONFIG_RL")
						end,
					},
					mTexture = {
						order = 3,
						type = "select",
						dialogControl = 'LSM30_Statusbar',
						name = L["Menu Hover Texture"],
						values = _G.AceGUIWidgetLSMlists.statusbar,
						get = function(info)
							return E.db[mPlugin].mHoverTexture
						end,
						set = function(info, value)
							E.db[mPlugin].mHoverTexture = value;
							E:StaticPopup_Show("CONFIG_RL")
						end,
					},
					mNameplate = {
						order = 4,
						type = 'toggle',
						name = L["Class colored Namplates"],
						desc = L["Class colored Namplates Hover and Boarder color."],
						get = function(info)
							return E.db[mPlugin].mClassNameplate
						end,
						set = function(info, value)
							E.db[mPlugin].mClassNameplate = value
							if value == false then
								mBackupSettings("backup")
							end
							E:StaticPopup_Show("CONFIG_RL")
						end,
					},
				},
			},
			setting2 = {
				order = 7,
				type = "group",
				name = L["Tooltip"],
				args = {
					mTIcon = {
						order = 2,
						type = 'toggle',
						name = L["Tooltip Icon"],
						desc = L["Enables or disables Tooltip Icon"],
						get = function(info)
							return E.db[mPlugin].mTIcon
						end,
						set = function(info, value)
							E.db[mPlugin].mTIcon = value
						end,
					
					},
					mRIcon = {
							order = 3,
							name = L["Iconsize"],
							desc = L["Tooltip Icon size."],
							type = "range",
							min = 16, max = 128, step = 2,
							get = function(info)
							return E.db[mPlugin].mTIconSize
							end,
							set = function(info, value)
								E.db[mPlugin].mTIconSize = value
							end,
					},
				},
			},
			setting3 = {
				order = 8,
				type = "group",
				name = L["Profession Menu"],
				args = {
					mTProfession1 = {
						order = 1,
						type = 'toggle',
						name = L["Fold up"],
						desc = L["When activated, the Profession Menu opens upwards."],
						get = function(info)
							return E.db[mPlugin].mProfUp
						end,
						set = function(info, value)
							E.db[mPlugin].mProfUp = value
						end,
					},
					mTProfession2 = {
						order = 2,
						type = 'toggle',
						name = L["Symbols"],
						desc = L["Displays the icons for the professions in the menu."],
						get = function(info)
							return E.db[mPlugin].mProfIcon
						end,
						set = function(info, value)
							E.db[mPlugin].mProfIcon = value
						end,
					},
				},
			},
			setting4 = {
				order = 9,
				type = "group",
				name = L['System Menu'],
				args = {
					mTMenu1 = {
						order = 1,
						type = 'toggle',
						name = L["Fold up"],
						desc = L["When activated, the System Menu opens upwards."],
						get = function(info)
							return E.db[mPlugin].mMenuUp
						end,
						set = function(info, value)
							E.db[mPlugin].mMenuUp = value
						end,
					},
					mTMenu2 = {
						order = 2,
						type = 'toggle',
						name = L["Colored System Menu"],
						desc = L["Activates a colored system menu."],
						get = function(info)
							return E.db[mPlugin].mMenuColor
						end,
						set = function(info, value)
							E.db[mPlugin].mMenuColor = value
						end,
					},
					mSystemTip = {
						order = 3,
						type = 'toggle',
						name = L["Tooltip Instanceinfo"],
						desc = L["Shows Inctance Info and Mythic Plus key tone in tooltip."],
						get = function(info)
							return E.db[mPlugin].InstancInfoToolTip
						end,
						set = function(info, value)
							E.db[mPlugin].InstancInfoToolTip = value
						end,
					},
					mSystemName = {
						order = 4,
						type = 'toggle',
						name = L["Text to Instance Name"],
						desc = L["Display the instance name instead of System Menu."],
						get = function(info)
							return E.db[mPlugin].InstancInfoName
						end,
						set = function(info, value)
							E.db[mPlugin].InstancInfoName = value
						end,
					},
					mSystemAffix = {
						order = 5,
						type = 'toggle',
						name = L["Weekly Affixes"],
						desc = L["Shows the Weekly Affixes."],
						get = function(info)
							return E.db[mPlugin].SAffix
						end,
						set = function(info, value)
							E.db[mPlugin].SAffix = value
						end,
					},
					mSystemArchievement10 = {
						order = 6,
						type = 'toggle',
						name = L["Mythic +10 Archievement"],
						desc = L["Display the Archievement for Mythic +10 Dungeons."],
						get = function(info)
							return E.db[mPlugin].SArchievement10
						end,
						set = function(info, value)
							E.db[mPlugin].SArchievement10 = value
						end,
					},
					mSystemArchievement15 = {
						order = 7,
						type = 'toggle',
						name = L["Mythic +15 Archievement"],
						desc = L["Display the Archievement for Mythic +15 Dungeons."],
						get = function(info)
							return E.db[mPlugin].SArchievement15
						end,
						set = function(info, value)
							E.db[mPlugin].SArchievement15 = value
						end,
					},
				},
			},
			setting5 = {
				order = 10,
				type = "group",
				name = L['Addons'],
				args = {
					addonsdescription1 = {
					order = 4,
					type = "description",
					name = ns.mName .. L[" Here you can install addon profiles for my ElvUI profile."],
					},
					addonsspacer1 = {
						order = 5,
						type = "description",
						name = "\n\n\n",
					},
					install = {
						order = 7,
						type = "execute",
						name = L["Install"],
						desc = L["Run the installation process."],
						func = function() mMT:mSetup() end,
					},
				},
			},
			setting6 = {
				order = 11,
				type = "group",
				name = L['Dungeon'],
				args = {
					mDungeonArchievement10 = {
						order = 1,
						type = 'toggle',
						name = L["Mythic +10 Archievement"],
						desc = L["Display the Archievement for Mythic +10 Dungeons."],
						get = function(info)
							return E.db[mPlugin].DArchievement10
						end,
						set = function(info, value)
							E.db[mPlugin].DArchievement10 = value
						end,
					},
					mDungeonArchievement15 = {
						order = 2,
						type = 'toggle',
						name = L["Mythic +15 Archievement"],
						desc = L["Display the Archievement for Mythic +15 Dungeons."],
						get = function(info)
							return E.db[mPlugin].DArchievement15
						end,
						set = function(info, value)
							E.db[mPlugin].DArchievement15 = value
						end,
					},
				},
			},
			setting7 = {
				order = 12,
				type = "group",
				name = L['Chatbaground'],
				args = {
					chatdescription1 = {
							order = 1,
							type = "description",
							name = L["Chath backgrounds, copy the path from the input fields to the chat settings to use the backgrounds."],
					},
					chatdescription2 = {
							order = 2,
							type = "description",
							name = "\n\n\n",
					},
					chat1 = {
							order = 3,
							name = "mChat1.1",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat1.1.tga" end,
					},
					chat2 = {
							order = 4,
							name = "mChat1.2",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat1.2.tga" end,
					},
					chat3 = {
							order = 5,
							name = "mChat2.1",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat2.1.tga" end,
					},
					chat4 = {
							order = 6,
							name = "mChat2.2",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat2.2.tga" end,
					},
					chat5 = {
							order = 7,
							name = "mChat3.1",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat3.1.tga" end,
					},
					chat6 = {
							order = 8,
							name = "mChat3.2",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat3.2.tga" end,
					},
					chat7 = {
							order = 9,
							name = "mChat4.1",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat4.1.tga" end,
					},
					chat8 = {
							order = 10,
							name = "mChat4.2",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat4.2.tga" end,
					},
					chat9 = {
							order = 11,
							name = "mChat5.1",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat5.1.tga" end,
					},
					chat10 = {
							order = 12,
							name = "mChat5.2",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat5.2.tga" end,
					},
					chat11 = {
							order = 13,
							name = "mChat6.1",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat6.1.tga" end,
					},
					chat12 = {
							order = 14,
							name = "mChat6.2",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat6.2.tga" end,
					},
					chat13 = {
							order = 15,
							name = "mChat7.1",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat7.1.tga" end,
					},
					chat14 = {
							order = 16,
							name = "mChat7.2",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat7.2.tga" end,
					},
					chat15 = {
							order = 17,
							name = "mChat8",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat8.tga" end,
					},
					chat16 = {
							order = 18,
							name = "mChat9",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat9.tga" end,
					},
					chat17 = {
							order = 19,
							name = "mChat10",
							type = 'input',
							width = 'full',
							get = function() return "Interface\\Addons\\ElvUI_mMediaTag\\media\\textures\\Chat\\mChat10.tga" end,
					},
				},
			},
		},
	}
end

function mMT:Initialize()
	mVersionCheck()
	
	if E.db[mPlugin].mMsg then
		print (format(L["Welcome to %s version |CFF8E44AD%q|r, for |CFFFE7B2CElvUI|r!"],ns.mName, Version))
	end
	
	if E.db[mPlugin].mPluginVersion ~= Version then
		mMT:Changelog()
	end
	
	if E.db[mPlugin].mClassNameplate == true then
		mMisc()
	end
	EP:RegisterPlugin(addon, mMT.InsertOptions)
end

E:RegisterModule(mMT:GetName())