local E, L, V, P, G = unpack(ElvUI);
local UF = E:GetModule("UnitFrames");

local CreateFrame = CreateFrame;

function UF:Construct_Portrait(frame, type)
	local portrait;
	if(type == "texture") then
		local backdrop = CreateFrame("Frame", nil, frame);
		portrait = frame:CreateTexture(nil, "OVERLAY");
		portrait:SetTexCoord(0.15, 0.85, 0.15, 0.85);
		E:SetOutside(backdrop, portrait);
		backdrop:SetFrameLevel(frame:GetFrameLevel());
		E:SetTemplate(backdrop, "Default");
		portrait.backdrop = backdrop;
	else
		portrait = CreateFrame("PlayerModel", nil, frame);
		E:CreateBackdrop(portrait, "Default", nil, nil, self.thinBorders, true)
	end

	portrait.PostUpdate = self.PortraitUpdate;

	portrait.overlay = CreateFrame("Frame", nil, frame);
	portrait.overlay:SetFrameLevel(frame.Health:GetFrameLevel() + 5);

	return portrait;
end

function UF:Configure_Portrait(frame, dontHide)
	if(not frame.VARIABLES_SET) then return; end
	local db = frame.db;
	if(frame.Portrait and not dontHide) then
		frame.Portrait:Hide();
		frame.Portrait:ClearAllPoints();
		frame.Portrait.backdrop:Hide();
	end
	frame.Portrait = db.portrait.style == "2D" and frame.Portrait2D or frame.Portrait3D;

	local portrait = frame.Portrait;
	if(frame.USE_PORTRAIT) then
		if(not frame:IsElementEnabled("Portrait")) then
			frame:EnableElement("Portrait", frame.unit);
		end

		local color = E.db.unitframe.colors.borderColor
		portrait.backdrop:SetBackdropBorderColor(color.r, color.g, color.b)

		portrait:ClearAllPoints();
		portrait.backdrop:ClearAllPoints();
		if(frame.USE_PORTRAIT_OVERLAY) then
			if(db.portrait.style == "3D") then
				portrait:SetFrameLevel(frame.Health:GetFrameLevel() + 1);
			else
				portrait:SetParent(frame.Health);
			end

			--portrait:SetAllPoints(frame.Health);
			portrait:SetWidth(frame.Health:GetWidth())
			portrait:SetHeight(frame.Health:GetHeight())
			portrait:SetPoint("TOPLEFT", frame.Health, "TOPLEFT", 0, -frame.BORDER)

			portrait:SetAlpha(0.35);
			if(not dontHide) then
				portrait:Show();
			end
			portrait.backdrop:Hide();
		else
			portrait:SetAlpha(1);
			if(not dontHide) then
				portrait:Show();
			end
			portrait.backdrop:Show();
			if(db.portrait.style == "3D") then
				portrait:SetFrameLevel(frame.Health:GetFrameLevel() - 4);
			else
				portrait:SetParent(frame.Health);
			end

			if(frame.ORIENTATION == "LEFT") then
				portrait.backdrop:SetPoint("TOPLEFT", frame, "TOPLEFT", frame.SPACING, frame.USE_MINI_CLASSBAR and -(frame.CLASSBAR_YOFFSET+frame.SPACING) or -frame.SPACING);

				if(frame.USE_MINI_POWERBAR or frame.USE_POWERBAR_OFFSET or not frame.USE_POWERBAR or frame.USE_INSET_POWERBAR or frame.POWERBAR_DETACHED) then
					portrait.backdrop:SetPoint("BOTTOMRIGHT", frame.Health.backdrop, "BOTTOMLEFT", frame.BORDER - frame.SPACING*3, 0);
				else
					portrait.backdrop:SetPoint("BOTTOMRIGHT", frame.Power.backdrop, "BOTTOMLEFT", frame.BORDER - frame.SPACING*3, 0);
				end
			elseif(frame.ORIENTATION == "RIGHT") then
				portrait.backdrop:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -frame.SPACING, frame.USE_MINI_CLASSBAR and -(frame.CLASSBAR_YOFFSET+frame.SPACING) or -frame.SPACING);

				if(frame.USE_MINI_POWERBAR or frame.USE_POWERBAR_OFFSET or not frame.USE_POWERBAR or frame.USE_INSET_POWERBAR or frame.POWERBAR_DETACHED) then
					portrait.backdrop:SetPoint("BOTTOMLEFT", frame.Health.backdrop, "BOTTOMRIGHT", -frame.BORDER + frame.SPACING*3, 0);
				else
					portrait.backdrop:SetPoint("BOTTOMLEFT", frame.Power.backdrop, "BOTTOMRIGHT", -frame.BORDER + frame.SPACING*3, 0);
				end
			end

			E:SetInside(portrait, portrait.backdrop, frame.BORDER);
		end
	else
		if(frame:IsElementEnabled("Portrait")) then
			frame:DisableElement("Portrait");
			portrait:Hide();
			portrait.backdrop:Hide();
		end
	end
end

function UF:PortraitUpdate()
	local db = self:GetParent().db;
	if(not db) then return; end

	local portrait = db.portrait;
	if(portrait.enable and self:GetParent().USE_PORTRAIT_OVERLAY) then
		self:SetAlpha(0);
		self:SetAlpha(0.35);
	else
		self:SetAlpha(1)
	end
end