ENERGYWATCH_VERSION = "EnergyWatch v2 1.4"

ENERGYWATCH_TIMER_START = 0
ENERGYWATCH_TIMER_END = 0

ENERGYWATCH_TICK_LENGTH = 2

ENERGYWATCH_ALPHA_STEP = 0.2

ENERGYWATCH_ENERGY = nil
ENERGYWATCH_MAX_ENERGY = nil;

ENERGYWATCH_STATUS = 1

ENERGYWATCH_TEXT = "&e/&em"

ENERGYWATCH_SOUND = ""
ENERGYWATCH_DEFAULT_SOUND = "SPELLBOOKCLOSE"

ENERGYWATCH_COMBO = 0

ENERGYWATCH_INVERT = false

ENERGYWATCH_SHOW = 0

ENERGYWATCH_SCALE = 1

ENERGYWATCH_PROFILE = ""

EnergyWatch_Save = {}

ENERGYWATCH_VARIABLES_LOADED = false

ENERGYWATCH_VARIABLE_TIMER = 0

ENERGYWATCH_SPARK_SIZE = 105;


function EnergyWatch_OnLoad()
	this:RegisterEvent("PLAYER_REGEN_ENABLED");
	this:RegisterEvent("PLAYER_REGEN_DISABLED");
	this:RegisterEvent("PLAYER_AURAS_CHANGED");
	this:RegisterEvent("PLAYER_COMBO_POINTS");
	this:RegisterEvent("UNIT_ENERGY");
	this:RegisterEvent("UNIT_MAXENERGY");

	SLASH_ENERGYWATCH1 = "/energywatch";
	SLASH_ENERGYWATCH2 = "/ew";
	SlashCmdList["ENERGYWATCH"] = function(msg)
		EnergyWatch_SlashCommandHandler(msg);
	end

	ENERGYWATCH_ENERGY = UnitMana("player");

	DEFAULT_CHAT_FRAME:AddMessage(ENERGYWATCH_VERSION.." Loaded - /ew");
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_ENERGY"] = function()
	EnergyWatch_TextUpdate();
end

EnergyWatch_EventHandler["UNIT_MAXENERGY"] = function()
        --EnergyWatch_OnUpdate();
        local max_energy = UnitManaMax("player");
        if (ENERGYWATCH_MAX_ENERGY == nil or ENERGYWATCH_MAX_ENERGY ~= max_energy) then
            ENERGYWATCH_MAX_ENERGY = max_energy;
        end
end

EnergyWatch_EventHandler["PLAYER_COMBO_POINTS"] = function()
	ENERGYWATCH_COMBO = GetComboPoints();
        EnergyWatch_TextUpdate();
end

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

EnergyWatch_EventHandler["PLAYER_REGEN_DISABLED"] = function()
	if ( ENERGYWATCH_SHOW ~= 3 ) then
		EnergyWatchBar:Show()
  else
    EnergyWatchBar:Hide()
	end
end

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

  checkStealth()
end

function checkStealth()
	local i = 0
	while GetPlayerBuffTexture(i) ~= nil do
    -- check for rogue's stealth and druid's prowl
		if GetPlayerBuffTexture(i) == "Interface\\Icons\\Ability_Stealth" or 
          GetPlayerBuffTexture(i) == "Interface\\Icons\\Ability_Ambush" 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 = string.lower(msg);
		if( command == "on" ) 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 == "off" ) 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 == "unlock" ) then
			if( ENERGYWATCH_STATUS ~= 2 ) then
				ENERGYWATCH_STATUS = 2;
				EnergyWatch_Save[ENERGYWATCH_PROFILE].status = ENERGYWATCH_STATUS;
				DEFAULT_CHAT_FRAME:AddMessage("기력타이머 풀림");
			end
		elseif( command == "lock" ) then
			if( ENERGYWATCH_STATUS == 2 ) then
				ENERGYWATCH_STATUS = 1;
				EnergyWatch_Save[ENERGYWATCH_PROFILE].status = ENERGYWATCH_STATUS;
				DEFAULT_CHAT_FRAME:AddMessage("기력타이머 잠금");
			end
		elseif( command == "clear" ) 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,4) == "text" ) then
			if( string.len(command) > 5 ) then
				ENERGYWATCH_TEXT = string.sub(msg,6);
			else
				ENERGYWATCH_TEXT = "";
			end
			EnergyWatch_Save[ENERGYWATCH_PROFILE].text = ENERGYWATCH_TEXT;
			EnergyWatch_TextUpdate();
		elseif( string.sub(command,1,5) == "sound" ) then
			local args = string.sub(command, 7);
			if( string.len(args) > 0 ) then
				if( args == "on" ) then
					ENERGYWATCH_SOUND = ENERGYWATCH_DEFAULT_SOUND;
				elseif( args == "off" ) 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 == "invert" ) 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 == "show always") then
			EnergyWatch_Save[ENERGYWATCH_PROFILE].show = 0;
			ENERGYWATCH_SHOW = EnergyWatch_Save[ENERGYWATCH_PROFILE].show
			if ENERGYWATCH_STATUS ~= 0 then
				EnergyWatchBar:Show()
			end
		elseif( command == "show combat") 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 == "show stealth" or command == "show stealthonly" ) then
      if ( command == "show stealth" ) then
			  EnergyWatch_Save[ENERGYWATCH_PROFILE].show = 2;
      else
  			EnergyWatch_Save[ENERGYWATCH_PROFILE].show = 3;
      end
			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, 5) == "scale" ) then
			local scale = tonumber(string.sub(command, 7))
			if( scale <= 3.0 and scale >= 0.25 ) then
				EnergyWatch_Save[ENERGYWATCH_PROFILE].scale = scale;
				ENERGYWATCH_SCALE = scale;
				EnergyWatchBar:SetScale(ENERGYWATCH_SCALE * UIParent:GetScale());
				DEFAULT_CHAT_FRAME:AddMessage("기력타이머 크기 : "..scale);
			else
				EnergyWatch_Help()
			end
		elseif( command == "help text" ) then
			EnergyWatch_HelpText();
		elseif( command == "help show" ) 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 (ENERGYWATCH_MAX_ENERGY == nil) then
            ENERGYWATCH_MAX_ENERGY = UnitManaMax("player");
        end
	
	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
		
		-- Edited by Mike Davis 09/22/05
		sparkPosition = ((Status - ENERGYWATCH_TIMER_START) / (ENERGYWATCH_TIMER_END - ENERGYWATCH_TIMER_START)) * ENERGYWATCH_SPARK_SIZE;
		if ENERGYWATCH_INVERT then
			sparkPosition = ENERGYWATCH_SPARK_SIZE - sparkPosition;
		end
		-- End of Edit
	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
        ]]
        --sparkPosition = (Energy / ENERGYWATCH_MAX_ENERGY) *  ENERGYWATCH_SPARK_SIZE;
        EnergyWatchFrameStatusBar:SetMinMaxValues(1, ENERGYWATCH_MAX_ENERGY);
        EnergyWatchFrameStatusBar:SetValue(Energy);
	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;

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

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

	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 = "Energy";
	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

	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:GetScale());

	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();

  checkStealth()
end

function EnergyWatch_TextUpdate()
	local ewtext = ENERGYWATCH_TEXT
	ewtext,_ = string.gsub(ewtext,"&em", UnitManaMax("player"))
	ewtext,_ = string.gsub(ewtext,"&e", UnitMana("player"))
	ewtext,_ = string.gsub(ewtext,"&c", ENERGYWATCH_COMBO)

	EnergyWatchText:SetText(ewtext);
        EnergyWatch_OnUpdate();
end

function EnergyWatch_Help()
	DEFAULT_CHAT_FRAME:AddMessage(ENERGYWATCH_VERSION.." : 사용법 - /ew option");
	DEFAULT_CHAT_FRAME:AddMessage(" options:");
	DEFAULT_CHAT_FRAME:AddMessage("  on      : 기력타이머 표시");
	DEFAULT_CHAT_FRAME:AddMessage("  off     : 기력타이머 숨김");
	DEFAULT_CHAT_FRAME:AddMessage("  unlock  : 기력타이머 풀림");
	DEFAULT_CHAT_FRAME:AddMessage("  lock    : 기력타이머 잠금");
	DEFAULT_CHAT_FRAME:AddMessage("  sound _ : 기력타이머 소리(on/off) 선택");
	--DEFAULT_CHAT_FRAME:AddMessage("  invert  : 기력타이머 그래프 표시 방향 선택");
	DEFAULT_CHAT_FRAME:AddMessage("  scale _ : 기력타이머 크기 (0.25 - 3.0)");
	DEFAULT_CHAT_FRAME:AddMessage("  help _  : 기력타이머 도움말");
	DEFAULT_CHAT_FRAME:AddMessage("  text _  : 기력타이머 메세지 선택");
	DEFAULT_CHAT_FRAME:AddMessage("  show _  : 기력타이머 보기 설정")
end

function EnergyWatch_HelpText()
	DEFAULT_CHAT_FRAME:AddMessage(ENERGYWATCH_VERSION.." : 사용법 - /ew text 명령어");
	DEFAULT_CHAT_FRAME:AddMessage(" 다음과 같은 명령어 사용 가능:");
	DEFAULT_CHAT_FRAME:AddMessage(" 예) /ew text 기력 &e/&em $c");
	DEFAULT_CHAT_FRAME:AddMessage("  &e  : 현재 기력 표시");
	DEFAULT_CHAT_FRAME:AddMessage("  &em : 최대 기력 표시");
	DEFAULT_CHAT_FRAME:AddMessage("  &c : 현재 연계포인트 표시");
end

function EnergyWatch_HelpShow()
	DEFAULT_CHAT_FRAME:AddMessage(ENERGYWATCH_VERSION.." : 사용법 - /ew show option");
	DEFAULT_CHAT_FRAME:AddMessage(" 명령어:");
	DEFAULT_CHAT_FRAME:AddMessage("  always  : 항상 보기");
	DEFAULT_CHAT_FRAME:AddMessage("  combat  : 전투중에만 보기");
	DEFAULT_CHAT_FRAME:AddMessage("  stealth : 전투중 및 은신상태일 때 표시");
end
