-------------------------------------------------------------------
-- TargetMod ver 1.2
-------------------------------------------------------------------
--[[
	By Arbi (Hanulari dev.)

TargetMod
	Show target's target name and bars
	Show distance of party or raid members

** History

1.2

- Added new distance frame

1.1
- Added Raidtarget Icons

1.0 
- Added Target's target health, secondry bars option
- Added Target's target health text option

0.5
- Added Target's target name display option
- Added Party or raid members distance toggle option

]]--

local TARGETMOD_BANDAGE_LIST = {
	"두꺼운 황천매듭 붕대",
	"황천매듭 붕대",
	"두꺼운 룬매듭 붕대",
	"룬매듭 붕대",
	"두꺼운 마법 붕대",
	"마법 붕대",
	"두꺼운 비단 붕대",
	"비단 붕대",
	"두꺼운 양모 붕대",
	"양모 붕대",
	"두꺼운 리넨 붕대",
	"리넨 붕대"
};

local TARGETMOD_UPDATE_RATE = 0.1
local TARGETMOD_BANDAGE_NAME;
local TargetMod_Once = false;

TargetMod_Distance = 1;
TargetMod_DisplayBar = 1;
TargetMod_HealthText = 1;

function TargetMod_OnLoad()
	this:RegisterEvent("PLAYER_ENTERING_WORLD");
	this:RegisterEvent("PLAYER_LEAVING_WORLD");

	TargetModFrame.TimeSinceLastUpdate = 0;

	if(not (Cosmos_RegisterConfiguration == nil)) then
		Cosmos_RegisterConfiguration("COS_TARGETMOD", "SECTION", TARGETMOD_SEP, TARGETMOD_SEP_INFO );
		Cosmos_RegisterConfiguration("COS_TARGETMOD_HEADER", "SEPARATOR", TARGETMOD_SEP, TARGETMOD_SEP_INFO );
		Cosmos_RegisterConfiguration("COS_TARGETMOD_ONOFF", "CHECKBOX", 
			TARGETMOD_CHECK, 
			TARGETMOD_CHECK_INFO,
			TargetMod_Toggle,
			1
			);
		Cosmos_RegisterConfiguration("COS_TARGETMOD_DISTANCE", "CHECKBOX", 
			TARGETMOD_DISTANCE, 
			TARGETMOD_DISTANCE_INFO,
			TargetMod_DistanceToggle,
			TargetMod_Distance
			);
		Cosmos_RegisterConfiguration("COS_TARGETMOD_DISPLAYBAR", "CHECKBOX", 
			TARGETMOD_DISPLAYBAR, 
			TARGETMOD_DISPLAYBAR_INFO,
			TargetMod_DisplayBarToggle,
			TargetMod_DisplayBar
			);
		Cosmos_RegisterConfiguration("COS_TARGETMOD_HEALTHTEXT", "CHECKBOX", 
			TARGETMOD_HEALTHTEXT, 
			TARGETMOD_HEALTHTEXT_INFO,
			TargetMod_HealthTextToggle,
			TargetMod_HealthText
			);
		Cosmos_RegisterConfiguration("COS_TARGETMOD_RESETPOS", "BUTTON",
			TARGETMOD_RESETPOS, 
			TARGETMOD_RESETPOS_INFO,
			TargetMod_ResetPos,
			0,
			0,
			0,
			0,
			TARGETMOD_RESETPOS_BUTTONTEXT
			);
	else
		-- ADD STANDALONE CONFIG HERE
	end
	
end

function TargetMod_Toggle(toggle)
	if ( toggle == 1 ) then
		TargetModFrame:Show();
	else
		TargetModFrame:Hide();
	end
end

function TargetMod_DistanceToggle(toggle)
	if ( toggle == 1 ) then
		TargetMod_Distance = 1;
		TargetModDistance:Show();
	else
		TargetMod_Distance = 0;
		TargetModDistance:Hide();
	end
end

function TargetMod_DisplayBarToggle(toggle)
	if ( toggle == 1 ) then
		TargetMod_DisplayBar = 1;
	else
		TargetMod_DisplayBar = 0;
	end
end

function TargetMod_HealthTextToggle(toggle)
	if ( toggle == 1 ) then
		TargetMod_HealthText = 1;
	else
		TargetMod_HealthText = 0;
	end
end

function TargetMod_AreAnyMenusVisible()
	local menu;
	for k, v in UIMenus do
		menu = getglobal(v);
		if ( menu ) then
			if ( ( menu:IsVisible() ) or ( ( menu.timeLeft) and ( menu.timeLeft > 0 ) ) ) then
				return true;
			end
		end
	end
	return false;
end


function TargetMod_OnEvent(event)
	if ( event == "PLAYER_ENTERING_WORLD" ) then
		this:RegisterEvent("PLAYER_TARGET_CHANGED");
		this:RegisterEvent("UNIT_COMBAT");
		this:RegisterEvent("PLAYER_REGEN_DISABLED");
		this:RegisterEvent("PLAYER_REGEN_ENABLED");
		this:RegisterEvent("UNIT_HEALTH");
		this:RegisterEvent("UNIT_MAXHEALTH");
		this:RegisterEvent("UNIT_MANA");
		this:RegisterEvent("UNIT_MAXMANA");
		this:RegisterEvent("RAID_TARGET_UPDATE");

		if ( not TargetMod_Once ) then
			TargetMod_ScanBandage();
		end
	elseif ( event == "PLAYER_LEAVING_WORLD" ) then
		this:UnregisterEvent("PLAYER_TARGET_CHANGED");
		this:UnregisterEvent("UNIT_COMBAT");
		this:UnregisterEvent("PLAYER_REGEN_DISABLED");
		this:UnregisterEvent("PLAYER_REGEN_ENABLED");
		this:UnregisterEvent("UNIT_HEALTH");
		this:UnregisterEvent("UNIT_MAXHEALTH");
		this:UnregisterEvent("UNIT_MANA");
		this:UnregisterEvent("UNIT_MAXMANA");
		this:UnregisterEvent("RAID_TARGET_UPDATE");
	elseif( event == "PLAYER_TARGET_CHANGED" ) then
		TargetMod_UpdateRaidTargetIcon();
		TargetMod_Scan();
	elseif( event == "UNIT_COMBAT" or event == "PLAYER_REGEN_DISABLED" or event == "PLAYER_REGEN_ENABLED" or event == "UNIT_HEALTH" or event == "UNIT_MAXHEALTH" or event == "UNIT_MANA" or event == "UNIT_MAXMANA" ) then		
		TargetMod_Scan();
	elseif ( event == "RAID_TARGET_UPDATE" ) then
		TargetModTargetRaidTargetIcon:Hide();
		TargetMod_UpdateRaidTargetIcon();
	end
end

function TargetMod_ScanBandage()
	for bag = 0, 4 do
		for slot=1,GetContainerNumSlots(bag) do
			if (GetContainerItemLink(bag,slot)) then
				for k, v in pairs(TARGETMOD_BANDAGE_LIST) do
					if (string.find(GetContainerItemLink(bag,slot), v)) then
						TARGETMOD_BANDAGE_NAME = v;
						break
					end
				end
			end
		end
	end
	TargetMod_Once = true;
end

function TargetMod_Scan()
	local health = UnitHealth("targettarget");
	local healthmax = UnitHealthMax("targettarget");
	local mana = UnitMana("targettarget");
	local manamax = UnitManaMax("targettarget");
	local info = ManaBarColor[UnitPowerType("targettarget")];
	if ( TargetMod_DisplayBar == 1 ) then
		TargetModFrameHPBar:Show();
		TargetModFrameHPBar:SetMinMaxValues(0, healthmax);
		TargetModFrameHPBar:SetValue(health);
		TargetModFrameHPBar:SetStatusBarColor(TargetMod_GetHealthBarColor(health, 0, healthmax, 0));
		TargetModFrameMPBar:Show();
		TargetModFrameMPBar:SetMinMaxValues(0, manamax);
		TargetModFrameMPBar:SetValue(mana);
		TargetModFrameMPBar:SetStatusBarColor(info.r, info.g, info.b);
	else
		TargetModFrameHPBar:Hide();
		TargetModFrameMPBar:Hide();
	end
	if ( UnitIsGhost("target") or ( not UnitName("targettarget") ) or ( UnitName("targettarget") == UNKNOWNOBJECT ) ) then
		TargetModText:SetTextColor(1.0,1.0,1.0);
		TargetModTexture:SetVertexColor(1,1,1);
		TargetModText:SetText("대상 없음");
		TargetModFrameHealthText:Hide();
		TargetModTargetRaidTargetIcon:Hide();
	elseif ( not UnitIsPlayer("target") and UnitName("targettarget") == UnitName("player") ) then
		TargetModText:SetTextColor(1.0,0,0);
		TargetModTexture:SetVertexColor(1,0,0);
		TargetModText:SetText(UnitName("targettarget"));
		if ( TargetMod_HealthText == 1 ) then
			TargetModFrameHealthText:Show();
			if ( healthmax == 100 ) then
				TargetModFrameHealthText:SetText(health.."/"..healthmax.."%");
			else
				TargetModFrameHealthText:SetText(health.."/"..healthmax);
			end
		end
	else
		TargetModText:SetTextColor(1.0,1.0,1.0);
		TargetModTexture:SetVertexColor(1,1,1);
		TargetModText:SetText(UnitName("targettarget"));
		if ( TargetMod_HealthText == 1 ) then
			TargetModFrameHealthText:Show();
			if ( healthmax == 100 ) then
				TargetModFrameHealthText:SetText(health.."/"..healthmax.."%");
			else
				TargetModFrameHealthText:SetText(health.."/"..healthmax);
			end
		end
	end
end

function TargetMod_OnUpdate(arg1)
	TargetModFrame.TimeSinceLastUpdate = TargetModFrame.TimeSinceLastUpdate + arg1;
	if(not TargetFrame:IsShown()) then
		return;
	end
	if( TargetModFrame.TimeSinceLastUpdate > TARGETMOD_UPDATE_RATE ) then
		if ( TargetMod_Distance == 1 ) then
			if(UnitIsUnit("target", "player")) then
				TargetModDistance:Hide();
			else
				TargetModDistance:Show();
			end
			TargetMod_ScanDistance();
			TargetMod_Scan();
		else
			TargetMod_Scan();
		end
		TargetModFrame.TimeSinceLastUpdate = 0;
	end
end

function TargetMod_Click()
	local targettext = TargetModText:GetText();
	if ( targettext == "대상 없음" ) then
		return;
	else
		TargetUnit("targettarget");
	end
end

function TargetMod_ResetPos()
	local targetmodframe = getglobal("TargetModFrame");
	targetmodframe:ClearAllPoints();
	targetmodframe:SetPoint("TOP","TargetFrame","TOP",-53,10);
end

function TargetMod_UpdateRaidTargetIcon()
	local index = GetRaidTargetIndex("targettarget");
	if ( index ) then
		SetRaidTargetIconTexture(TargetModTargetRaidTargetIcon, index);
		TargetModTargetRaidTargetIcon:Show();
	else
		TargetModTargetRaidTargetIcon:Hide();
	end
end

function TargetMod_ScanDistance()
	if ( CheckInteractDistance("target", 3) and CheckInteractDistance("target", 4) ) then
		TargetModDistanceTexture:SetVertexColor(1, 1, 1);
		TargetModDistanceText:SetText("~10m", 1, 1, 1);
		TargetModDistanceText:SetTextColor(1, 1, 1);
	elseif ( IsItemInRange(TARGETMOD_BANDAGE_NAME, "target") == 1 and not CheckInteractDistance("target", 3) ) then
		TargetModDistanceTexture:SetVertexColor(0, 1, 1);
		TargetModDistanceText:SetText("~15m");
		TargetModDistanceText:SetTextColor(0, 1, 1);
	elseif ( CheckInteractDistance("target", 1) and not CheckInteractDistance("target", 3) ) then
		TargetModDistanceTexture:SetVertexColor(0, 1, 1);
		TargetModDistanceText:SetText("~30m");
		TargetModDistanceText:SetTextColor(0, 1, 0);
	elseif ( not CheckInteractDistance("target", 1) ) then
		TargetModDistanceTexture:SetVertexColor(1, 0, 0);
		TargetModDistanceText:SetText("30m+");
		TargetModDistanceText:SetTextColor(1, 0, 0);
	end
end

function TargetMod_GetHealthBarColor(value, min, max, smooth)
	if ( not value ) then
		return;
	end
	local r, g, b;
	if ( (value < min) or (value > max) ) then
		return;
	end
	if ( (max - min) > 0 ) then
		value = (value - min) / (max - min);
	else
		value = 0;
	end
	if(smooth) then
		if(value > 0.5) then
			r = (1.0 - value) * 2;
			g = 1.0;
		else
			r = 1.0;
			g = value * 2;
		end
	else
		r = 0.0;
		g = 1.0;
	end
	b = 0.0;
	return r, g, b;
end