--------------------------------------------
--
-- SMART - CONFIG UI
--
--------------------------------------------
SCONFIG_ENABLE = true

BINDING_HEADER_SCONFIG_HEADER = "스마트유닛프레임 설정"
BINDING_NAME_SCONFIG_BINDING_OPEN	= "설정 열기/닫기"
BINDING_NAME_SCONFIG_BINDING_RESET = "설정 초기화"
BINDING_NAME_SCONFIG_BINDING_RESETPOSITION = "유닛 위치초기화"

SCONFIG_PARTY = {}
SCONFIG_PLAYER = {}
SCONFIG_TARGET = {}
SCONFIG_TOT = {}
SCONFIG_BUFF = {}
SCONFIG_DEBUFF = {}
SCONFIG_FOCUS = {}

function SmartConfigUI_LoadFuBar()
	if (not (AceLibrary and AceLibrary:HasInstance("AceAddon-2.0") and AceLibrary:HasInstance("FuBarPlugin-2.0"))) then 
		return false
	end

	SUFFu = AceLibrary("AceAddon-2.0"):new("FuBarPlugin-2.0")

	SUFFu.hasIcon = "Interface\\Addons\\SmartConfigUI\\icon.tga"
	SUFFu.name = "SUF"
	SUFFu.description = "SmartUnitFrame 설정"
	SUFFu.category = "UnitFrame"
	SUFFu.hasNoText = true
	SUFFu.defaultPosition = "RIGHT"

	function SUFFu:OnEnable()
		SmartConfigUIStartFrame:Hide()
	end

	function SUFFu:OnClick(button)
		SmartConfigUI_OpenConfig()
	end

	return true
end

if IsAddOnLoaded("FuBar") then
	SmartConfigUI_LoadFuBar()
end

function SmartConfigUI_ResetConfig()
	HIDE_PARTY_INTERFACE = "0"

	-- SmartUnitFrame Config Reset
	SCONFIG_PARTY = {statusbartext=true,percent=false,loss=true,losstopercent=false,level=true,color=true,namewithserver=true,bufffilter=false,debufffilter=false,groupicon=true,movable=false,movablegroup=false,castingbar=false,target=false}
	SCONFIG_PLAYER = {statusbartext=true,statusbarmana=true,statusbarloss=false,percent=true,percenttoloss=false,percentmana=true,elite=false,rare=false,petstatusbartext=true,petstatusbarmana=true,groupicon=true,movable=false,healthwithpercent=false}
	SCONFIG_TARGET = {statusbartext=true,statusbarmana=true,statusbarloss=false,percent=true,percenttoloss=false,percentmana=true,indicator=false,ecastbarfixed=false,infomation=true,groupinfo=true,classinfo=true,aggroinfo=true,aggrosound=false,buffborder=false,groupicon=true,movable=false,healthwithpercent=false}
	SCONFIG_TOT = {enable=true,statusbartext=true,loss=true,losstopercent=false,percent=true,tott=false,WoWToTOnOff=true,raid=true,buff=true,debuff=true,buffswap=false,ChkBuff=16,MaxBuff=16,ChkDebuff=16,MaxDebuff=16}

	-- Focus Config Reset
	if SMART_FOCUS_ENABLE then
		SCONFIG_FOCUS = { ChkBuff=24, MaxBuff=24, ChkDebuff=40, MaxDebuff=40 }
	end

	-- Buff/Debuff Config Reset
	if SMART_BUFFDEBUFF_ENABLE then
		SCONFIG_BUFF = {party=24,pet=16,partypet=16}
		SCONFIG_DEBUFF = {party=6,pet=8,partypet=6}
	end

	-- Load to reset value
	DEFAULT_CHAT_FRAME:AddMessage("SmartConfigUI: 설정초기값 불러오기")

	if SmartConfigUIFrame:IsShown() then
		SmartConfigUI_LoadConfig()
	else
		RaidOptionsFrame_UpdatePartyFrames()
		SmartConfigUI_SaveConfig()
	end
end

function SmartConfigUI_SaveConfig()
	RaidOptionsFrame_UpdatePartyFrames()
	--SmartUnitFrame Config Save

	SMART_PARTY_Config = nil
	SMART_PLAYER_Config = nil
	SMART_TARGET_Config = nil
	SMART_TOT_Config = nil

	SMART_PARTY_Config = SCONFIG_PARTY
	SMART_PLAYER_Config = SCONFIG_PLAYER
	SMART_TARGET_Config = SCONFIG_TARGET
	SMART_TOT_Config = SCONFIG_TOT

	if SMART_FOCUS_ENABLE then
		SMART_FOCUS_Config = SCONFIG_FOCUS
	end

	SmartUnitFrame_MovableCheck()

	SmartParty_GroupCheck()

	if GetNumPartyMembers() then
		for id=1, GetNumPartyMembers() do
			SmartParty_UpdateMember(getglobal("SmartPartyFrame"..id))

			if UnitExists("partypet"..id) then
				SmartPartyPet_HealthUpdate(id)
			end

			-- Targets of party members
			PartyTargetFrame_SetUnitWatch(getglobal("PartyTargetFrame"..id), SMART_PARTY_Config.target)
			SmartParty_UpdateMember(getglobal("SmartPartyTargetFrame"..id))
		end
	end

	SmartPlayer_OnEvent("PLAYER_ENTERING_WORLD")

	if not PlayerFrame:IsUserPlaced() then
		SmartPlayer_ResetPosition()
	end

	SmartTarget_OnEvent("PLAYER_TARGET_CHANGED")

	if not TargetFrame:IsUserPlaced() then
		SmartTarget_ResetPosition()
	end

	if SMART_FOCUS_ENABLE then
		if UnitExists("focus") then
			SmartFocus_Update()
		end

		SmartFocus_BuffDebuffInitialize()

		if UnitExists("focus") then
			SmartFocus_DebuffButtonUpdate()
		end

		if SMART_FOCUSTOT_ENABLE then
			SmartFocusToT_BuffDebuffInitialize()
			SmartFocusToT_BuffDebuffLayout()
			SmartFocusToT_CheckWoWFocusToTOnOff()
			SmartFocusToTFrame.enable = SMART_TOT_Config.enable
			SmartFocusToT_RegisterUnitWatch(SmartFocusToTFrame, SmartFocusToTFrame.enable)
			SmartFocusToTTFrame.enable = SMART_TOT_Config.enable and SMART_TOT_Config.tott
			SmartFocusToT_RegisterUnitWatch(SmartFocusToTTFrame, SmartFocusToTTFrame.enable)

			if SMART_TOT_Config.enable then
				SmartFocusToT_Update(SmartFocusToTFrame)

				if SMART_TOT_Config.tott then
					SmartFocusToT_Update(SmartFocusToTTFrame)
				end
			end
		end
	end

	SmartToT_BuffDebuffInitialize()
	SmartToT_BuffDebuffLayout()
	SmartToT_CheckWoWToTOnOff()
	SmartToTFrame.enable = SMART_TOT_Config.enable
	SmartToT_RegisterUnitWatch(SmartToTFrame,SmartToTFrame.enable)
	SmartToTTFrame.enable = SMART_TOT_Config.enable and SMART_TOT_Config.tott
	SmartToT_RegisterUnitWatch(SmartToTTFrame,SmartToTTFrame.enable)

	if SMART_TOT_Config.enable then
		SmartToT_Update(SmartToTFrame)

		if SMART_TOT_Config.tott then
			SmartToT_Update(SmartToTTFrame)
		end
	end

	-- SmartGroupIcon Append
	if SMART_GROUPICON_ENABLE then
		SmartGroupIcon_OnEvent("PLAYER_ENTERING_WORLD")
	end

	-- SmartBuffDebuff Config Save/Append
	if SMART_BUFFDEBUFF_ENABLE then
		SMART_BUFF_ChkNum = SCONFIG_BUFF
		SMART_DEBUFF_ChkNum = SCONFIG_DEBUFF

		SmartBuffDebuff_Initialize()
	end

	DEFAULT_CHAT_FRAME:AddMessage("SUF: 선택된 모든 설정값 저장")

	if SCONFIG_PLAYER.movable or SCONFIG_TARGET.movable or SCONFIG_PARTY.movable then
		DEFAULT_CHAT_FRAME:AddMessage("SUF: 이름에 마우스 커서를 가져가 프레임을 이동하실 수 있습니다.")
	end
end

function SmartConfigUI_CheckConfig()
	-- Player Config Check
	if SmartConfigUIGroup1MovableCheckButton:GetChecked() then
		SCONFIG_PLAYER.movable = true
	else
		SCONFIG_PLAYER.movable = false
	end
	if SmartConfigUIGroup1StatusBarTextCheckButton:GetChecked() then
		SCONFIG_PLAYER.statusbartext = true
	else
		SCONFIG_PLAYER.statusbartext = false
	end
	if SmartConfigUIGroup1StatusBarHealthWithPercentCheckButton:GetChecked() then
		SCONFIG_PLAYER.healthwithpercent = true
	else
		SCONFIG_PLAYER.healthwithpercent = false
	end
	if SmartConfigUIGroup1StatusBarManaCheckButton:GetChecked() then
		SCONFIG_PLAYER.statusbarmana = true
	else
		SCONFIG_PLAYER.statusbarmana = false
	end
	if SmartConfigUIGroup1StatusBarLossCheckButton:GetChecked() then
		SCONFIG_PLAYER.statusbarloss = true
	else
		SCONFIG_PLAYER.statusbarloss = false
	end
	if SmartConfigUIGroup1PercentCheckButton:GetChecked() then
		SCONFIG_PLAYER.percent = true
	else
		SCONFIG_PLAYER.percent = false
	end
	if SmartConfigUIGroup1PercentToLossCheckButton:GetChecked() then
		SCONFIG_PLAYER.percenttoloss = true
	else
		SCONFIG_PLAYER.percenttoloss = false
	end
	if SmartConfigUIGroup1PercentManaCheckButton:GetChecked() then
		SCONFIG_PLAYER.percentmana = true
	else
		SCONFIG_PLAYER.percentmana = false
	end
	if SmartConfigUIGroup1EliteCheckButton:GetChecked() then
		SCONFIG_PLAYER.elite = true
	else
		SCONFIG_PLAYER.elite = false
	end
	if SmartConfigUIGroup1RareCheckButton:GetChecked() then
		SCONFIG_PLAYER.rare = true
	else
		SCONFIG_PLAYER.rare = false
	end
	if SmartConfigUIGroup1PetStatusBarTextCheckButton:GetChecked() then
		SCONFIG_PLAYER.petstatusbartext = true
	else
		SCONFIG_PLAYER.petstatusbartext = false
	end
	if SmartConfigUIGroup1PetStatusBarManaCheckButton:GetChecked() then
		SCONFIG_PLAYER.petstatusbarmana = true
	else
		SCONFIG_PLAYER.petstatusbarmana = false
	end
	if SmartConfigUIGroup1GroupIconCheckButton:GetChecked() then
		SCONFIG_PLAYER.groupicon = true
	else
		SCONFIG_PLAYER.groupicon = false
	end
	-- Target Config Check
	if SmartConfigUIGroup2MovableCheckButton:GetChecked() then
		SCONFIG_TARGET.movable = true
	else
		SCONFIG_TARGET.movable = false
	end
	if SmartConfigUIGroup2StatusBarTextCheckButton:GetChecked() then
		SCONFIG_TARGET.statusbartext = true
	else
		SCONFIG_TARGET.statusbartext = false
	end
	if SmartConfigUIGroup2StatusBarHealthWithPercentCheckButton:GetChecked() then
		SCONFIG_TARGET.healthwithpercent = true
	else
		SCONFIG_TARGET.healthwithpercent = false
	end
	if SmartConfigUIGroup2StatusBarManaCheckButton:GetChecked() then
		SCONFIG_TARGET.statusbarmana = true
	else
		SCONFIG_TARGET.statusbarmana = false
	end
	if SmartConfigUIGroup2StatusBarLossCheckButton:GetChecked() then
		SCONFIG_TARGET.statusbarloss = true
	else
		SCONFIG_TARGET.statusbarloss = false
	end
	if SmartConfigUIGroup2PercentCheckButton:GetChecked() then
		SCONFIG_TARGET.percent = true
	else
		SCONFIG_TARGET.percent = false
	end
	if SmartConfigUIGroup2PercentToLossCheckButton:GetChecked() then
		SCONFIG_TARGET.percenttoloss = true
	else
		SCONFIG_TARGET.percenttoloss = false
	end
	if SmartConfigUIGroup2PercentManaCheckButton:GetChecked() then
		SCONFIG_TARGET.percentmana = true
	else
		SCONFIG_TARGET.percentmana = false
	end
	if SmartConfigUIGroup2IndicatorCheckButton:GetChecked() then
		SCONFIG_TARGET.indicator = true
	else
		SCONFIG_TARGET.indicator = false
	end
	if SmartConfigUIGroup2ECastBarFixedCheckButton:GetChecked() then
		SCONFIG_TARGET.ecastbarfixed = true
	else
		SCONFIG_TARGET.ecastbarfixed = false
	end
	if SmartConfigUIGroup2InfoCheckButton:GetChecked() then
		SCONFIG_TARGET.infomation = true
	else
		SCONFIG_TARGET.infomation = false
	end
	if SmartConfigUIGroup2InfoGroupCheckButton:GetChecked() then
		SCONFIG_TARGET.groupinfo = true
	else
		SCONFIG_TARGET.groupinfo = false
	end
	if SmartConfigUIGroup2InfoClassCheckButton:GetChecked() then
		SCONFIG_TARGET.classinfo = true
	else
		SCONFIG_TARGET.classinfo = false
	end
	if SmartConfigUIGroup2InfoAggroCheckButton:GetChecked() then
		SCONFIG_TARGET.aggroinfo = true
	else
		SCONFIG_TARGET.aggroinfo = false
	end
	if SmartConfigUIGroup2InfoAggroSoundCheckButton:GetChecked() then
		SCONFIG_TARGET.aggrosound = true
	else
		SCONFIG_TARGET.aggrosound = false
	end
	if SmartConfigUIGroup2BuffBorderCheckButton:GetChecked() then
		SCONFIG_TARGET.buffborder = true
	else
		SCONFIG_TARGET.buffborder = false
	end
	if SmartConfigUIGroup2GroupIconCheckButton:GetChecked() then
		SCONFIG_TARGET.groupicon = true
	else
		SCONFIG_TARGET.groupicon = false
	end
	-- Party Config Check
	if SmartConfigUIGroup3MovableCheckButton:GetChecked() then
		SCONFIG_PARTY.movable = true
	else
		SCONFIG_PARTY.movable = false
	end
	if SmartConfigUIGroup3MovableGroupCheckButton:GetChecked() then
		SCONFIG_PARTY.movablegroup = true
	else
		SCONFIG_PARTY.movablegroup = false
	end
	if SmartConfigUIGroup3PartyHideInRaidCheckButton:GetChecked() then
		HIDE_PARTY_INTERFACE = "1"
	else
		HIDE_PARTY_INTERFACE = "0"
	end
	if SmartConfigUIGroup3StatusBarTextCheckButton:GetChecked() then
		SCONFIG_PARTY.statusbartext = true
	else
		SCONFIG_PARTY.statusbartext = false
	end
	if SmartConfigUIGroup3PercentCheckButton:GetChecked() then
		SCONFIG_PARTY.percent = true
	else
		SCONFIG_PARTY.percent = false
	end
	if SmartConfigUIGroup3HealthLossCheckButton:GetChecked() then
		SCONFIG_PARTY.loss = true
	else
		SCONFIG_PARTY.loss = false
	end
	if SmartConfigUIGroup3LossToPercentCheckButton:GetChecked() then
		SCONFIG_PARTY.losstopercent = true
	else
		SCONFIG_PARTY.losstopercent = false
	end
	if SmartConfigUIGroup3LevelCheckButton:GetChecked() then
		SCONFIG_PARTY.level = true
	else
		SCONFIG_PARTY.level = false
	end
	if SmartConfigUIGroup3ColorCheckButton:GetChecked() then
		SCONFIG_PARTY.color = true
	else
		SCONFIG_PARTY.color = false
	end
	if SmartConfigUIGroup3NameWithServerCheckButton:GetChecked() then
		SCONFIG_PARTY.namewithserver = true
	else
		SCONFIG_PARTY.namewithserver = false
	end
	if SmartConfigUIGroup3BuffFilterCheckButton:GetChecked() then
		SCONFIG_PARTY.bufffilter = true
	else
		SCONFIG_PARTY.bufffilter = false
	end
	if SmartConfigUIGroup3DebuffFilterCheckButton:GetChecked() then
		SCONFIG_PARTY.debufffilter = true
	else
		SCONFIG_PARTY.debufffilter = false
	end
	if SmartConfigUIGroup3CastingBarCheckButton:GetChecked() then
		SCONFIG_PARTY.castingbar = true
	else
		SCONFIG_PARTY.castingbar = false
	end
	if SmartConfigUIGroup3GroupIconCheckButton:GetChecked() then
		SCONFIG_PARTY.groupicon = true
	else
		SCONFIG_PARTY.groupicon = false
	end
	if SmartConfigUIGroup3PartyTargetCheckButton:GetChecked() then
		SCONFIG_PARTY.target = true
	else
		SCONFIG_PARTY.target = false
	end
	-- ToT&ToTT Config Check
	if SmartConfigUIGroup4EnableCheckButton:GetChecked() then
		SCONFIG_TOT.enable = true
	else
		SCONFIG_TOT.enable = false
	end
	if SmartConfigUIGroup4FrameFixedCheckButton:GetChecked() then
		SCONFIG_TOT.WoWToTOnOff = true
	else
		SCONFIG_TOT.WoWToTOnOff = false
	end
	if SmartConfigUIGroup4StatusCheckButton:GetChecked() then
		SCONFIG_TOT.statusbartext = true
	else
		SCONFIG_TOT.statusbartext = false
	end
	if SmartConfigUIGroup4PercentCheckButton:GetChecked() then
		SCONFIG_TOT.percent = true
	else
		SCONFIG_TOT.percent = false
	end
	if SmartConfigUIGroup4HealthLossCheckButton:GetChecked() then
		SCONFIG_TOT.loss = true
	else
		SCONFIG_TOT.loss = false
	end
	if SmartConfigUIGroup4LossToPercentCheckButton:GetChecked() then
		SCONFIG_TOT.losstopercent = true
	else
		SCONFIG_TOT.losstopercent = false
	end
	if SmartConfigUIGroup4ToTTCheckButton:GetChecked() then
		SCONFIG_TOT.tott = true
	else
		SCONFIG_TOT.tott = false
	end
	if SmartConfigUIGroup4RaidIconCheckButton:GetChecked() then
		SCONFIG_TOT.raid = true
	else
		SCONFIG_TOT.raid = false
	end
	if SmartConfigUIGroup4BuffCheckButton:GetChecked() then
		SCONFIG_TOT.buff = true
	else
		SCONFIG_TOT.buff = false
	end
	if SmartConfigUIGroup4DebuffCheckButton:GetChecked() then
		SCONFIG_TOT.debuff = true
	else
		SCONFIG_TOT.debuff = false
	end
	if SmartConfigUIGroup4BuffSwapCheckButton:GetChecked() then
		SCONFIG_TOT.buffswap = true
	else
		SCONFIG_TOT.buffswap = false
	end

	SCONFIG_TOT.ChkBuff = SmartConfigUIGroup4Slider1:GetValue()
	SCONFIG_TOT.ChkDebuff = SmartConfigUIGroup4Slider2:GetValue()

	if SMART_FOCUS_ENABLE then
		SCONFIG_FOCUS.ChkBuff = SmartBuffDebuffConfigUIFrameSlider9:GetValue()
		SCONFIG_FOCUS.ChkDebuff = SmartBuffDebuffConfigUIFrameSlider10:GetValue()
	end

	-- SmartBuffDebuff Config Check
	if SMART_BUFFDEBUFF_ENABLE then
		SCONFIG_BUFF.pet = SmartBuffDebuffConfigUIFrameSlider1:GetValue()
		SCONFIG_DEBUFF.pet = SmartBuffDebuffConfigUIFrameSlider2:GetValue()

		SCONFIG_BUFF.party = SmartBuffDebuffConfigUIFrameSlider5:GetValue()
		SCONFIG_DEBUFF.party = SmartBuffDebuffConfigUIFrameSlider6:GetValue()

		SCONFIG_BUFF.partypet = SmartBuffDebuffConfigUIFrameSlider7:GetValue()
		SCONFIG_DEBUFF.partypet = SmartBuffDebuffConfigUIFrameSlider8:GetValue()
	end

	SmartConfigUI_SaveConfig()
end

function SmartConfigUI_LoadConfig()
	-- Player Config Load
	SmartConfigUIGroup1MovableCheckButton:SetChecked(SCONFIG_PLAYER.movable)
	SmartConfigUIGroup1StatusBarTextCheckButton:SetChecked(SCONFIG_PLAYER.statusbartext)
	SmartConfigUIGroup1StatusBarHealthWithPercentCheckButton:SetChecked(SCONFIG_PLAYER.healthwithpercent)
	SmartConfigUIGroup1StatusBarManaCheckButton:SetChecked(SCONFIG_PLAYER.statusbarmana)
	SmartConfigUIGroup1StatusBarLossCheckButton:SetChecked(SCONFIG_PLAYER.statusbarloss)
	SmartConfigUIGroup1PercentCheckButton:SetChecked(SCONFIG_PLAYER.percent)
	SmartConfigUIGroup1PercentToLossCheckButton:SetChecked(SCONFIG_PLAYER.percenttoloss)
	SmartConfigUIGroup1PercentManaCheckButton:SetChecked(SCONFIG_PLAYER.percentmana)
	SmartConfigUIGroup1EliteCheckButton:SetChecked(SCONFIG_PLAYER.elite)
	SmartConfigUIGroup1RareCheckButton:SetChecked(SCONFIG_PLAYER.rare)
	SmartConfigUIGroup1PetStatusBarTextCheckButton:SetChecked(SCONFIG_PLAYER.petstatusbartext)
	SmartConfigUIGroup1PetStatusBarManaCheckButton:SetChecked(SCONFIG_PLAYER.petstatusbarmana)
	SmartConfigUIGroup1GroupIconCheckButton:SetChecked(SCONFIG_PLAYER.groupicon)
	-- Target Config Load
	SmartConfigUIGroup2MovableCheckButton:SetChecked(SCONFIG_TARGET.movable)
	SmartConfigUIGroup2StatusBarTextCheckButton:SetChecked(SCONFIG_TARGET.statusbartext)
	SmartConfigUIGroup2StatusBarHealthWithPercentCheckButton:SetChecked(SCONFIG_TARGET.healthwithpercent)
	SmartConfigUIGroup2StatusBarManaCheckButton:SetChecked(SCONFIG_TARGET.statusbarmana)
	SmartConfigUIGroup2StatusBarLossCheckButton:SetChecked(SCONFIG_TARGET.statusbarloss)
	SmartConfigUIGroup2PercentCheckButton:SetChecked(SCONFIG_TARGET.percent)
	SmartConfigUIGroup2PercentToLossCheckButton:SetChecked(SCONFIG_TARGET.percenttoloss)
	SmartConfigUIGroup2PercentManaCheckButton:SetChecked(SCONFIG_TARGET.percentmana)
	SmartConfigUIGroup2IndicatorCheckButton:SetChecked(SCONFIG_TARGET.indicator)
	SmartConfigUIGroup2ECastBarFixedCheckButton:SetChecked(SCONFIG_TARGET.ecastbarfixed)
	SmartConfigUIGroup2InfoCheckButton:SetChecked(SCONFIG_TARGET.infomation)
	SmartConfigUIGroup2InfoGroupCheckButton:SetChecked(SCONFIG_TARGET.groupinfo)
	SmartConfigUIGroup2InfoClassCheckButton:SetChecked(SCONFIG_TARGET.classinfo)
	SmartConfigUIGroup2InfoAggroCheckButton:SetChecked(SCONFIG_TARGET.aggroinfo)
	SmartConfigUIGroup2InfoAggroSoundCheckButton:SetChecked(SCONFIG_TARGET.aggrosound)
	SmartConfigUIGroup2BuffBorderCheckButton:SetChecked(SCONFIG_TARGET.buffborder)
	SmartConfigUIGroup2GroupIconCheckButton:SetChecked(SCONFIG_TARGET.groupicon)
	-- Party Config Load
	SmartConfigUIGroup3MovableCheckButton:SetChecked(SCONFIG_PARTY.movable)
	SmartConfigUIGroup3MovableGroupCheckButton:SetChecked(SCONFIG_PARTY.movablegroup)
	SmartConfigUIGroup3StatusBarTextCheckButton:SetChecked(SCONFIG_PARTY.statusbartext)
	SmartConfigUIGroup3PercentCheckButton:SetChecked(SCONFIG_PARTY.percent)
	SmartConfigUIGroup3HealthLossCheckButton:SetChecked(SCONFIG_PARTY.loss)
	SmartConfigUIGroup3LossToPercentCheckButton:SetChecked(SCONFIG_PARTY.losstopercent)
	SmartConfigUIGroup3LevelCheckButton:SetChecked(SCONFIG_PARTY.level)
	SmartConfigUIGroup3ColorCheckButton:SetChecked(SCONFIG_PARTY.color)
	SmartConfigUIGroup3NameWithServerCheckButton:SetChecked(SCONFIG_PARTY.namewithserver)
	if HIDE_PARTY_INTERFACE == "0" then
		SmartConfigUIGroup3PartyHideInRaidCheckButton:SetChecked(false)
	else
		SmartConfigUIGroup3PartyHideInRaidCheckButton:SetChecked(true)
	end
	SmartConfigUIGroup3BuffFilterCheckButton:SetChecked(SCONFIG_PARTY.bufffilter)
	SmartConfigUIGroup3DebuffFilterCheckButton:SetChecked(SCONFIG_PARTY.debufffilter)
	SmartConfigUIGroup3CastingBarCheckButton:SetChecked(SCONFIG_PARTY.castingbar)
	SmartConfigUIGroup3GroupIconCheckButton:SetChecked(SCONFIG_PARTY.groupicon)
	SmartConfigUIGroup3PartyTargetCheckButton:SetChecked(SCONFIG_PARTY.target)
	-- ToT&ToTT Config Load
	SmartConfigUIGroup4EnableCheckButton:SetChecked(SCONFIG_TOT.enable)
	SmartConfigUIGroup4FrameFixedCheckButton:SetChecked(SCONFIG_TOT.WoWToTOnOff)
	SmartConfigUIGroup4StatusCheckButton:SetChecked(SCONFIG_TOT.statusbartext)
	SmartConfigUIGroup4PercentCheckButton:SetChecked(SCONFIG_TOT.percent)
	SmartConfigUIGroup4HealthLossCheckButton:SetChecked(SCONFIG_TOT.loss)
	SmartConfigUIGroup4LossToPercentCheckButton:SetChecked(SCONFIG_TOT.losstopercent)
	SmartConfigUIGroup4RaidIconCheckButton:SetChecked(SCONFIG_TOT.raid)
	SmartConfigUIGroup4BuffCheckButton:SetChecked(SCONFIG_TOT.buff)
	SmartConfigUIGroup4DebuffCheckButton:SetChecked(SCONFIG_TOT.debuff)
	SmartConfigUIGroup4BuffSwapCheckButton:SetChecked(SCONFIG_TOT.buffswap)
	SmartConfigUIGroup4ToTTCheckButton:SetChecked(SCONFIG_TOT.tott)
	SmartConfigUIGroup4Slider1:SetValue(SCONFIG_TOT.ChkBuff)
	SmartConfigUIGroup4Slider2:SetValue(SCONFIG_TOT.ChkDebuff)

	if SMART_FOCUS_ENABLE then
		SmartBuffDebuffConfigUIFrameSlider9:SetValue(SCONFIG_FOCUS.ChkBuff)
		SmartBuffDebuffConfigUIFrameSlider10:SetValue(SCONFIG_FOCUS.ChkDebuff)
	end

	-- SmartBuffDebuff Config Load
	if SMART_BUFFDEBUFF_ENABLE then
		SmartBuffDebuffConfigUIFrameSlider1:SetValue(SCONFIG_BUFF.pet)
		SmartBuffDebuffConfigUIFrameSlider2:SetValue(SCONFIG_DEBUFF.pet)
		SmartBuffDebuffConfigUIFrameSlider5:SetValue(SCONFIG_BUFF.party)
		SmartBuffDebuffConfigUIFrameSlider6:SetValue(SCONFIG_DEBUFF.party)
		SmartBuffDebuffConfigUIFrameSlider7:SetValue(SCONFIG_BUFF.partypet)
		SmartBuffDebuffConfigUIFrameSlider8:SetValue(SCONFIG_DEBUFF.partypet)
	end
end

function SmartConfigUI_OpenConfig()
	if not SmartConfigUIFrame:IsShown() then
		SCONFIG_PARTY = SMART_PARTY_Config
		SCONFIG_PLAYER = SMART_PLAYER_Config
		SCONFIG_TARGET = SMART_TARGET_Config
		SCONFIG_TOT = SMART_TOT_Config

		SmartConfigUIGroup4Slider1:SetMinMaxValues(0, SMART_TOT_Config.MaxBuff)
		SmartConfigUIGroup4Slider1:SetValueStep(1)
		SmartConfigUIGroup4Slider1Text:SetText("대상의 대상 버프")

		SmartConfigUIGroup4Slider2:SetMinMaxValues(0, SMART_TOT_Config.MaxDebuff)
		SmartConfigUIGroup4Slider2:SetValueStep(1)
		SmartConfigUIGroup4Slider2Text:SetText("대상의 대상 디버프")
		if SMART_FOCUS_ENABLE then
			SCONFIG_FOCUS = SMART_FOCUS_Config

			SmartBuffDebuffConfigUIFrameSlider9:SetMinMaxValues(0, SMART_FOCUS_Config.MaxBuff)
			SmartBuffDebuffConfigUIFrameSlider9:SetValueStep(1)
			SmartBuffDebuffConfigUIFrameSlider9Text:SetText("주시대상의 버프")

			SmartBuffDebuffConfigUIFrameSlider10:SetMinMaxValues(0, SMART_FOCUS_Config.MaxDebuff)
			SmartBuffDebuffConfigUIFrameSlider10:SetValueStep(1)
			SmartBuffDebuffConfigUIFrameSlider10Text:SetText("주시대상의 디버프")
		end
		if SMART_BUFFDEBUFF_ENABLE then
			SCONFIG_BUFF = SMART_BUFF_ChkNum
			SCONFIG_DEBUFF = SMART_DEBUFF_ChkNum

			SmartBuffDebuffConfigUIFrameSlider5:SetMinMaxValues(0, SMART_BUFF_MaxNum.party)
			SmartBuffDebuffConfigUIFrameSlider5:SetValueStep(1)
			SmartBuffDebuffConfigUIFrameSlider5Text:SetText("파티 버프")
			SmartBuffDebuffConfigUIFrameSlider1:SetMinMaxValues(0, SMART_BUFF_MaxNum.pet)
			SmartBuffDebuffConfigUIFrameSlider1:SetValueStep(1)
			SmartBuffDebuffConfigUIFrameSlider1Text:SetText("소환수 버프")
			SmartBuffDebuffConfigUIFrameSlider7:SetMinMaxValues(0, SMART_BUFF_MaxNum.partypet)
			SmartBuffDebuffConfigUIFrameSlider7:SetValueStep(1)
			SmartBuffDebuffConfigUIFrameSlider7Text:SetText("파티소환수 버프")
			SmartBuffDebuffConfigUIFrameSlider6:SetMinMaxValues(0, SMART_DEBUFF_MaxNum.party)
			SmartBuffDebuffConfigUIFrameSlider6:SetValueStep(1)
			SmartBuffDebuffConfigUIFrameSlider6Text:SetText("파티 디버프")
			SmartBuffDebuffConfigUIFrameSlider2:SetMinMaxValues(0, SMART_DEBUFF_MaxNum.pet)
			SmartBuffDebuffConfigUIFrameSlider2:SetValueStep(1)
			SmartBuffDebuffConfigUIFrameSlider2Text:SetText("소환수 디버프")
			SmartBuffDebuffConfigUIFrameSlider8:SetMinMaxValues(0, SMART_DEBUFF_MaxNum.partypet)
			SmartBuffDebuffConfigUIFrameSlider8:SetValueStep(1)
			SmartBuffDebuffConfigUIFrameSlider8Text:SetText("파티소환수 디버프")

			for i=1, 8 do
				if getglobal("SmartBuffDebuffConfigUIFrameSlider"..i) then
					getglobal("SmartBuffDebuffConfigUIFrameSlider"..i):Show()
				end
			end
		else
			for i=1, 8 do
				if getglobal("SmartBuffDebuffConfigUIFrameSlider"..i) then
					getglobal("SmartBuffDebuffConfigUIFrameSlider"..i):Hide()
				end
			end
		end

		SmartConfigUI_LoadConfig()
		SmartConfigUIFrame:ClearAllPoints()
		SmartConfigUIFrame:SetPoint("CENTER", UIParent, "CENTER", 0, 50)
		SmartConfigUIFrame:SetWidth(750)

		local frameTitle = sUnitFrames_info.name .. " v" .. sUnitFrames_info.ver
		local groupHeight = 360
		local frameHeight = 620

		if SMART_GROUPICON_ENABLE then
			SmartConfigUIGroup1GroupIconCheckButton:Show()
			SmartConfigUIGroup2GroupIconCheckButton:Show()
			SmartConfigUIGroup3GroupIconCheckButton:Show()
			groupHeight = groupHeight + 20
			frameHeight = frameHeight + 20
		end

		if SMART_SKIN_ENABLE then
			SmartConfigUIGroupStatusStyle:Show()
			frameHeight = frameHeight + 40
			frameTitle = frameTitle .. " ("..SMART_SKIN_NAME.." Skin)"
		end

		SmartConfigUIFrame:SetHeight(frameHeight)
		SmartConfigUIFrameTitleText:SetText(frameTitle)
		SmartConfigUIGroup1TitleText:SetText(UnitName("player"))

		for i=1, 4 do
			getglobal("SmartConfigUIGroup"..i):SetWidth(180)
			getglobal("SmartConfigUIGroup"..i):SetHeight(groupHeight)
		end

		SmartConfigUIFrame:Show()
	else
		SmartConfigUIFrame:Hide()
	end
end

function SmartConfigUI_CommandHandler(msg)
	local msg = strlower(msg)
	if (msg == strlower("reset")) then
		SmartConfigUI_ResetConfig()
	elseif (msg == strlower("reseticon")) then
		SmartConfigUIStartFrame:Show()
		SmartConfigUIStartFrame:ClearAllPoints()
		SmartConfigUIStartFrame:SetPoint("TOPLEFT", Minimap, "TOPLEFT", -25, -55)
	else
		SmartConfigUI_OpenConfig()
	end
end

function SmartConfigUI_MinimapIconMove()
	local xpos, ypos = GetCursorPosition()
	local xmin, ymin = Minimap:GetLeft() or 400, Minimap:GetBottom() or 400

	xpos = xmin - xpos / Minimap:GetEffectiveScale() + 70
	ypos = ypos / Minimap:GetEffectiveScale() - ymin - 70

	local IconPos = math.deg(math.atan2(ypos, xpos))

	if (
		(GetMinimapShape and type(GetMinimapShape) == "function" and GetMinimapShape() == "SQUARE") or -- Squeenix
		(IsAddOnLoaded("WindMinimap") and -- WindMinimap
		((WindMinimapConfigAll and WindMinimapConfigAll["Body"][8] and WindMinimapConfigAll["Body"][8] == 2) or
		(WindMinimapBodyConfig and WindMinimapBodyConfig[8] and WindMinimapBodyConfig[8] == 2))) or
		(IsAddOnLoaded("simpleMinimap") and simpleMinimap_Skins and simpleMinimap_Skins:GetShape() == "square") -- simpleMinimap
		) then
		-- SquareMinimap Process Start
		xpos = 110 * cos(IconPos or 0)
		ypos = 110 * sin(IconPos or 0)
		xpos = math.max(-82,math.min(xpos,84))
		ypos = math.max(-86,math.min(ypos,82))
		-- SquareMinimap Process End
	else 
		-- CircleMinimap Process Start
		xpos = 78 * cos(IconPos or 0)
		ypos = 78 * sin(IconPos or 0)
		-- CircleMinimap Process End
	end

	SmartConfigUIStartFrame:ClearAllPoints()
	SmartConfigUIStartFrame:SetPoint("TOPLEFT", Minimap, "TOPLEFT", 53 - xpos, ypos - 55)
end
