ENERGYWATCH_VERSION = "기력타이머 v1.3"

ENERGYWATCH_TIMER_START = 0
ENERGYWATCH_TIMER_END = 0

ENERGYWATCH_TICK_LENGTH = 2

ENERGYWATCH_ALPHA_STEP = 0.2

ENERGYWATCH_ENERGY = nil

ENERGYWATCH_STATUS = 0

ENERGYWATCH_TEXT = "기력"

ENERGYWATCH_SOUND = ""
ENERGYWATCH_DEFAULT_SOUND = "SPELLBOOKCLOSE"

ENERGYWATCH_INVERT = false

ENERGYWATCH_SHOW = 0

ENERGYWATCH_SCALE = 1.3

ENERGYWATCH_PROFILE = ""

EnergyWatch_Save = {}

ENERGYWATCH_VARIABLES_LOADED = false

ENERGYWATCH_VARIABLE_TIMER = 0

EnergyWatch_OnOffStatus = 0;

EnergyWatch_LockStatus = 0;

EnergyWatch_InvertStatus = 0;

EnergyWatch_SoundStatus = 0;

EnergyWatch_CombatStatus = 0;

function EnergyWatch_OnLoad()
	this:RegisterEvent("UNIT_MANA");
	this:RegisterEvent("PLAYER_REGEN_ENABLED");
	this:RegisterEvent("PLAYER_REGEN_DISABLED");
	this:RegisterEvent("PLAYER_AURAS_CHANGED");

	local playerName;
	if playerName==nil or not playerName==UNKNOWNBEING or not playerName==UKNOWNBEING or not playerName==UNKNOWNOBJECT then
		playerName = UnitName("player");
	end

	ENERGYWATCH_PROFILE = GetCVar("RealmName") .." 서버의 ".. playerName;

	ENERGYWATCH_ENERGY = UnitMana("player");

	if (Cosmos_RegisterConfiguration ~= nil) then
		EnergyWatch_CosmosRegistration();
		--ENERGYWATCH_VARIABLES_LOADED = true;
	else
		SLASH_ENERGYWATCH1 = "/energywatch";
		SLASH_ENERGYWATCH2 = "/ew";
		SlashCmdList["ENERGYWATCH"] = function(msg)
			EnergyWatch_SlashCommandHandler(msg);
		end

		DEFAULT_CHAT_FRAME:AddMessage(ENERGYWATCH_VERSION.." Loaded - /ew");
	end
end

function EnergyWatch_CosmosRegistration()
	Cosmos_RegisterConfiguration(
		"COS_EW",
		"SECTION",
		EW_HEADER,
		EW_INFO
	);
	Cosmos_RegisterConfiguration(
		"COS_EW_SEPARATOR",
		"SEPARATOR",
		EW_HEADER,
		EW_INFO
	);
	Cosmos_RegisterConfiguration(
		"COS_EW_ENABLE",
		"CHECKBOX",
		EW_ENABLE_TEXT,
		EW_ENABLE_INFO,
		EnergyWatch_Enable,
		EnergyWatch_OnOffStatus
	);
	Cosmos_RegisterConfiguration(
		"COS_EW_LOCK",
		"CHECKBOX",
		EW_LOCK_TEXT,
		EW_LOCK_INFO,
		EnergyWatch_Lock,
		EnergyWatch_LockStatus
	);
	Cosmos_RegisterConfiguration(
		"COS_EW_SOUND",
		"CHECKBOX",
		EW_SOUND_TEXT,
		EW_SOUND_INFO,
		EnergyWatch_Sound,
		EnergyWatch_SoundStatus
	);
	Cosmos_RegisterConfiguration(
		"COS_EW_INVERT",
		"CHECKBOX",
		EW_INVERT_TEXT,
		EW_INVERT_INFO,
		EnergyWatch_Invert,
		EnergyWatch_InvertStatus
	);
	Cosmos_RegisterConfiguration(
		"COS_EW_COMBAT",
		"CHECKBOX",
		EW_COMBAT_TEXT,
		EW_COMBAT_INFO,
		EnergyWatch_Combat,
		EnergyWatch_CombatStatus
	);
	if ( Cosmos_RegisterChatCommand ) then
		local EWCommands = {"/기력타이머","/ew"};				
		Cosmos_RegisterChatCommand (
			"EW_COMMANDS", -- Some Unique Group ID
			EWCommands, -- The Commands
			EnergyWatch_SlashCommandHandler,
			EW_CHAT_COMMAND_INFO -- Description String
		);
	end
end

function EnergyWatch_Enable(toggle)
	if ( toggle == 1 ) then
		ENERGYWATCH_STATUS = 1;
		EnergyWatch_OnOffStatus = 1;
		EnergyWatchBar:Show();
		EnergyWatch_TextUpdate();
	else
		ENERGYWATCH_STATUS = 0;
		EnergyWatch_OnOffStatus = 0;
		EnergyWatchBar:Hide();
		EnergyWatch_TextUpdate();
	end
end

function EnergyWatch_Lock(toggle)
	if ( toggle == 1 ) then
		ENERGYWATCH_STATUS = 1;
		EnergyWatch_LockStatus = 1;
	else
		ENERGYWATCH_STATUS = 2;
		EnergyWatch_LockStatus = 0;
	end
end

function EnergyWatch_Sound(toggle)
	if ( toggle == 1 ) then
		EnergyWatch_SoundStatus = 1;
		ENERGYWATCH_SOUND = ENERGYWATCH_DEFAULT_SOUND;
	else
		EnergyWatch_SoundStatus = 0;
		ENERGYWATCH_SOUND = "";
	end
end

function EnergyWatch_Invert(toggle)
	if ( toggle == 1 ) then
		ENERGYWATCH_INVERT = true;
		EnergyWatch_InvertStatus = 1;
	else
		ENERGYWATCH_INVERT = false;
		EnergyWatch_InvertStatus = 0;
	end
end

function EnergyWatch_Combat(toggle)
	if ( toggle == 1 ) then
		ENERGYWATCH_SHOW = 1;
		EnergyWatch_CombatStatus = 1;
		if ENERGYWATCH_STATUS ~= 0 then
			if UnitAffectingCombat("player") then
				EnerbyWatchBar:Show()
			else
				EnergyWatchBar:Hide()
			end
		end
	else
		ENERGYWATCH_SHOW = 0;
		EnergyWatch_CombatStatus = 0;
	end
end

function EnergyWatch_OnEvent(event)
	if( ENERGYWATCH_STATUS == 0 ) then
		return
	end

	EnergyWatch_EventHandler[event](arg1, arg2, arg3, arg4, arg5);
end

EnergyWatch_EventHandler = {}

EnergyWatch_EventHandler["UNIT_MANA"] = function()
	EnergyWatch_TextUpdate();
end

EnergyWatch_EventHandler["PLAYER_REGEN_ENABLED"] = function()
	if ENERGYWATCH_SHOW > 0 then
		EnergyWatchBar:Hide()
	end
end

EnergyWatch_EventHandler["PLAYER_REGEN_DISABLED"] = function()
	if ENERGYWATCH_SHOW > 0 then
		EnergyWatchBar:Show()
	end
end

EnergyWatch_EventHandler["PLAYER_AURAS_CHANGED"] = function()
	if ENERGYWATCH_SHOW ~= 2 then
		return
	end

	local i = 0
	while GetPlayerBuffTexture(i) ~= nil do
		if GetPlayerBuffTexture(i) == "Interface\\Icons\\Ability_Stealth" then
			EnergyWatchBar:Show()
			return
		else
			i = i + 1
		end
	end
	if not UnitAffectingCombat("player") then
		EnergyWatchBar:Hide()
	end
end

function EnergyWatch_SlashCommandHandler(msg)
	if( msg ) then
		local command = msg;
		if( command == "켜기" ) then
			if( ENERGYWATCH_STATUS == 0 ) then
				ENERGYWATCH_STATUS = 1;
				EnergyWatch_Save[ENERGYWATCH_PROFILE].status = ENERGYWATCH_STATUS;
				DEFAULT_CHAT_FRAME:AddMessage("기력타이머가 켜졌습니다.");
				EnergyWatchBar:Show();
			end
		elseif( command == "끄기" ) then
			if( ENERGYWATCH_STATUS ~= 0 ) then
				ENERGYWATCH_STATUS = 0;
				EnergyWatch_Save[ENERGYWATCH_PROFILE].status = ENERGYWATCH_STATUS;
				DEFAULT_CHAT_FRAME:AddMessage("기력타이머가 꺼졌습니다.");
				EnergyWatchBar:Hide();
			end
		elseif( command == "이동" ) then
			if( ENERGYWATCH_STATUS ~= 2 ) then
				ENERGYWATCH_STATUS = 2;
				EnergyWatch_Save[ENERGYWATCH_PROFILE].status = ENERGYWATCH_STATUS;
				DEFAULT_CHAT_FRAME:AddMessage("기력타이머를 이동시킬 수 있습니다.");
			end
		elseif( command == "잠금" ) then
			if( ENERGYWATCH_STATUS == 2 ) then
				ENERGYWATCH_STATUS = 1;
				EnergyWatch_Save[ENERGYWATCH_PROFILE].status = ENERGYWATCH_STATUS;
				DEFAULT_CHAT_FRAME:AddMessage("기력타이머가 잠금 상태가 되었습니다.");
			end
		elseif( command == "초기화" ) then
			local pn = UnitName("player");
			if(pn ~= nil and pn ~= UNKNOWNBEING and pn ~= UKNOWNBEING and pn ~= UNKNOWNOBJECT) then
				ENERGYWATCH_PROFILE = GetCVar("RealmName") .. " 서버의 " .. pn;
				EnergyWatch_Save[ENERGYWATCH_PROFILE] = nil;
				ENERGYWATCH_VARIABLES_LOADED = false;
				EnergyWatch_LoadVariables(2);
			else
				DEFAULT_CHAT_FRAME:AddMessage("기력타이머: 로딩이 끝나지 않았습니다, 기다려 주세요...")
			end

		elseif( string.sub(command,1,6) == "문자" ) then
			if( string.len(command) > 7 ) then
				ENERGYWATCH_TEXT = string.sub(msg,8);
			else
				ENERGYWATCH_TEXT = "";
			end
			EnergyWatch_Save[ENERGYWATCH_PROFILE].text = ENERGYWATCH_TEXT;
			EnergyWatch_TextUpdate();
		elseif( string.sub(command,1,6) == "소리" ) then
			local args = string.sub(command, 8);
			if( string.len(args) > 0 ) then
				if( args == "켜기" ) then
					ENERGYWATCH_SOUND = ENERGYWATCH_DEFAULT_SOUND;
				elseif( args == "끄기" ) then
					ENERGYWATCH_SOUND = "";
				else
					ENERGYWATCH_SOUND = args;
				end
				EnergyWatch_Save[ENERGYWATCH_PROFILE].sound = ENERGYWATCH_SOUND;
			else
				if( ENERGYWATCH_SOUND == "" ) then
					DEFAULT_CHAT_FRAME:AddMessage("기력 타이머 소리기능이 꺼졌습니다.");
				elseif( ENERGYWATCH_SOUND == ENERGYWATCH_DEFAULT_SOUND ) then
					DEFAULT_CHAT_FRAME:AddMessage("기력타이머 소리기능이 켜졌습니다.");
				else
					DEFAULT_CHAT_FRAME:AddMessage("기력타이머 소리기능이 "..ENERGYWATCH_SOUND.." 상태입니다.");
				end
			end
		elseif( command == "반전" ) then
			ENERGYWATCH_INVERT = not ENERGYWATCH_INVERT;
			EnergyWatch_Save[ENERGYWATCH_PROFILE].invert = ENERGYWATCH_INVERT;
			if ENERGYWATCH_INVERT then
				DEFAULT_CHAT_FRAME:AddMessage("기력타이머 방향 반전기능이 켜졌습니다.");
			else
				DEFAULT_CHAT_FRAME:AddMessage("기력타이머 방향 반전기능이 꺼졌습니다.");
			end
		elseif( command == "표시 항상") then
			EnergyWatch_Save[ENERGYWATCH_PROFILE].show = 0;
			ENERGYWATCH_SHOW = EnergyWatch_Save[ENERGYWATCH_PROFILE].show
			if ENERGYWATCH_STATUS ~= 0 then
				EnergyWatchBar:Show()
			end
		elseif( command == "표시 전투") then
			EnergyWatch_Save[ENERGYWATCH_PROFILE].show = 1;
			ENERGYWATCH_SHOW = EnergyWatch_Save[ENERGYWATCH_PROFILE].show
			if ENERGYWATCH_STATUS ~= 0 then
				if UnitAffectingCombat("player") then
					EnerbyWatchBar:Show()
				else
					EnergyWatchBar:Hide()
				end
			end
		elseif( command == "표시 은신") then
			EnergyWatch_Save[ENERGYWATCH_PROFILE].show = 2;
			ENERGYWATCH_SHOW = EnergyWatch_Save[ENERGYWATCH_PROFILE].show
			local i = 0
			while GetPlayerBuffTexture(i) ~= nil do
				if GetPlayerBuffTexture(i) == "Interface\\Icons\\Ability_Stealth" then
					EnergyWatchBar:Show()
					return
				else
					i = i + 1
				end
			end
			if UnitAffectingCombat("player") then
				EnergyWatchBar:Show()
			else
				EnergyWatchBar:Hide()
			end
		elseif( string.sub(command, 1, 6) == "크기" ) then
			local scale = tonumber(string.sub(command, 8))
			if( scale <= 3.0 and scale >= 0.25 ) then
				EnergyWatch_Save[ENERGYWATCH_PROFILE].scale = scale;
				ENERGYWATCH_SCALE = scale;
				EnergyWatchBar:SetScale(ENERGYWATCH_SCALE * UIParent:GetEffectiveScale());
				DEFAULT_CHAT_FRAME:AddMessage("기력타이머 크기가 "..scale.."|1으로;로; 변경되었습니다.");
			else
				EnergyWatch_Help()
			end
		elseif( command == "도움말 글자" ) then
			EnergyWatch_HelpText();
		elseif( command == "도움말 표시" ) then
			EnergyWatch_HelpShow();
		else
			EnergyWatch_Help();
		end
	end
end

--function EnergyWatch_OnShow()
--	ENERGYWATCH_ENERGY = UnitMana("player");
--	ENERGYWATCH_TIMER_START = GetTime();
--	ENERGYWATCH_TIMER_END = ENERGYWATCH_TIMER_START + ENERGYWATCH_TICK_LENGTH;
--	EnergyWatchFrameStatusBar:SetMinMaxValues(ENERGYWATCH_TIMER_START, ENERGYWATCH_TIMER_END);
--	EnergyWatchFrameStatusBar:SetValue(ENERGYWATCH_TIMER_START);
--	EnergyWatchSpark:SetPoint("CENTER", "EnergyWatchFrameStatusBar", "LEFT", 0, 0);
--
--end

function EnergyWatch_OnUpdate()
	if( ENERGYWATCH_STATUS == 0 ) then
		return;
	end

	local Energy = UnitMana("player")
	local Status = GetTime()
	local sparkPosition = 1;
	if( Energy > ENERGYWATCH_ENERGY or Status >= ENERGYWATCH_TIMER_END) then
		if( ENERGYWATCH_SOUND ~= "" ) then
			PlaySound(ENERGYWATCH_SOUND);
		end
		ENERGYWATCH_ENERGY = Energy;
		ENERGYWATCH_TIMER_START = Status;
		ENERGYWATCH_TIMER_END = Status + ENERGYWATCH_TICK_LENGTH;
	else
		if( ENERGYWATCH_ENERGY ~= Energy ) then
			ENERGYWATCH_ENERGY = Energy;
		end
		sparkPosition = ((Status - ENERGYWATCH_TIMER_START) / (ENERGYWATCH_TIMER_END - ENERGYWATCH_TIMER_START)) * 65;
		if ENERGYWATCH_INVERT then
			sparkPosition = 65 - sparkPosition;
		end
	end
	EnergyWatchFrameStatusBar:SetMinMaxValues(ENERGYWATCH_TIMER_START, ENERGYWATCH_TIMER_END);
	if ENERGYWATCH_INVERT then
		EnergyWatchFrameStatusBar:SetValue(ENERGYWATCH_TIMER_START + ENERGYWATCH_TIMER_END - Status);
	else
		EnergyWatchFrameStatusBar:SetValue(Status);
	end
	if( sparkPosition < 1 ) then
		sparkPosition = 1;
	end
	EnergyWatchSpark:SetPoint("CENTER", "EnergyWatchFrameStatusBar", "LEFT", sparkPosition, 0);
end

function EnergyWatch_LoadVariables(arg1)
	if ENERGYWATCH_VARIABLES_LOADED then
		return
	end

	ENERGYWATCH_VARIABLE_TIMER = ENERGYWATCH_VARIABLE_TIMER + arg1
	if ENERGYWATCH_VARIABLE_TIMER < 0.2 then
		return
	end
	ENERGYWATCH_VARIABLE_TIMER = 0;

	if EnergyWatch_Save[ENERGYWATCH_PROFILE] == nil then
		EnergyWatch_Save[ENERGYWATCH_PROFILE] = { };
		if EW_Save ~= nil and EW_Save[ENERGYWATCH_PROFILE] ~= nil then
			if EW_Save[ENERGYWATCH_PROFILE].status ~= nil then
				EnergyWatch_Save[ENERGYWATCH_PROFILE].status = EW_Save[ENERGYWATCH_PROFILE].status;
				EW_Save[ENERGYWATCH_PROFILE].status = nil
			end
			if EW_Save[ENERGYWATCH_PROFILE].text ~= nil then
				EnergyWatch_Save[ENERGYWATCH_PROFILE].text = EW_Save[ENERGYWATCH_PROFILE].text;
				EW_Save[ENERGYWATCH_PROFILE].text = nil
			end
			if EW_Save[ENERGYWATCH_PROFILE].sound ~= nil then
				EnergyWatch_Save[ENERGYWATCH_PROFILE].sound = EW_Save[ENERGYWATCH_PROFILE].sound;
				EW_Save[ENERGYWATCH_PROFILE].sound = nil
			end
			if EW_Save[ENERGYWATCH_PROFILE] == { } then
				EW_Save[ENERGYWATCH_PROFILE] = nil;
			end
			if EW_Save == { } then
				EW_Save = nil;
			end
		end
	end

	if EnergyWatch_Save[ENERGYWATCH_PROFILE].status == nil then
		EnergyWatch_Save[ENERGYWATCH_PROFILE].status = 0;
	end
	if EnergyWatch_Save[ENERGYWATCH_PROFILE].text == nil then
		EnergyWatch_Save[ENERGYWATCH_PROFILE].text = "기력";
	end
	if EnergyWatch_Save[ENERGYWATCH_PROFILE].sound == nil then
		EnergyWatch_Save[ENERGYWATCH_PROFILE].sound = "";
	end
	if EnergyWatch_Save[ENERGYWATCH_PROFILE].sound == nil then
		EnergyWatch_Save[ENERGYWATCH_PROFILE].invert = false;
	end
	if EnergyWatch_Save[ENERGYWATCH_PROFILE].show == nil then
		EnergyWatch_Save[ENERGYWATCH_PROFILE].show = 0;
	end
	if EnergyWatch_Save[ENERGYWATCH_PROFILE].scale == nil then
		EnergyWatch_Save[ENERGYWATCH_PROFILE].scale = 1;
	end
	if Cosmos_RegisterConfiguration ~= nil then
		EnergyWatch_Save[ENERGYWATCH_PROFILE].status = EnergyWatch_OnOffStatus;
		EnergyWatch_Save[ENERGYWATCH_PROFILE].invert = ENERGYWATCH_INVERT;
		EnergyWatch_Save[ENERGYWATCH_PROFILE].sound = ENERGYWATCH_SOUND;
		EnergyWatch_Save[ENERGYWATCH_PROFILE].show = ENERGYWATCH_SHOW;
	end

	ENERGYWATCH_SOUND = EnergyWatch_Save[ENERGYWATCH_PROFILE].sound;
	ENERGYWATCH_TEXT = EnergyWatch_Save[ENERGYWATCH_PROFILE].text;
	ENERGYWATCH_STATUS = EnergyWatch_Save[ENERGYWATCH_PROFILE].status;
	ENERGYWATCH_INVERT = EnergyWatch_Save[ENERGYWATCH_PROFILE].invert;
	ENERGYWATCH_SHOW = EnergyWatch_Save[ENERGYWATCH_PROFILE].show;
	ENERGYWATCH_SCALE = EnergyWatch_Save[ENERGYWATCH_PROFILE].scale;

	EnergyWatchBar:SetScale(ENERGYWATCH_SCALE * UIParent:GetEffectiveScale());

	if( ENERGYWATCH_STATUS ~= 0 ) then
		if ENERGYWATCH_SHOW == 0 then
			EnergyWatchBar:Show();
		end
	else
		EnergyWatchBar:Hide();
	end
	EnergyWatch_TextUpdate();

	ENERGYWATCH_VARIABLES_LOADED = true;
	EnergyWatch_Variable_Frame:Hide();
end

function EnergyWatch_TextUpdate()
	local ewtext = ENERGYWATCH_TEXT
	ewtext,_ = string.gsub(ewtext,"&em", UnitManaMax("player"))
	ewtext,_ = string.gsub(ewtext,"&e", UnitMana("player"))
	EnergyWatchText:SetText(ewtext);
end

function EnergyWatch_Help()
		DEFAULT_CHAT_FRAME:AddMessage(ENERGYWATCH_VERSION.." : 사용법 - /기력타이머 명령어");
		DEFAULT_CHAT_FRAME:AddMessage("명령어:");
		DEFAULT_CHAT_FRAME:AddMessage(" 켜기      : 기력타이머를 켭니다.");
		DEFAULT_CHAT_FRAME:AddMessage(" 끄기     : 기력타이머를 끕니다.");
		DEFAULT_CHAT_FRAME:AddMessage(" 이동  : 기력타이머 창을 이동합니다.");
		DEFAULT_CHAT_FRAME:AddMessage(" 잠금    : 기력타이머 창을 잠급니다.");
		DEFAULT_CHAT_FRAME:AddMessage(" 소리 <소리이름> : 기력타이머에 효과음을 냅니다.");
		DEFAULT_CHAT_FRAME:AddMessage(" 반전  : 바의 이동방향을 반전시킵니다.");
		DEFAULT_CHAT_FRAME:AddMessage(" 크기 <숫자> : 기력 타이머의 크기를 조정합니다 (0.25 - 3.0)");
		DEFAULT_CHAT_FRAME:AddMessage(" 도움말 <명령어>  : 지정한 명령어의 도움말을 표시합니다.");
		DEFAULT_CHAT_FRAME:AddMessage(" 문자 <설정할 문자>  : 기력타이머의 문자를 설정합니다.");
		DEFAULT_CHAT_FRAME:AddMessage(" 표시 <옵션> : 기력타이머가 표시될 조건을 설정합니다.")
end

function EnergyWatch_HelpText()
	DEFAULT_CHAT_FRAME:AddMessage(ENERGYWATCH_VERSION.." : 사용법 - /기력타이머 글자 옵션");
	DEFAULT_CHAT_FRAME:AddMessage(" 옵션은 몇몇 특수한 상황에서 글자 변경시 사용합니다:");
	DEFAULT_CHAT_FRAME:AddMessage("  &e  : 현재 기력");
	DEFAULT_CHAT_FRAME:AddMessage("  &em : 최대 기력");
end

function EnergyWatch_HelpShow()
	DEFAULT_CHAT_FRAME:AddMessage(ENERGYWATCH_VERSION.." : 사용법 - /기력타이머 표시 옵션");
	DEFAULT_CHAT_FRAME:AddMessage(" 옵션:");
	DEFAULT_CHAT_FRAME:AddMessage("  항상  : 항상 표시");
	DEFAULT_CHAT_FRAME:AddMessage("  전투  : 전투중에만 표시");
	DEFAULT_CHAT_FRAME:AddMessage("  은신 : 은신 시 숨기고 전투중에만 표시");
end
