--------------------------------------------
--
-- SMART - PLAYER
--
--------------------------------------------
SMART_PLAYER_ENABLE = true;
SMART_PLAYER_Config = {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};

function SmartPlayer_OnLoad(self)
	self:RegisterEvent("PLAYER_ENTERING_WORLD");
	self:RegisterEvent("VARIABLES_LOADED");
--[[	
	self:RegisterEvent("UNIT_COMBAT");
	self:RegisterEvent("UNIT_PET");
	
	self:RegisterEvent("UNIT_MANA");
	self:RegisterEvent("UNIT_RAGE");
	self:RegisterEvent("UNIT_FOCUS");
	self:RegisterEvent("UNIT_ENERGY");
	self:RegisterEvent("UNIT_HAPPINESS");
	self:RegisterEvent("UNIT_RUNIC_POWER");

	self:RegisterEvent("UNIT_MAXMANA");
	self:RegisterEvent("UNIT_MAXRAGE");
	self:RegisterEvent("UNIT_MAXFOCUS");
	self:RegisterEvent("UNIT_MAXENERGY");
	self:RegisterEvent("UNIT_MAXRUNIC_POWER");

	self:RegisterEvent("UNIT_DISPLAYPOWER");
	]]

	PlayerFrameHealthBarText:SetAlpha(0);
	PlayerFrameManaBarText:SetAlpha(0);
	PetFrameHealthBarText:SetAlpha(0);
	PetFrameManaBarText:SetAlpha(0);
	PlayerFrame:SetMovable(true);
	--PlayerFrame:SetResizable(true);
	PetFrame:SetMovable(true);
	--PetFrame:SetResizable(true);
	CreateFrame("Frame","SmartPetFrame",PetFrame);
	SmartPetFrame:SetPoint("TOPLEFT",PetFrame,"TOPLEFT");
	SmartPetFrame:SetFrameLevel(PetFrame:GetFrameLevel()+2);
	SmartPetFrame:SetWidth(128);
	SmartPetFrame:SetHeight(53);
	SmartPetFrame:CreateFontString("SmartPetFrameHealthBarText","ARTWORK");
	SmartPetFrameHealthBarText:SetPoint("CENTER",PetFrame,"TOPLEFT",82,-27);
	SmartPetFrameHealthBarText:SetTextColor(1,1,1);
	SmartPetFrameHealthBarText:SetFontObject("SmartStatusBarText");
	SmartPetFrameHealthBarText:SetJustifyH("CENTER");
	SmartPetFrame:CreateFontString("SmartPetFrameManaBarText","ARTWORK");
	SmartPetFrameManaBarText:SetPoint("CENTER",PetFrame,"TOPLEFT",82,-38);
	SmartPetFrameManaBarText:SetTextColor(1,1,1);
	SmartPetFrameManaBarText:SetFontObject("SmartStatusBarText");
	SmartPetFrameManaBarText:SetJustifyH("CENTER");
	PlayerStatusTexture:SetTexture("Interface\\AddOns\\SmartUnitFrame\\CircleFlash.tga");
	PlayerStatusTexture:SetWidth(64);
	PlayerStatusTexture:SetHeight(64);
	PlayerStatusTexture:SetTexCoordModifiesRect(false);
	PlayerStatusTexture:SetTexCoord(0,1,0,1);
	PlayerStatusTexture:ClearAllPoints();
	--PlayerStatusTexture:SetBlendMode("ADD");
	PlayerStatusTexture:SetPoint("CENTER",PlayerPortrait,"CENTER",0,0);

	PlayerFrame:SetHitRectInsets(36,0,16,22);
end

function SmartPlayer_ResetPosition()
	if PlayerFrame:IsUserPlaced() then
		PlayerFrame:ClearAllPoints();
		if SMART_PLAYER_Config.elite or SMART_PVPICON_ENABLE or SMART_GROUPICON_ENABLE then
			PlayerFrame:SetPoint("TOPLEFT", "UIParent", "TOPLEFT", 5, 0);
		else
			PlayerFrame:SetPoint("TOPLEFT", "UIParent", "TOPLEFT", -16, 0);
		end
	else
		local _, _, _, _, yOfs = PlayerFrame:GetPoint();
		PlayerFrame:ClearAllPoints();
		if SMART_PLAYER_Config.elite or SMART_PVPICON_ENABLE or SMART_GROUPICON_ENABLE then
			PlayerFrame:SetPoint("TOPLEFT", "UIParent", "TOPLEFT", 5, yOfs);
		else
			PlayerFrame:SetPoint("TOPLEFT", "UIParent", "TOPLEFT", -16, yOfs);
		end
	end
	PlayerFrame:SetUserPlaced(false);
end

function SmartPlayer_PetFramePosition()
	PetFrame:ClearAllPoints();
	PetFrame:SetPoint("TOPLEFT", "PlayerFrame", "TOPLEFT", 106, -64);
	PetFrame:SetUserPlaced(false);
	PetFrameHappiness:SetPoint("LEFT", "PetFrame", "RIGHT", -150, -4);
end

function SmartPlayer_OnUpdate(self, elapsed)
	SmartPlayer_HealthUpdate()
	SmartPlayer_ManaUpdate()

	SmartPlayerPet_HealthUpdate();
	SmartPlayerPet_ManaUpdate();
end

function SmartPlayer_OnEvent(self, event, ...)
	local arg1, arg2, arg3, arg4, arg5 = ...;

	if (event == "VARIABLES_LOADED") then
		if not PlayerFrame:IsUserPlaced() then
			SmartPlayer_ResetPosition();
		end
		SmartPlayer_PetFramePosition();
	elseif (event == "PLAYER_ENTERING_WORLD") then
		if SMART_PLAYER_Config.elite then
			if SMART_PLAYER_Config.rare then
				PlayerFrameTexture:SetTexture("Interface\\TargetingFrame\\UI-TargetingFrame-Rare.blp");
			else
				PlayerFrameTexture:SetTexture("Interface\\TargetingFrame\\UI-TargetingFrame-Elite.blp");
			end
		else
			PlayerFrameTexture:SetTexture("Interface\\TargetingFrame\\UI-TargetingFrame.blp");
		end
	
		SmartPlayer_HealthUpdate();
		SmartPlayer_ManaUpdate();

		SmartPlayerPet_HealthUpdate();
		SmartPlayerPet_ManaUpdate();

		if not PartyMemberFrame1:IsUserPlaced() then
			SmartParty_ResetPosition()
		end

		SmartPartyTarget_ResetPosition()
	end
end

function SmartPlayer_HealthUpdate()
	if SMART_PLAYER_Config.statusbartext then
		if SMART_PLAYER_Config.statusbarloss then
			SMART_HealthLoss(SmartPlayerHealthBarText, "player");
		else
			SMART_HealthUpdate(SmartPlayerHealthBarText, "player", SMART_PLAYER_Config.healthwithpercent);
		end
	else
		SmartPlayerHealthBarText:Hide();
	end

	if SMART_PLAYER_Config.percent then
		if SMART_PLAYER_Config.percenttoloss then
			SMART_HealthLoss(SmartPlayerHealthPercent,"player");
		else
			SMART_HealthPercent(SmartPlayerHealthPercent,"player");
		end
	else
		SmartPlayerHealthPercent:Hide();
	end
end

function SmartPlayer_ManaUpdate()
	if SMART_PLAYER_Config.statusbartext and SMART_PLAYER_Config.statusbarmana then
		SMART_ManaUpdate(SmartPlayerManaBarText, "player");
	else
		SmartPlayerManaBarText:Hide();
	end

	if SMART_PLAYER_Config.percent and SMART_PLAYER_Config.percentmana then
		SMART_ManaPercent(SmartPlayerManaPercent, "player");
	else
		SmartPlayerManaPercent:Hide();
	end
end

function SmartPlayerPet_HealthUpdate()
	if SMART_PLAYER_Config.petstatusbartext then
		SMART_HealthUpdate(SmartPetFrameHealthBarText,"pet");
	else
		SmartPetFrameHealthBarText:Hide();
	end
end

function SmartPlayerPet_ManaUpdate()
	if SMART_PLAYER_Config.petstatusbartext and SMART_PLAYER_Config.petstatusbarmana then
		SMART_ManaUpdate(SmartPetFrameManaBarText, "pet");
	else
		SmartPetFrameManaBarText:Hide();
	end
end
