-- ---------------------------------------------------
-- Tidy Plates: Grey - Theme Definition
-- ---------------------------------------------------

--[[ 
To Do: 
	* Iconlist
		- Acidmaw/Dreadscale, etc.
		- "Watch your step"
		- Healer, Melee, Ranged
		- Faction Champs
--]]

--[[
local function GetPlayerThreat()
	if not UnitExists("mouseover") then return end
	local _, group, size, index, unitid
	local playerthreat, leaderthreat, tempthreat = 0,0,0
	
	_, _,  playerthreat, _, _ = UnitDetailedThreatSituation("player", "mouseover")
	if HasPetUI() then _, _,  leaderthreat, _, _ = UnitDetailedThreatSituation("pet", "mouseover") end
	
	if UnitInRaid("player") then group = "raid"; size = GetNumRaidMembers() - 1
	elseif UnitInParty("player") then group = "party"; size = GetNumPartyMembers()
	else group = nil end
	if group then
		for index = 1, size do
			unitid = group..index
			_, _, tempthreat , _, _ = UnitDetailedThreatSituation(unitid, "mouseover")
			if tempthreat and tempthreat > leaderthreat then leaderthreat = tempthreat end
		end
	end
	
	if playerthreat and leaderthreat then
		if playerthreat == 100 then return playerthreat + (100-leaderthreat)
		else return playerthreat end
	else return nil end
end

GameTooltip:HookScript("OnTooltipSetUnit", function(self) TidyPlates:ForceUpdate() end)

local function OnInitializeDelegate(plate)
	plate.bars.health:RegisterEvent("CURSOR_UPDATE")
	plate.bars.health:RegisterEvent("PLAYER_TARGET_CHANGED")
	plate.bars.health:RegisterEvent("UNIT_TARGET")
	plate.ThreatFrame = CreateFrame("Frame", nil, plate)
	plate.ThreatFrame.ThreatText = plate.ThreatFrame:CreateFontString(nil, 'ARTWORK', 'GameFontNormalLarge')
	plate.ThreatFrame.ThreatText:SetPoint("BOTTOM",plate,"TOP")
	plate.ThreatFrame:Hide()
 end
 
local function SetFade(frame)
	if GetTime() > frame.FadeTime then
		frame:Hide()
		frame:SetScript("OnUpdate", nil)
	end
end

local function trunc(value)
	if not value then return 0 end
	if value >= 1000000 then return format('%.1fm', value / 1000000)
	elseif value >= 1000 then return format('%.1fk', value / 1000)
	else return value end
end

local function OnUpdateDelegate(plate, unit) 
	if unit.isMouseover then 
		plate.ThreatFrame:Show()
		local playerthreat = GetPlayerThreat()
		plate.ThreatFrame.ThreatText:SetText(trunc(playerthreat))
		plate.ThreatFrame.FadeTime = GetTime() + 3
		plate.ThreatFrame:SetScript("OnUpdate", SetFade)
	end
end

--]] 

SetCVar("threatWarning", 3)
SetCVar("ShowClassColorInNameplate", 1)
SetCVar("CameraDistanceMaxFactor", 6)

local path = "Interface\\Addons\\TidyPlates_StandardGrey"
local TANK_MODE, DPS_MODE, PVP_MODE = 2, 3, 4
local HEALTH_PCT, HEALTH_TOTAL, HEALTH_DEF = 3, 3, 4

if not TidyPlatesGreyVariables then TidyPlatesGreyVariables = {} end
TidyPlatesGreyVariables = {Mode = 1, HealthText = 1, AutoHide = false}

local function valueToString(value)
	if value >= 1000000 then return format('%.1fm', value / 1000000)
	elseif value >= 1000 then return format('%.1fk', value / 1000)
	else return value end
end

local function SpellTextDelegate(unit)
	local spellname
	if unit.isCasting then 
		spellname = UnitCastingInfo("target") or UnitChannelInfo("target")
		return spellname
	else return "" end
end

local function HealthTextDelegate(unit)
	if TidyPlatesGreyVariables.HealthText == HEALTH_PCT then
		if unit.health ~= unit.healthmax then 
			return "%"..ceil(100*(unit.health/unit.healthmax))
		else return nil end
	elseif TidyPlatesGreyVariables.HealthText == HEALTH_TOTAL then
		return valueToString(unit.health)
	elseif TidyPlatesGreyVariables.HealthText == HEALTH_DEF then
		if unit.health ~= unit.healthmax then return "-"..valueToString(unit.healthmax - unit.health) end
	else return "" end
end

local function ArtDelegate(unit)
	if TidyPlatesGreyVariables.Mode == 4 then
		if unit.class and (unit.class ~= "UNKNOWN") then 
			return path.."\\Art\\"..unit.class  
		end
	else return nil end
end		

local function ScaleDelegate(unit)
	local basescale = 1
	if InCombatLockdown() then
		-- Set Scale for Tank Mode
		if TidyPlatesGreyVariables.Mode == TANK_MODE and unit.reaction == "HOSTILE" and InCombatLockdown() then
			if (not unit.isElite) then basescale = .7 end
			if unit.threatSituation == "LOW" then return basescale * 1.2
			elseif unit.isCasting then return basescale
			elseif unit.threatSituation == "HIGH" then return basescale * 0.85 end
		-- Set Scale for DPS Mode
		elseif TidyPlatesGreyVariables.Mode == DPS_MODE then
			if (not unit.isElite) then basescale = .8 end
			if unit.threatSituation == "HIGH" then return basescale * 1.2
			elseif unit.isCasting then return basescale
			elseif unit.threatSituation == "LOW" then return basescale * .9 end
		elseif TidyPlatesGreyVariables.Mode == PVP_MODE then end
	end
	return 1	
end

local function HealthbarColorDelegate(unit)
	if (not unit.isElite) then return unit.red/2, unit.green/2, unit.blue/2 
	else return unit.red, unit.green, unit.blue end
end

local function AlphaDelegate(unit)
	local basealpha = 1
	if unit.isCasting or unit.isMarked then return 1 end
	
	if InCombatLockdown() then
		-- Mode Filters
		if TidyPlatesGreyVariables.Mode == TANK_MODE then
			if (not unit.isElite) then basealpha = .4 end
			if unit.threatSituation == "HIGH" then return basealpha * 0.8 end
		elseif TidyPlatesGreyVariables.Mode == DPS_MODE then
			if (not unit.isElite) then basealpha = .4 end
			if unit.threatSituation == "LOW" then return basealpha * 0.8 end
		end
		
		-- Basic Filters
		if unit.reaction == "NEUTRAL" and unit.threatSituation == "LOW" then return 0 
		else return basealpha end
	else return 1 end
end


local themedata = {
	options = {
		showSpecialText = true,
		showSpecialText2 = true,
		showSpecialArt = true,
	},
	SetSpecialText = HealthTextDelegate,
	SetSpecialText2 = SpellTextDelegate,
	SetSpecialArt = ArtDelegate,
	SetScale = ScaleDelegate,
	SetAlpha = AlphaDelegate,
	--OnUpdate = OnUpdateDelegate,
	--OnInitialize = OnInitializeDelegate,
}

TidyPlatesThemeList["Standard Grey"] = themedata

------------------------------------------------------------------
-- Panel
------------------------------------------------------------------

-- Main Panel
local panel = PanelHelpers:CreatePanelFrame( "TidyPlatesGreyInterfaceOptions", "Grey Theme" )
panel.parent = "Tidy Plates"

---- MODE 
panel.radiobuttonlabel = panel:CreateFontString(nil, 'ARTWORK', 'GameFontNormal')
panel.radiobuttonlabel:SetPoint("TOPLEFT", panel, "TOPLEFT", 35, -65)
panel.radiobuttonlabel:SetText("Display Mode:")
local radiolist = {"Default: |cFFFFFFFF No Scaling or extra art.",
		"Tank: |cFFFFFFFF Reduces visibility of units that are attacking you, preventing information overload. ",
		"Damage: |cFFFFFFFF Increases visibility of units that are attacking you.", 
		"PVP: |cFFFFFFFF  Shows enemy class icon on the nameplate. "}
panel.radiobuttons = PanelHelpers:CreateRadioButtons("TidyPlatesDefault_ModeRadio", panel, 4, 1, 20, radiolist)
panel.radiobuttons[1]:SetPoint("TOPLEFT", panel.radiobuttonlabel, 0, -17)

---- HEALTH TEXT
panel.dropdownmenulabel = panel:CreateFontString(nil, 'ARTWORK', 'GameFontNormal')
panel.dropdownmenulabel:SetPoint("TOPLEFT", panel, "TOPLEFT", 35, -225)
panel.dropdownmenulabel:SetText("Health Text:")
local dropmenu = { 
				{ text = "None", notCheckable = 1 },
				{ text = "Percent", notCheckable = 1 } ,
				{ text = "Total Health", notCheckable = 1 } ,
				{ text = "Health Deficit", notCheckable = 1 },
			}
panel.dropdown = PanelHelpers:CreateDropdownFrame("TidyPlatesDefault_HealthTextDropdown", panel, dropmenu, 1)
panel.dropdown:SetPoint("TOPLEFT", panel.dropdownmenulabel, 0, -17)
panel.dropdown:SetValue(2)

-- Functions
panel.okay = function () 
	TidyPlatesGreyVariables.Mode = panel.radiobuttons:GetChecked()
	TidyPlatesGreyVariables.HealthText = panel.dropdown.Value
	TidyPlates:ForceUpdate()
end

panel.refresh = function ()
	panel.radiobuttons:SetChecked(TidyPlatesGreyVariables.Mode)
	panel.dropdown:SetValue(TidyPlatesGreyVariables.HealthText)
end

panel:SetScript("OnEvent", function(self, event, ...) InterfaceOptions_AddCategory(panel) end)
panel:RegisterEvent("PLAYER_LOGIN")

--------------------------------------------  Slash Commands  --------------------------------------------
TidyPlatesGreySlashCommands = {}
TidyPlatesGreySlashCommands.tank = function() 
	print("Tank")
		TidyPlatesGreyVariables.Mode = TANK_MODE
		TidyPlates:ForceUpdate()
end

TidyPlatesGreySlashCommands.dps = function() 
end

--------------------------------------------  Slash Commands  --------------------------------------------
function slash_TidyGrey(arg)
	if type(TidyPlatesGreySlashCommands[arg]) == 'function' then 
		TidyThreatCommands[arg]() 
	else ChatFrame1:AddMessage('Tidy Plates: Grey') end
end

SLASH_TIDYGREY1 = '/tidygrey'
SlashCmdList['TIDYGREY'] = slash_TidyGrey;













