local dewdrop = AceLibrary("Dewdrop-2.0")
local media = LibStub("LibSharedMedia-2.0")
local L = AceLibrary("AceLocale-2.2"):new("Proximo")
local test = false

local defaults = {
		scale=1,
		x=0,
		y=0,
		framecolor = {r = 0, g = 0, b = .1, a = 1},
		manacolor = {r = .18, g = .44, b = .75},
		highlightcolor = {r = 1, g = 1, b = 0},
		fontsize=16,
		padding=5,
		barheight=22,
		barwidth=200,
		header=true,
		rightclick="disable",
		leftclick="target",
		leftspellname="",
		spellname="",
		texture="Minimalist",
		font="Friz Quadrata TT",
		announce="disabled",
		ma=UnitName("player"),
		sync=true,
		partytargets=true,
		partytargetheight=4,
		mana=true,
		manaheight=4,
		highlighttar=true,
		highlight="solid",
		lowhealth=true,
		lowhealthsound=true,
		classtext=nil,
}

function Proximo:GetDefaultConfig()
	return defaults
end

function Proximo:SetupOptions()
	media:Register("statusbar", "Minimalist", "Interface\\Addons\\Proximo\\Textures\\Minimalist")
	self.options = { 
	  type="group",
	  args = {
	  	colors = {
	  		type="group",
	  		name=L["Colors"],
				desc=L["Colors"],
	  		order=1,
	  		args = {
			  	framecolor = {
			  		type="color",
						name=L["FrameColor"],
				    desc=L["FrameColor"],
				    get=function()
				        return self.db.profile.framecolor.r, self.db.profile.framecolor.g, self.db.profile.framecolor.b, self.db.profile.framecolor.a
				    end,
				    set=function(r, g, b, a)
				        self.db.profile.framecolor.r, self.db.profile.framecolor.g, self.db.profile.framecolor.b, self.db.profile.framecolor.a = r, g, b, a
					 			self:UpdateFrame()
				    end,
				    hasAlpha=true,
				    order=1,
					},
					manacolor = {
			  		type="color",
						name=L["ManaColor"],
				    desc=L["ManaColor"],
				    get=function()
				        return self.db.profile.manacolor.r, self.db.profile.manacolor.g, self.db.profile.manacolor.b
				    end,
				    set=function(r, g, b)
				        self.db.profile.manacolor.r, self.db.profile.manacolor.g, self.db.profile.manacolor.b = r, g, b
					 			self:UpdateFrame()
				    end,
				    hasAlpha=false,
				    order=2,
					},
					highlightcolor = {
			  		type="color",
						name=L["HighlightColor"],
				    desc=L["HighlightColor"],
				    get=function()
				        return self.db.profile.highlightcolor.r, self.db.profile.highlightcolor.g, self.db.profile.highlightcolor.b
				    end,
				    set=function(r, g, b)
				        self.db.profile.highlightcolor.r, self.db.profile.highlightcolor.g, self.db.profile.highlightcolor.b = r, g, b
					 			self:UpdateFrame()
				    end,
				    hasAlpha=false,
				    order=3,
					},
				},
			},
			frame = {
	  		type="group",
	  		name=L["Frame"],
				desc=L["Frame"],
	  		order=2,
	  		args = {
		  		fontsize = {
						type="range",
						name=L["FontSize"],
						desc=L["FontSize"],
						get=function() return self.db.profile.fontsize end,
			  		set=function(v) self.db.profile.fontsize = v 
			  			self:UpdateFrame()
			  		end,
						min=8,
						max=36,
						step=1,
						order=1,
			  	},
			  	font = {
						type = 'text',
						name = L["Font"],
						desc = L["Font"],
						validate = media:List('font'),
						get=function() return self.db.profile.font end,
			  		set=function(v) self.db.profile.font = v 
			  			self:UpdateFrame()
			  		end,
			  		order=2,
					},
			  	barwidth = {
						type="range",
						name=L["BarWidth"],
						desc=L["BarWidth"],
						get=function() return self.db.profile.barwidth end,
			  		set=function(v) self.db.profile.barwidth = v 
			  			self:UpdateFrame()
			  		end,
						min=100,
						max=500,
						step=5,
						order=3,
			  	},
			  	barheight = {
						type="range",
						name=L["BarHeight"],
						desc=L["BarHeight"],
						get=function() return self.db.profile.barheight end,
			  		set=function(v) self.db.profile.barheight = v 
			  			self:UpdateFrame()
			  		end,
						min=10,
						max=40,
						step=1,
						order=4,
			  	},
			  	texture = {
						type = 'text',
						name = L["BarTexture"],
						desc = L["BarTexture"],
						validate = media:List('statusbar'),
						get=function() return self.db.profile.texture end,
			  		set=function(v) self.db.profile.texture = v 
			  			self:UpdateFrame()
			  		end,
			  		order=5,
					},
					padding = {
						type="range",
						name=L["Padding"],
						desc=L["Padding"],
						get=function() return self.db.profile.padding end,
			  		set=function(v) self.db.profile.padding = v 
			  			self:UpdateFrame()
			  		end,
						min=1,
						max=30,
						step=1,
						order=6,
			  	},
					scale = {
						type="range",
						name=L["Scale"],
						desc=L["Scale"],
						get=function() return self.db.profile.scale end,
			  		set=function(v) self.db.profile.scale = v 
			  			self:UpdateFrame()
			  		end,
						min=0.1,
						max=3,
						step=0.1,
						order=7,
			  	},
			  	header = {
						type='toggle',
						name=L["Header"],
						desc=L["Header"],
						get=function() return self.db.profile.header end,
						set=function(v) self.db.profile.header = v 
			  			self:UpdateFrame()
			  		end,
						order=8,
					},
	  		},
	  	},
	  	misc = {
	  		type="group",
	  		name=L["Misc"],
				desc=L["Misc"],
	  		order=3,
	  		args = {
	  			classtext = {
						type='toggle',
						name=L["ClassText"],
						desc=L["ClassText"],
						get=function() return self.db.profile.classtext end,
						set=function(v) self.db.profile.classtext = v 
			  			self:UpdateFrame()
			  		end,
						order=1,
					},
	  			sync = {
						type='toggle',
						name=L["Sync"],
						desc=L["SyncDesc"],
						get=function() return self.db.profile.sync end,
						set=function(v) self.db.profile.sync = v 
			  			self:UpdateFrame()
			  		end,
						order=2,
					},
					s0 = {
						type = "header",
						name = " ",
						order = 3,
					},
					leftclick = {
			  		type = "text",
						name = L["LeftClick"],
				    desc = L["LeftClick"],
				    get = function() return self.db.profile.leftclick end,
				    set = function(v) self.db.profile.leftclick = v 
				    	self:UpdateFrame()
				    end,
				    validate = {
				    	target = TARGET,
				    	focus = L["Focus"],
					    spell = L["CastSpell"],
					    macro = MACRO,
					    disable = ADDON_DISABLED,
						},
						order = 4,
					},
					leftspellname = {
						type = 'text',
						name = L["CastSpellNameLeft"],
						desc = L["CastSpellNameDesc"],
						usage = "<spellname>",
						get=function() return self.db.profile.leftspellname end,
			  		set=function(v) self.db.profile.leftspellname = v 
			  			self:UpdateFrame()
			  		end,
			  		disabled = function() return self.db.profile.leftclick ~= "spell" and self.db.profile.leftclick ~= "macro" end,
			  		order=5,
					},
	  			rightclick = {
			  		type = "text",
						name = L["RightClick"],
				    desc = L["RightClick"],
				    get = function() return self.db.profile.rightclick end,
				    set = function(v) self.db.profile.rightclick = v 
				    	self:UpdateFrame()
				    end,
				    validate = {
				    	target = TARGET,
				    	focus = L["Focus"],
					    spell = L["CastSpell"],
					    macro = MACRO,
					    disable = ADDON_DISABLED,
						},
						order = 6,
					},
					spellname = {
						type = 'text',
						name = L["CastSpellName"],
						desc = L["CastSpellNameDesc"],
						usage = "<spellname>",
						get=function() return self.db.profile.spellname end,
			  		set=function(v) self.db.profile.spellname = v 
			  			self:UpdateFrame()
			  		end,
			  		disabled = function() return self.db.profile.rightclick ~= "spell" and self.db.profile.rightclick ~= "macro" end,
			  		order=7,
					},
					s1 = {
						type = "header",
						name = " ",
						order = 8,
					},
					partytargets = {
						type='toggle',
						name=L["PartyTargets"],
						desc=L["PartyTargets"],
						get=function() return self.db.profile.partytargets end,
						set=function(v) self.db.profile.partytargets = v 
			  			self:UpdateFrame()
			  		end,
						order=9,
					},
					partyheight = {
						type="range",
						name=L["PartyTargetsHeight"],
						desc=L["PartyTargetsHeight"],
						get=function() return self.db.profile.partytargetheight end,
			  		set=function(v) self.db.profile.partytargetheight = v 
			  			self:UpdateFrame()
			  		end,
						min=2,
						max=20,
						step=1,
						order=10,
						disabled = function() return not self.db.profile.partytargets end,
			  	},
					mana = {
						type='toggle',
						name=L["Mana"],
						desc=L["Mana"],
						get=function() return self.db.profile.mana end,
						set=function(v) self.db.profile.mana = v 
			  			self:UpdateFrame()
			  		end,
						order=11,
					},
					manaheight = {
						type="range",
						name=L["ManaHeight"],
						desc=L["ManaHeight"],
						get=function() return self.db.profile.manaheight end,
			  		set=function(v) self.db.profile.manaheight = v 
			  			self:UpdateFrame()
			  		end,
						min=2,
						max=20,
						step=1,
						order=12,
						disabled = function() return not self.db.profile.mana end,
			  	},
			  	s2 = {
						type = "header",
						name = " ",
						order = 13,
					},
					highlighttarget = {
						type='toggle',
						name=L["HighlightTarget"],
						desc=L["HighlightTarget"],
						get=function() return self.db.profile.highlighttar end,
						set=function(v) self.db.profile.highlighttar = v 
			  			self:UpdateFrame()
			  		end,
						order=14,
					},
					highlight = {
			  		type = "text",
						name = L["Highlight"],
				    desc = L["Highlight"],
				    get = function() return self.db.profile.highlight end,
				    set = function(v) self.db.profile.highlight = v 
				    	self:UpdateFrame()
				    end,
				    validate = {
				    	solid = L["HighlightSolid"],
					    tooltip = L["HighlightTooltip"],
						},
						order = 15,
					},
					s3 = {
						type = "header",
						name = " ",
						order = 16,
					},
					lowhealth = {
						type='toggle',
						name=L["LowHealth"],
						desc=L["LowHealthDesc"],
						get=function() return self.db.profile.lowhealth end,
						set=function(v) self.db.profile.lowhealth = v 
			  			self:UpdateFrame()
			  		end,
						order=17,
					},
					lowhealthsound = {
						type='toggle',
						name=L["LowHealthSound"],
						desc=L["LowHealthSound"],
						get=function() return self.db.profile.lowhealthsound end,
						set=function(v) self.db.profile.lowhealthsound = v 
			  			self:UpdateFrame()
			  		end,
						order=18,
						disabled = function() return not self.db.profile.lowhealth end,
					},
					announce = {
			  		type = "text",
						name = L["Announce"],
				    desc = L["Announce"],
				    get = function() return self.db.profile.announce end,
				    set = function(v) self.db.profile.announce = v end,
				    validate = {
				    	chat = CHAT_LABEL,
					    raid = CHAT_MSG_RAID_WARNING,
							sct = "SCT",
							msbt = "MSBT",
							party = PARTY,
							disabled = "Disabled",
						},
						order=19,
					},
	  		},
	  	},
			ma = {
				type = 'text',
				name = L["MainAssist"],
				desc = L["MainAssist"],
				usage = "<name>",
				get=function() return self.db.profile.ma end,
	  		set=function(v) self.db.profile.ma = v 
	  			self:SetMainAssist(v)
	  			self:SendCommMessage("GROUP", "ReceiveMainAssist", v)
	  			self:Print(L["MainAssist"]..": "..v)
	  		end,
	  		order=4,
			},
			version = {
				name = L["VersionCheck"], type = 'execute',
		    desc = L["VersionCheck"],
		    order = 5,
		    func = function()
		    		self:Print(L["VersionCheckDesc"])
		        self:SendCommMessage("GROUP", "ReceiveVersion")
    		end
			},
			test = {
				type='toggle',
				name=L["Test"],
				desc=L["TestDesc"],
				get=function() return test end,
				set=function(v)
					test = v
					if v then 
						if self.frame==nil then
							self:MakeFrame()
						end
						self.frame:Show()
						self:Test()
						self:Print(L["HelpTip"])
					else 
						self:ResetList()
						self.frame:Hide()
					end 
				end,
				order=6,
			},
		}
	}
end

local function DewDropOptions()
  dewdrop:AddLine('text', 'Proximo Options','isTitle', true)
	dewdrop:InjectAceOptionsTable(Proximo, Proximo.options)
	dewdrop:FeedAceOptionsTable(Proximo.options)
end

function Proximo:DewDropMenu() 
	dewdrop:Open(Proximo.frame, 'children', DewDropOptions, 'cursorX', true, 'cursorY', true)
end