--------------------------------------------
--
-- 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=true};

function SmartPlayer_OnLoad()
	this:RegisterEvent("PLAYER_ENTERING_WORLD");
	this:RegisterEvent("UNIT_COMBAT");
	this:RegisterEvent("UNIT_PET");
	this:RegisterEvent("UNIT_HEALTH");
	this:RegisterEvent("UNIT_MANA");
	this:RegisterEvent("UNIT_RAGE");
	this:RegisterEvent("UNIT_ENERGY");
	this:RegisterEvent("UNIT_FOCUS");
	this:RegisterEvent("UNIT_MAXHEALTH");
	this:RegisterEvent("UNIT_MAXMANA");
	this:RegisterEvent("UNIT_MAXRAGE");
	this:RegisterEvent("UNIT_MAXENERGY");
	this:RegisterEvent("UNIT_MAXFOCUS");
	this:RegisterEvent("UNIT_DISPLAYPOWER");
	this:RegisterEvent("VARIABLES_LOADED");
	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_OnEvent(event)
	if (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();
		if UnitExists("pet") then
			SmartPlayerPet_HealthUpdate();
			SmartPlayerPet_ManaUpdate();
		end
		if not PartyMemberFrame1:IsUserPlaced() then
			SmartParty_ResetPosition();
		end
	elseif (event == "VARIABLES_LOADED") then
		if not PlayerFrame:IsUserPlaced() then
			SmartPlayer_ResetPosition();
		end
		SmartPlayer_PetFramePosition();
	elseif (event == "UNIT_PET") then
		if (arg1 == "player") then
			if UnitExists("pet") then
				SmartPlayerPet_HealthUpdate();
				SmartPlayerPet_ManaUpdate();
			end
			if not PartyMemberFrame1:IsUserPlaced() then
				SmartParty_ResetPosition();
			end
		end
	elseif (event == "UNIT_HEALTH") or (event == "UNIT_MAXHEALTH") then
		if (arg1 == "player") then
			SmartPlayer_HealthUpdate();
		end
		if (arg1 == "pet") then
			SmartPlayerPet_HealthUpdate();
		end
	elseif (event == "UNIT_DISPLAYPOWER") or (event == "UNIT_FOCUS") or (event == "UNIT_MANA") or (event == "UNIT_RAGE") or (event == "UNIT_ENERGY")  or (event == "UNIT_MAXFOCUS") or (event == "UNIT_MAXMANA") or (event == "UNIT_MAXRAGE") or (event == "UNIT_MAXENERGY")then
		if (arg1 == "player") then
			SmartPlayer_ManaUpdate();
		end
		if (arg1 == "pet") then
			SmartPlayerPet_ManaUpdate();
		end
	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
