local opt = ToolsOptionCore
function ToolsActionBar_OptionInit()
	local v = {}
	local id
	local tab = ToolsActionBar
	local db = ToolsActionBarDB
	local grpselect = 1
	local dragframe = {}
	local grp = {}
	local total = 0
	
	local function ondragstop()
		dragframe[grpselect].stopfunc(db[grpselect].Anchor)
		db[grpselect].Xpoint = ToolsOptionCore.DragFrame.x 
		db[grpselect].Ypoint = ToolsOptionCore.DragFrame.y
		ToolsOptionCore:Refresh(id)
	end

	for i = 1, 14 do
		grp[i] = {}
		local drag, stopfunc = ToolsOptionCore.DragFrame:Create(getglobal("ToolsBarGroup"..i), UIParent, 1)
		dragframe[i] = drag
		dragframe[i].stopfunc = stopfunc
		dragframe[i]:SetScript("OnMouseDown", function()
			if arg1 == "LeftButton" then
				grpselect = i
				ToolsOptionCore:Refresh(id)
			end
		end)
		dragframe[i]:SetScript("OnDragStop", function()
			ondragstop()
		end)
		
		if i <= 12 then
			grp[i].text = "그룹 "..i
		elseif i == 13 then
			grp[i].text = "태세전환"
		elseif i == 14 then
			grp[i].text = "소환수"
		end
		opt:SetOnEnter(dragframe[i], grp[i].text.." 이동/선택")
		opt:SetOnLeave(dragframe[i])
	end
	local function getsum()
		if grpselect > 12 then return end
		local group, first, last = 0, 0, 0
		total = 0
		for i = 1, 12 do
			group = db[i].Col * db[i].Row
			total = total + group
			first = total - group + 1
			last = first + group - 1
			if group == 0 then
				db[i].ID = nil
			else
				db[i].ID = {Start = first, Last = last}
			end
		end
		db.Total = total
	end
	local function setbuttonsort(key, v)
		if grpselect > 12 then return end
		local prev = db[grpselect][key]
		db[grpselect][key] = v
		getsum()
		if db.Total > 60 then
			db[grpselect][key] = prev
			getsum()
			this.refresh = true
			this:SetValue(prev)
			this.refresh = nil
			return
		end
		for i = 1, 12 do
			tab:SetGroupButton(i)
			tab:SetBorderSize(i)
			tab:SetScale(i)
			tab:HideButton()
			tab:SetTexture(i)
		end
	end
	local function setpoint(v, point)
		local f = getglobal("ToolsBarGroup"..grpselect)
		local x = (v == "X" and point or db[grpselect].Xpoint)/f:GetEffectiveScale()
		local y = (v == "Y" and point or db[grpselect].Ypoint)/f:GetEffectiveScale()
		Tools_SetPoint(f, db[grpselect].Anchor, 0, db[grpselect].Anchor, x, y)
		ondragstop()
	end
	local function checkgrpselect()
		if grpselect > 12 then
			return true
		end
	end
	
	local class = {["도적"] = true, ["성기사"] = true, ["전사"] = true, ["드루이드"] = true}
	local unit = UnitClass("player")
	local function checkshapeshiftbarhide()
		if not class[unit] then return true end
		if grpselect ~= 13 then
			return true
		end
	end
	v.Header = {	text = "툴스 액션바",
			tooltip = "툴스 액션바 옵션을 설정합니다",
			onshow = function()
				for i = 1, 14 do
					dragframe[i]:Show()
				end
			end,
			onhide = function(v)
				for i = 1, 14 do
					dragframe[i]:Hide()
				end
				if not (UnitIsVisible("pet")) then
					PetActionBarFrame:Hide()
				end
			end
		}
	v.option ={
		{	type = "Title",
			text = "설정그룹 선택"
		},
		{	type = "Dropdown",
			text = "그룹 선택",
			get = function() return grpselect end,
			set = function(v) grpselect = v;opt:Refresh(id) end,
			values = grp,
			tooltip = "변경할 그룹을 선택합니다"
		},
		{	type = "Title",
			text = "버튼의 설정"
		},
		{	type = "CheckButton",
			text = "버튼 확장 방향",
			disable = checkgrpselect,
			get = function() return db[grpselect].Flip end,
			set = function(v)
				db[grpselect].Flip = v
				tab:SetGroupButton(grpselect)
			end,
			tooltip = "체크시 아래에서 위로 설정합니다",
		},
		{	type = "Slider",
			text = "가로 버튼 수",
			disable = checkgrpselect,
			get = function() return db[grpselect].Col end,
			set = function(v) setbuttonsort("Col", v) tab:SetBorderShow(grpselect) end,
			minmax = {0,36},
			step = 1,
			tooltip = "가로로 나열될 버튼의 갯수를 정합니다",
		},
		{	type = "Slider",
			text = "세로 버튼 수",
			disable = checkgrpselect,
			get = function() return db[grpselect].Row end,
			set = function(v) setbuttonsort("Row", v) end,
			minmax = {1,36},
			step = 1,
			tooltip = "세로로 나열될 버튼의 줄수를 설정합니다",
		},
		{	type = "Slider",
			text = "크기",
			get = function() return db[grpselect].Scale end,
			set = function(v) db[grpselect].Scale = v; tab:SetScale(grpselect); tab:UpdateFramePoint(grpselect) end,
			minmax = {0.4,2},
			step = 0.01,
			tooltip = "버튼의 스케일을 변경합니다",
		},
		{	type = "Slider",
			text = "버튼간의 간격",
			get = function() return db[grpselect].GapButton end,
			set = function(v)
				db[grpselect].GapButton = v
				tab:SetGroupButton(grpselect)
				tab:SetBorderSize(grpselect)
			end,
			minmax = {0,50},
			step = 1,
			tooltip = "버튼과 버튼과의 간격을 설정합니다",
		},
		{	type = "CheckButton",
			text = "태세전환바 숨김",
			disable = checkshapeshiftbarhide,
			get = function() return db.HideShapeshiftBar end,
			set = function(v)
				db.HideShapeshiftBar = v
				tab:SetShapeshiftBar()
			end,
			tooltip = "태세전환바를 화면에서 숨깁니다",
		},
		{	type = "CheckButton",
			text = "소환수바 간단히 표시",
			disable = function()
				if grpselect ~= 14 then
					return true
				end
			end,
			get = function() return db.PetCompactMode end,
			set = function(v)
				db.PetCompactMode = v
				db[grpselect].Col = v and 7 or 10
				tab:SetGroupButton(grpselect)
				tab:SetBorderSize(grpselect)
				opt:Refresh(id)
			end,
			tooltip = "공격/따르기/대기 버튼을 숨기거나 표시합니다",
		},
		{	type = "Title",
			text = "위치 설정"
		},
		{	type = "RadioButton",
			col = 3,
			text = "앵커(기준점) 위치 설정",
			get = function() 
				if db[grpselect].Anchor == 1 then
					return 7
				elseif db[grpselect].Anchor == 2 then
					return 8
				elseif db[grpselect].Anchor == 3 then
					return 9
				elseif db[grpselect].Anchor == 7 then
					return 1
				elseif db[grpselect].Anchor == 8 then
					return 2
				elseif db[grpselect].Anchor == 9 then
					return 3
				else
					return db[grpselect].Anchor
				end
			end,
			set = function(v)
				if v == 1 then
					db[grpselect].Anchor = 7
				elseif v == 2 then
					db[grpselect].Anchor = 8
				elseif v == 3 then
					db[grpselect].Anchor = 9
				elseif v == 7 then
					db[grpselect].Anchor = 1
				elseif v == 8 then
					db[grpselect].Anchor = 2
				elseif v == 9 then
					db[grpselect].Anchor = 3
				else
					db[grpselect].Anchor = v
				end
				Tools_SetPoint("ToolsBarGroup"..grpselect, db[grpselect].Anchor, 0, db[grpselect].Anchor, 0, 0)
				ondragstop()
			end,
			values = {	"상단 좌측", "상단 중앙", "상단 우측",
					"좌측", "중앙", "우측",
					"하단 좌측", "하단 중앙", "하단 우측",
				},
			tooltip = "앵커(기준점) 위치를 설정합니다"
		},
		{	type = "EditBox",
			text = "X 위치 직접입력",
			get = function() return tonumber(format("%0.0f",db[grpselect].Xpoint)) end,
			set = function(v) setpoint("X", tonumber(v)) end,
			tooltip = "X위치를 직접 설정합니다",
		},
		{	type = "EditBox",
			text = "Y 위치 직접입력",
			get = function() return tonumber(format("%0.0f",db[grpselect].Ypoint)) end,
			set = function(v) setpoint("Y", tonumber(v)) end,
			tooltip = "Y위치를 직접 설정합니다",
		},
		{	type = "Title",
			text = "비주얼 설정(테두리 모양 등등)"
		},
		{	type = "CheckButton",
			text = "배경 보임/숨김",
			get = function() return db[grpselect].Show end,
			set = function(v)
				db[grpselect].Show = v
				tab:SetBorderShow(grpselect)
			end,
			tooltip = "배경을 보임/숨김 토글합니다",
		},
		{	type = "Dropdown",
			text = "배경 테두리 설정",
			get = function() return db[grpselect].Border end,
			set = function(v)
				db[grpselect].Border = v
				tab:SetBorder(grpselect)
			end,
			values = {	{text = "기본형"},
					{text = "사각형"},
					{text = "클래식형"}
				},
			tooltip = "버튼 테두리를 설정합니다"
		},
		{	type = "ColorPicker",
			text = "배경의 색상설정",
			default = {0,0,0,0.5},
			hasOpacity = true,
			get = function() return db[grpselect].ColorBackdrop end,
			set = function(c)
				db[grpselect].ColorBackdrop ={c.r,c.g,c.b,c.a}
				tab:SetBorderColor(grpselect)
			end,
			tooltip = "배경(보더)의 배경색상을 설정합니다"
		},
		{	type = "ColorPicker",
			text = "배경의 테두리 색상설정",
			default = {1,1,1,1},
			hasOpacity = true,
			get = function() return db[grpselect].ColorBorder end,
			set = function(c)
				db[grpselect].ColorBorder ={c.r,c.g,c.b,c.a}
				tab:SetBorderColor(grpselect)
			end,
			tooltip = "배경(보더)의 테두리색상을 설정합니다"
		},
		{	type = "Slider",
			text = "테두리의 두께",
			get = function() return db[grpselect].Thickness end,
			set = function(v)
				db[grpselect].Thickness = v
				tab:SetBorder(grpselect)
				tab:SetBorderColor(grpselect)
			end,
			minmax = {1,50},
			step = 1,
			tooltip = "테두리의 두께를 설정합니다",
		},
		{	type = "Slider",
			text = "배경과 버튼간의 간격",
			get = function() return db[grpselect].GapBorder end,
			set = function(v)
				db[grpselect].GapBorder = v
				tab:SetGroupButton(grpselect)
				tab:SetBorderSize(grpselect)
			end,
			minmax = {0,50},
			step = 1,
			tooltip = "버튼과 배경과의 간격을 설정합니다",
		},
		ToolsOptionCore.Get:Texture(
			{	get = function() return db[grpselect].Overlay end,
				set = function(v)
					db[grpselect].Overlay = v
					tab:SetTexture(grpselect)
				end	},
			{	get = function() return db[grpselect].BT3 end,
				set = function(v)
					db[grpselect].BT3 = v
					tab:SetTexture(grpselect)
				end	})
	}
	id = opt:RegesterOption(v)
	ToolsOptionCore["headerButton"..id].optionFrame:SetScript("OnUpdate", function()
		PetActionBarFrame:Show()
		for i = 1, 14 do
			if db[i].Col ~= 0 then
				dragframe[i]:Show()
			else
				dragframe[i]:Hide()
			end
		end
	end)
end
opt:Insert("ToolsActionBar")