--[[

	SuperInspect: ---------
		copyright 2005-2006 by Chester

]]

SUPERINSPECT_VERSION = GetAddOnMetadata("SuperInspect", "Version");

BINDING_HEADER_SUPERINSPECT = "Super Inspect";
BINDING_NAME_SUPERINSPECT = "观察目标";

SuperInspect = {};

UIPanelWindows["SuperInspectFrame"] = { area = "left", pushable = 0 };

function SuperInspectFrame_Load_OnLoad()
	this:RegisterEvent("VARIABLES_LOADED");
	this:RegisterEvent("ADDON_LOADED");
	
	SLASH_SUPERINSPECT1 = "/superinspect";
	SLASH_SUPERINSPECT2 = "/si";
	SlashCmdList["SUPERINSPECT"] = function(msg)
		SuperInspect_SlashCommand(msg);
	end	
end


function SuperInspectFrame_Load_OnEvent()
	if( event == "VARIABLES_LOADED" ) then
		if( DEFAULT_CHAT_FRAME ) then
			DEFAULT_CHAT_FRAME:AddMessage("|cffffd200SuperInspect |cffffff00v"..SUPERINSPECT_VERSION.."已加载.");
			if (not IsAddOnLoaded("SuperInspect_UI")) then
				--DEFAULT_CHAT_FRAME:AddMessage("|cffffd200SuperInspect is not loaded");
			end
		end
		if (not SI_Save) then
			SI_Save = { };
			SI_Save.default = 1;
		end

		InspectUnit = SuperInspect_InspectTargetHooked;
		InspectFrame_OnUpdate = SuperInspect_InspectFrame_Nothing;
		InspectTitleText = SI_DummyText;
		
		-- keeps the inspect selection from greying out on the targetframe dropdown
		UnitPopupButtons["INSPECT"] = { text = TEXT(INSPECT), dist = 0 };
	end
	if( event == "ADDON_LOADED" and arg1 == "SuperInspect_UI") then
		--DEFAULT_CHAT_FRAME:AddMessage("|cffffd200SuperInspect is loaded!");
		SuperInspectFrame.uiScale = SuperInspect_GetUIScale();
		if (SI_Save.framepos_L or SI_Save.framepos_T) then
			SuperInspectFrame:ClearAllPoints();
			SuperInspectFrame:SetPoint("TOPLEFT", "UIParent", "BOTTOMLEFT", SI_Save.framepos_L, SI_Save.framepos_T);
		else
			SuperInspectFrame:ClearAllPoints();
			SuperInspectFrame:SetPoint("CENTER", "UIParent", "CENTER", 0, 0);
			SI_Save.framepos_L = SuperInspectFrame:GetLeft();
			SI_Save.framepos_T = SuperInspectFrame:GetTop();
		end
	end
end

function SI_AddMessage( text )
	if (not text) then
		return;	
	end
	if (SI_Save.debug) then
		ChatFrame3:AddMessage(GREEN_FONT_COLOR_CODE..""..text.."");
	end
end

function SuperInspect_GetUIScale()
	local uiScale;
	if ( GetCVar("useUiScale") == "1" ) then
		uiScale = tonumber(GetCVar("uiscale"));
	else
		uiScale = 1.0;
	end
	return uiScale;
end

function SuperInspect_GetEffectiveScale(frame)
	return frame:GetEffectiveScale()
end

function SuperInspect_SetEffectiveScale(frame, scale, parentframe)
	frame.scale = scale;  -- Saved in case it needs to be re-calculated when the parent's scale changes.
	local parent = getglobal(parentframe);
	if ( not parent ) then
		parent = getglobal("UIParent");
	end
	scale = scale / parent:GetEffectiveScale()
	frame:SetScale(scale);
end

function SuperInspect_SlashCommand(msg)
	if( not msg or msg == "" ) then
		DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect 命令:");
		DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20/superinspect scale <数字> |cff3366ff- 在0.25与2.0比例之间缩放观察窗口 (默认为0.75)");
		DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20/superinspect reset |cff3366ff- 将观察窗口重新放置在屏幕的正中");
		DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20/superinspect defaulttoggle |cff3366ff- 打开/关闭默认观察模式(打开此选项时, SI将与WOW默认观察窗口保持一样位置和大小. 关闭此选项时, 你可以拖动并调整观察窗口的大小)");
		DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20/superinspect itembgtoggle |cff3366ff- 切换物品质量的显示模式");
		DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20/superinspect durabilitytoggle |cff3366ff- 当观察自己时,显示/隐藏装备耐久度信息");
		DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20/superinspect sound |cff3366ff- 窗口显示和关闭时 打开/关闭 声音提示");
		DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20/superinspect playercache |cff3366ff- 启用或禁用玩家缓存");
		return;
	end
	if (string.find(msg, "scale") ~= nil) then
		for scale in string.gfind(msg, "scale%s(%d.*)") do
			SuperInspect.s = tonumber(scale);
		end
		if (SuperInspect.s) then
			if (SuperInspect.s > 2) then
				SuperInspect.s = 2;
			elseif (SuperInspect.s < 0.25) then
				SuperInspect.s = 0.25;
			end
			if (not IsAddOnLoaded("SuperInspect_UI")) then
				UIParentLoadAddOn("SuperInspect_UI");
			end
			SuperInspectFrame:SetScale(3);
			SuperInspect_SetEffectiveScale(SuperInspectFrame, SuperInspect.s, "UIParent");
			--SuperInspect_ModelFrame:SetScale(SuperInspectFrame:GetEffectiveScale());
			SuperInspect_ResetFrame();
			SuperInspect_ModelFrame:RefreshUnit();
			SI_Save.scale = SuperInspect.s;
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: SuperInspect观察窗口缩放比例设置为:"..SuperInspect.s);
		else
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: 请在 'scale' 后输入要缩放的比例(合法的缩放比例: 0.5-2.0, 例如: /superinspect scale 0.8)");
		end
		return;
--/script CSG_Main_Frame:SetScale(UIParent:GetScale() * 0.75);
	end
	if( msg == "reset" ) then
		if (not UIParentLoadAddOn("SuperInspect_UI")) then
			return; 
		end
		SuperInspect_ResetFrame();
		DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: 已将观察窗口重新放置在屏幕的正中");
	end
	if (msg == "playercache") then
		if (not UIParentLoadAddOn("SuperInspect_UI")) then
			return; 
		end	
		if (SI_Save.playercache) then
			SI_Save.playercache = false;
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: Disabling Playercache");
		else
			SI_Save.playercache = true;
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: Enabling Playercache");
		end			
	end
	if( msg == "defaulttoggle" ) then
		if (not UIParentLoadAddOn("SuperInspect_UI")) then
			return; 
		end
		if (SI_Save.default) then
			SI_Save.default = nil;
			SuperInspect_ResetFrame();
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: 暴雪默认模式已关闭 (可以移动和缩放)");						
		else
			SI_Save.default = 1;
			SuperInspect_ResetFrame();
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: 暴雪默认模式已开启 (大小和位置已锁定)");			
		end
	end
	if( msg == "itembgtoggle" ) then
		if (not UIParentLoadAddOn("SuperInspect_UI")) then
			return; 
		end
		if (not SI_Save.itembg) then
			SI_Save.itembg = 1;
			if (SuperInspectFrame:IsVisible()) then
				SuperInspectFrame:Hide();
				SuperInspectFrame:Show();
			end
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: item quality art is now using TABS");
		elseif (SI_Save.itembg == 1) then
			SI_Save.itembg = 2;
			if (SuperInspectFrame:IsVisible()) then
				SuperInspectFrame:Hide();
				SuperInspectFrame:Show();
			end
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: 关闭物品质量颜色显示");						
		else
			SI_Save.itembg = nil;
			if (SuperInspectFrame:IsVisible()) then
				SuperInspectFrame:Hide();
				SuperInspectFrame:Show();
			end
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: item quality art is now using BORDERS");	
		end
	end
	--SI_Save.durabilityoff
	if( msg == "durabilitytoggle" ) then
		if (not UIParentLoadAddOn("SuperInspect_UI")) then
			return; 
		end

		if (SI_Save.durabilityoff) then
			SI_Save.durabilityoff = nil;
			SuperInspect_ResetFrame();
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: 显示耐久度信息");						
		else
			SI_Save.durabilityoff = 1;
			SuperInspect_ResetFrame();
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: 隐藏耐久度信息");	
		end
	end
	if( msg == "sound" ) then
		if (not UIParentLoadAddOn("SuperInspect_UI")) then
			return; 
		end
		if (SI_Save.snd) then
			SI_Save.snd = nil;
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: 关闭声音提示");						
		else
			SI_Save.snd = 1;
			PlaySound("igCharacterInfoOpen");
			DEFAULT_CHAT_FRAME:AddMessage("|cff20ff20SuperInspect: 打开声音提示");	
		end
	end
end

function SuperInspect_InspectFrame_Nothing()
	--what
end

function SuperInspect_InspectTarget(unit)
	if (not UIParentLoadAddOn("SuperInspect_UI")) then
		--UIParentLoadAddOn("SuperInspect_UI");
		--SuperInspect_InspectTarget(unit);
		return; 
	end
	if (not unit) then
		unit = "target";	
	end
	if (SuperInspectFrame:IsVisible()) then
		HideUIPanel(SuperInspectFrame);
		SuperInspect.isVisible = nil;
		return;
	else
		if (not UnitExists("target")) then
			TargetUnit("player");
		end
		SuperInspect_UpdateModel(unit);	
	end
end

function SuperInspect_InspectTargetHooked(unit)
	if (not UIParentLoadAddOn("SuperInspect_UI")) then
		return; 
	end
	if (not unit) then
		unit = "target";	
	end
	if (not UnitExists("target")) then
		TargetUnit("player");
	end
	SuperInspect_UpdateModel(unit);	
end

