local AceGUI = LibStub("AceGUI-3.0")
local refPoints = {"CENTER", "TOPLEFT", "LEFT", "BOTTOMLEFT", "BOTTOM", "BOTTOMRIGHT", "RIGHT", "TOPRIGHT", "TOP"}
local sativ = {
	UnitFrames = {
		player = {
			x = -350,
			y = 125,
			ref = "CENTER",
			scale = 1
		},
		target = {
			x = -250,
			y = 75,
			ref = "CENTER",
			scale = 1
		},
		focus = {
			x = -500,
			y = 125,
			ref = "CENTER",
			scale = 1
		},
		party = {
			x = 75,
			y = -150,
			ref = "TOPLEFT",
			scale = 1.2
		},
		classIcons = true,
		classColors = true
	},
	ActionBars = {
		modify = true,
		macroText = true,
		CastBar = {
			x = 0,
			y = 250,
			ref = "BOTTOM",
			scale = 1
		},
		rangeIndicator = true
	},
	chat = true
}
local reloadNeeded = false
local function PositionPartyFrames()
	local DB = PUI.db.profile.UnitFrames.party
	for i=1,4 do _G["PartyMemberFrame"..i]:ClearAllPoints() end
	PartyMemberFrame1:SetPoint(DB.ref,UIParent,DB.ref,DB.x,DB.y)
	PartyMemberFrame2:SetPoint(DB.ref,UIParent,DB.ref,DB.x,DB.y-70)
	PartyMemberFrame3:SetPoint(DB.ref,UIParent,DB.ref,DB.x,DB.y-140)
	PartyMemberFrame4:SetPoint(DB.ref,UIParent,DB.ref,DB.x,DB.y-210)
	for i=GetNumSubgroupMembers()+1,4 do _G["PartyMemberFrame"..i.."PetFrame"]:Show(); _G["PartyMemberFrame"..i]:Show(); end
	for i=1,4 do _G["PartyMemberFrame"..i]:SetScale(DB.scale) end
end
		-- PLAYER ------------------------------------------
local function playerDropdown(widget, event, key)
	local DB = PUI.db.profile.UnitFrames.player
	DB.ref = refPoints[key]
	PlayerFrame:ClearAllPoints()
	PlayerFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function playerXChange(widget, event, text)
	local DB = PUI.db.profile.UnitFrames.player
	DB.x = text
	PlayerFrame:ClearAllPoints()
	PlayerFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function playerYChange(widget, event, text)
	local DB = PUI.db.profile.UnitFrames.player
	DB.y = text
	PlayerFrame:ClearAllPoints()
	PlayerFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function playerSclChange(widget, event, newScale)
	local DB = PUI.db.profile.UnitFrames.player
	DB.scale = newScale
	PlayerFrame:SetScale(DB.scale)
end
		-- TARGET -------------------------------------------
local function targetDropdown(widget, event, key)
	local DB = PUI.db.profile.UnitFrames.target
	DB.ref = refPoints[key]
	TargetFrame:ClearAllPoints()
	TargetFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function targetXChange(widget, event, text)
	local DB = PUI.db.profile.UnitFrames.target
	DB.x = text
	TargetFrame:ClearAllPoints()
	TargetFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function targetYChange(widget, event, text)
	local DB = PUI.db.profile.UnitFrames.target
	DB.y = text
	TargetFrame:ClearAllPoints()
	TargetFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function targetSclChange(widget, event, newScale)
	local DB = PUI.db.profile.UnitFrames.target
	DB.scale = newScale
	TargetFrame:SetScale(DB.scale)
end
		-- FOCUS -------------------------------------------
local function focusDropdown(widget, event, key)
	local DB = PUI.db.profile.UnitFrames.focus
	DB.ref = refPoints[key]
	FocusFrame:ClearAllPoints()
	FocusFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function focusXChange(widget, event, text)
	local DB = PUI.db.profile.UnitFrames.focus
	DB.x = text
	FocusFrame:ClearAllPoints()
	FocusFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function focusYChange(widget, event, text)
	local DB = PUI.db.profile.UnitFrames.focus
	DB.y = text
	FocusFrame:ClearAllPoints()
	FocusFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function focusSclChange(widget, event, newScale)
	local DB = PUI.db.profile.UnitFrames.focus
	DB.scale = newScale
	FocusFrame:SetScale(DB.scale)
end
		-- PARTY -------------------------------------------
local function partyDropdown(widget, event, key)
	local DB = PUI.db.profile.UnitFrames.party
	DB.ref = refPoints[key]
	PositionPartyFrames()
end
local function partyXChange(widget, event, text)
	local DB = PUI.db.profile.UnitFrames.party
	DB.x = text
	PositionPartyFrames()
end
local function partyYChange(widget, event, text)
	local DB = PUI.db.profile.UnitFrames.party
	DB.y = text
	PositionPartyFrames()
end
local function partySclChange(widget, event, newScale)
	local DB = PUI.db.profile.UnitFrames.party
	DB.scale = newScale
	PositionPartyFrames()
end
	
		-- CAST BAR ----------------------------------------
local function castDropdown(widget, event, key)
	local DB = PUI.db.profile.ActionBars.CastBar
	DB.ref = refPoints[key]
	CastingBarFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function castXChange(widget, event, text)
	local DB = PUI.db.profile.ActionBars.CastBar
	DB.x = text
	CastingBarFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function castYChange(widget, event, text)
	local DB = PUI.db.profile.ActionBars.CastBar
	DB.y = text
	CastingBarFrame:SetPoint(DB.ref, UIParent, DB.ref, DB.x, DB.y)
end
local function castSclChange(widget, event, newScale)
	local DB = PUI.db.profile.ActionBars.CastBar
	DB.scale = newScale
	CastingBarFrame:SetScale(DB.scale)
end

function PUI_OpenConfig(self, curProfile)
	PositionPartyFrames()
	local backFrame = AceGUI:Create("Frame")
	backFrame:SetTitle("PowerUI Settings")
	backFrame:SetStatusText(curProfile)
	backFrame:SetCallback("OnClose", function(widget)
		AceGUI:Release(widget)
		if reloadNeeded then
			ReloadUI();
		else
			for i=GetNumSubgroupMembers()+1,4 do
				_G["PartyMemberFrame"..i.."PetFrame"]:Hide();
				_G["PartyMemberFrame"..i]:Hide();
			end
		end
	end)
	backFrame:SetLayout("Flow")
	backFrame:SetWidth(670)
	
	local scrollcontainer = AceGUI:Create("SimpleGroup")
	scrollcontainer:SetFullWidth(true)
	scrollcontainer:SetFullHeight(true) -- probably?
	scrollcontainer:SetLayout("Fill") -- important!

	backFrame:AddChild(scrollcontainer)
	local frame = AceGUI:Create("ScrollFrame")
	frame:SetLayout("Flow")
	scrollcontainer:AddChild(frame)
	
			-- SATIV BUTTON
	local sativPic = AceGUI:Create("Icon")
	sativPic:SetImage("Interface\\AddOns\\PowerInterface\\Media\\sativ.tga")
	sativPic:SetImageSize(60, 60)
	frame:AddChild(sativPic)
	local sativButtonLabel = AceGUI:Create("Label")
	sativButtonLabel:SetText("Do you watch Sativ's stream or youtube videos? Want his layout?")
	sativButtonLabel:SetWidth(335)
	frame:AddChild(sativButtonLabel)
	local sativButton = AceGUI:Create("Button")
	sativButton:SetText("Use Sativ Layout")
	sativButton:SetWidth(140)
	sativButton:SetCallback("OnClick", function(widget)
		local function initDB(db, defaults)
			for k, v in pairs(defaults) do
				if type(v) == "table" then
					db[k] = initDB(db[k], v)
				else
					db[k] = v
				end
			end
			return db
		end
		local DB = initDB(self.db.profile, sativ);
		PUI:RefreshConfig();
		--PUI_OpenConfig(self, curProfile);
	end)	
	frame:AddChild(sativButton)
	
			-- PLAYERFRAME
	local playerGroup = AceGUI:Create("InlineGroup")
	playerGroup:SetTitle("Player Frame")
	playerGroup:SetLayout("Flow")
	playerGroup:SetWidth(600)
	frame:AddChild(playerGroup)
	local playerRef = AceGUI:Create("Dropdown")
	playerRef:SetLabel("      Anchor Point")
	playerRef:SetList(refPoints)
	playerRef:SetText(self.db.profile.UnitFrames.player.ref)
	playerRef:SetWidth(130)
	playerRef:SetCallback("OnValueChanged", playerDropdown)
	playerGroup:AddChild(playerRef)
	local playerScl = AceGUI:Create("Slider")
	playerScl:SetLabel("Scale")
	playerScl:SetSliderValues(.5, 2, .1)
	playerScl:SetValue(self.db.profile.UnitFrames.player.scale)
	playerScl:SetWidth(130)
	playerScl:SetCallback("OnMouseUp", playerSclChange)
	playerGroup:AddChild(playerScl)
	local playerX = AceGUI:Create("EditBox")
	playerX:SetLabel("      Horizontal Offset")
	playerX:SetWidth(130)
	playerX:SetText(self.db.profile.UnitFrames.player.x)
	playerX:SetCallback("OnEnterPressed", playerXChange)
	local playerY = AceGUI:Create("EditBox")
	playerY:SetLabel("      Vertical Offset")
	playerY:SetWidth(130)
	playerY:SetText(self.db.profile.UnitFrames.player.y)
	playerY:SetCallback("OnEnterPressed", playerYChange)
	playerGroup:AddChild(playerX)
	playerGroup:AddChild(playerY)
	
	
			-- TARGETFRAME
	local targetGroup = AceGUI:Create("InlineGroup")
	targetGroup:SetTitle("Target Frame")
	targetGroup:SetLayout("Flow")
	targetGroup:SetWidth(600)
	frame:AddChild(targetGroup)
	local targetRef = AceGUI:Create("Dropdown")
	targetRef:SetLabel("      Anchor Point")
	targetRef:SetList(refPoints)
	targetRef:SetText(self.db.profile.UnitFrames.target.ref)
	targetRef:SetWidth(130)
	targetRef:SetCallback("OnValueChanged", targetDropdown)
	targetGroup:AddChild(targetRef)
	local targetScl = AceGUI:Create("Slider")
	targetScl:SetLabel("Scale")
	targetScl:SetSliderValues(.5, 2, .1)
	targetScl:SetValue(self.db.profile.UnitFrames.target.scale)
	targetScl:SetWidth(130)
	targetScl:SetCallback("OnMouseUp", targetSclChange)
	targetGroup:AddChild(targetScl)
	local targetX = AceGUI:Create("EditBox")
	targetX:SetLabel("      Horizontal Offset")
	targetX:SetWidth(130)
	targetX:SetText(self.db.profile.UnitFrames.target.x)
	targetX:SetCallback("OnEnterPressed", targetXChange)
	local targetY = AceGUI:Create("EditBox")
	targetY:SetLabel("      Vertical Offset")
	targetY:SetWidth(130)
	targetY:SetText(self.db.profile.UnitFrames.target.y)
	targetY:SetCallback("OnEnterPressed", targetYChange)
	targetGroup:AddChild(targetX)
	targetGroup:AddChild(targetY)
	
	
			-- FOCUSFRAME
	local focusGroup = AceGUI:Create("InlineGroup")
	focusGroup:SetTitle("Focus Frame")
	focusGroup:SetLayout("Flow")
	focusGroup:SetWidth(600)
	frame:AddChild(focusGroup)
	local focusRef = AceGUI:Create("Dropdown")
	focusRef:SetLabel("      Anchor Point")
	focusRef:SetList(refPoints)
	focusRef:SetText(self.db.profile.UnitFrames.focus.ref)
	focusRef:SetWidth(130)
	focusRef:SetCallback("OnValueChanged", focusDropdown)
	focusGroup:AddChild(focusRef)
	local focusScl = AceGUI:Create("Slider")
	focusScl:SetLabel("Scale")
	focusScl:SetSliderValues(.5, 2, .1)
	focusScl:SetValue(self.db.profile.UnitFrames.focus.scale)
	focusScl:SetWidth(130)
	focusScl:SetCallback("OnMouseUp", focusSclChange)
	focusGroup:AddChild(focusScl)
	local focusX = AceGUI:Create("EditBox")
	focusX:SetLabel("      Horizontal Offset")
	focusX:SetWidth(130)
	focusX:SetText(self.db.profile.UnitFrames.focus.x)
	focusX:SetCallback("OnEnterPressed", focusXChange)
	local focusY = AceGUI:Create("EditBox")
	focusY:SetLabel("      Vertical Offset")
	focusY:SetWidth(130)
	focusY:SetText(self.db.profile.UnitFrames.focus.y)
	focusY:SetCallback("OnEnterPressed", focusYChange)
	focusGroup:AddChild(focusX)
	focusGroup:AddChild(focusY)
	
			-- PARTYFRAME
	local partyGroup = AceGUI:Create("InlineGroup")
	partyGroup:SetTitle("Party Frames")
	partyGroup:SetLayout("Flow")
	partyGroup:SetWidth(600)
	frame:AddChild(partyGroup)
	local partyRef = AceGUI:Create("Dropdown")
	partyRef:SetLabel("      Anchor Point")
	partyRef:SetList(refPoints)
	partyRef:SetText(self.db.profile.UnitFrames.party.ref)
	partyRef:SetWidth(130)
	partyRef:SetCallback("OnValueChanged", partyDropdown)
	partyGroup:AddChild(partyRef)
	local partyScl = AceGUI:Create("Slider")
	partyScl:SetLabel("Scale")
	partyScl:SetSliderValues(.5, 2, .1)
	partyScl:SetValue(self.db.profile.UnitFrames.party.scale)
	partyScl:SetWidth(130)
	partyScl:SetCallback("OnMouseUp", partySclChange)
	partyGroup:AddChild(partyScl)
	local partyX = AceGUI:Create("EditBox")
	partyX:SetLabel("      Horizontal Offset")
	partyX:SetWidth(130)
	partyX:SetText(self.db.profile.UnitFrames.party.x)
	partyX:SetCallback("OnEnterPressed", partyXChange)
	local partyY = AceGUI:Create("EditBox")
	partyY:SetLabel("      Vertical Offset")
	partyY:SetWidth(130)
	partyY:SetText(self.db.profile.UnitFrames.party.y)
	partyY:SetCallback("OnEnterPressed", partyYChange)
	partyGroup:AddChild(partyX)
	partyGroup:AddChild(partyY)
	
			-- CAST BAR	------------------------------------
	local castGroup = AceGUI:Create("InlineGroup")
	castGroup:SetTitle("Cast Bar")
	castGroup:SetLayout("Flow")
	castGroup:SetWidth(600)
	frame:AddChild(castGroup)
	local castRef = AceGUI:Create("Dropdown")
	castRef:SetLabel("      Anchor Point")
	castRef:SetList(refPoints)
	castRef:SetText(self.db.profile.ActionBars.CastBar.ref)
	castRef:SetWidth(130)
	castRef:SetCallback("OnValueChanged", castDropdown)
	castGroup:AddChild(castRef)
	local castScl = AceGUI:Create("Slider")
	castScl:SetLabel("Scale")
	castScl:SetSliderValues(.5, 2, .1)
	castScl:SetValue(self.db.profile.ActionBars.CastBar.scale)
	castScl:SetWidth(130)
	castScl:SetCallback("OnMouseUp", castSclChange)
	castGroup:AddChild(castScl)
	local castX = AceGUI:Create("EditBox")
	castX:SetLabel("      Horizontal Offset")
	castX:SetWidth(130)
	castX:SetText(self.db.profile.ActionBars.CastBar.x)
	castX:SetCallback("OnEnterPressed", castXChange)
	local castY = AceGUI:Create("EditBox")
	castY:SetLabel("      Vertical Offset")
	castY:SetWidth(130)
	castY:SetText(self.db.profile.ActionBars.CastBar.y)
	castY:SetCallback("OnEnterPressed", castYChange)
	castGroup:AddChild(castX)
	castGroup:AddChild(castY)
	
	
			-- EXTRAS	------------------------------------
	local toggleClassIcons = AceGUI:Create("CheckBox")
	if self.db.profile.UnitFrames.classIcons then
		toggleClassIcons:SetValue(true)
	else
		toggleClassIcons:SetValue(false)
	end
	toggleClassIcons:SetLabel("Class Icons")
	toggleClassIcons:SetDescription("Enable // Disable class icons on unit frames (target, focus, etc).")
	toggleClassIcons:SetCallback("OnValueChanged", function(widget, event, newVal)
		PUI.db.profile.UnitFrames.classIcons = newVal
		reloadNeeded = true
	end)
	frame:AddChild(toggleClassIcons)
	--------------------------------------------------------
	local toggleClassColor = AceGUI:Create("CheckBox")
	if self.db.profile.UnitFrames.classIcons then
		toggleClassColor:SetValue(true)
	else
		toggleClassColor:SetValue(false)
	end
	toggleClassColor:SetLabel("Class Colors")
	toggleClassColor:SetDescription("Enable // Disable class colored name bars on unit frames.")
	toggleClassColor:SetCallback("OnValueChanged", function(widget, event, newVal)
		PUI.db.profile.UnitFrames.classColors = newVal
		reloadNeeded = true
	end)
	frame:AddChild(toggleClassColor)
	--------------------------------------------------------
	local toggleActionBars = AceGUI:Create("CheckBox")
	toggleActionBars:SetLabel("Modify Action Bars")
	if self.db.profile.ActionBars.modify then
		toggleActionBars:SetValue(true)
	else
		toggleActionBars:SetValue(false)
	end
	toggleActionBars:SetDescription("Enable // Disable the modification & replacement of Action Bars (disable if using Bartender or similar)")
	frame:AddChild(toggleActionBars)
	--------------------------------------------------------
	local toggleMacro = AceGUI:Create("CheckBox")
	local isDisabled = false
	if toggleActionBars:GetValue() == false then
		toggleMacro:SetDisabled(true)
		isDisabled = true
	else
		toggleMacro:SetDisabled(false)
		isDisabled = false
	end
	toggleMacro:SetLabel("Show Macro Text")
	if self.db.profile.ActionBars.macroText then
		toggleMacro:SetValue(true)
	else
		toggleMacro:SetValue(false)
	end
	toggleMacro:SetDescription("Enable // Disable macro text on hotkeys. Requires Action Bar modification to be enabled.")
	toggleMacro:SetCallback("OnValueChanged", function(widget, event, newVal)
		if isDisabled == false then
			PUI.db.profile.ActionBars.macroText = newVal
			reloadNeeded = true
		end
	end)
	frame:AddChild(toggleMacro)
	-----------------------------------------------
	local toggleRange = AceGUI:Create("CheckBox")
	local isDisabled2 = false
	if toggleActionBars:GetValue() == false then
		toggleRange:SetDisabled(true)
		isDisabled2 = true
	else
		toggleRange:SetDisabled(false)
		isDisabled2 = false
	end
	toggleRange:SetLabel("Full-icon Out of Range")
	if self.db.profile.ActionBars.rangeIndicator then
		toggleRange:SetValue(true)
	else
		toggleRange:SetValue(false)
	end
	toggleRange:SetDescription("Enable // Disable robust OOR indication. Requires Action Bar modification.")
	toggleRange:SetCallback("OnValueChanged", function(widget, event, newVal)
		if isDisabled2 == false then
			PUI.db.profile.ActionBars.rangeIndicator = newVal
			reloadNeeded = true
		end
	end)
	frame:AddChild(toggleRange)
	
	local function onABUpdate(widget, event, newVal) -- Fade in/out dependants of AB modification
		if toggleActionBars:GetValue() == false then
			toggleMacro:SetDisabled(true)
			toggleRange:SetDisabled(true)
			isDisabled = true
		else
			toggleRange:SetDisabled(false)
			toggleMacro:SetDisabled(false)
			isDisabled = false
		end
		PUI.db.profile.ActionBars.modify = newVal
		reloadNeeded = true
	end
	toggleActionBars:SetCallback("OnValueChanged", onABUpdate)
	
	local toggleChat = AceGUI:Create("CheckBox")
	toggleChat:SetLabel("Modify Chat")
	if self.db.profile.chat then
		toggleChat:SetValue(true)
	else
		toggleChat:SetValue(false)
	end
	toggleChat:SetDescription("Locks chat font to 15px. Adds a black outline for text")
	toggleChat:SetCallback("OnValueChanged", function(widget, event, newVal)
		PUI.db.profile.chat = newVal
		reloadNeeded = true
	end)
	frame:AddChild(toggleChat)
end