-- ----------------------------------------------------------------------------
-- GridIndicatorCornerText by Slaren (based on kunda's GridIndicatorText3)
-- ----------------------------------------------------------------------------
local L = AceLibrary("AceLocale-2.2"):new("GridIndicatorCornerText")
local AceOO = AceLibrary("AceOO-2.0")
local media = LibStub("LibSharedMedia-3.0", true)

GridIndicatorCornerText = GridStatus:NewModule("GridIndicatorCornerText")
GridIndicatorCornerText.defaultDB = {
	fontSize = 8,
	font = "Friz Quadrata TT",
}

local indicators = GridFrame.frameClass.prototype.indicators
table.insert(indicators, { type = "tcorner1", order = 8.5, name = L["Top Left Corner Text"] })
table.insert(indicators, { type = "tcorner2", order = 9.5, name = L["Top Right Corner Text"] })
table.insert(indicators, { type = "tcorner3", order = 10.5, name = L["Bottom Left Corner Text"] })
table.insert(indicators, { type = "tcorner4", order = 11.5, name = L["Bottom Right Corner Text"] })

local statusmap = GridFrame.db.profile.statusmap
if not statusmap["tcorner1"] then
	statusmap["tcorner1"] = {}
	statusmap["tcorner2"] = {}
	statusmap["tcorner3"] = {}
	statusmap["tcorner4"] = {}
end

local GridIndicatorCornerTextFrameClass = AceOO.Class(GridFrame.frameClass)

local _frameClass = nil
if not _frameClass then
	_frameClass = GridFrame.frameClass
	GridFrame.frameClass = GridIndicatorCornerTextFrameClass
end

function GridIndicatorCornerTextFrameClass.prototype:CreateIndicator(indicator)
	GridIndicatorCornerTextFrameClass.super.prototype.CreateIndicator(self, indicator)
	local f = self.frame

  if indicator == "tcorner1" or indicator == "tcorner2" or indicator == "tcorner3" or indicator == "tcorner4" then
    if(not f.CornerText) then
      f.CornerText = {}
    end
  end

	if indicator == "tcorner1" then
    f.CornerText[indicator] = GridIndicatorCornerText:CreateIndicatorFontString(f)
		f.CornerText[indicator]:SetJustifyH("LEFT")
		f.CornerText[indicator]:SetJustifyV("TOP")
	elseif indicator == "tcorner2" then
    f.CornerText[indicator] = GridIndicatorCornerText:CreateIndicatorFontString(f)
		f.CornerText[indicator]:SetJustifyH("RIGHT")
		f.CornerText[indicator]:SetJustifyV("TOP")
	elseif indicator == "tcorner3" then
    f.CornerText[indicator] = GridIndicatorCornerText:CreateIndicatorFontString(f)
		f.CornerText[indicator]:SetJustifyH("LEFT")
		f.CornerText[indicator]:SetJustifyV("BOTTOM")
	elseif indicator == "tcorner4" then
    f.CornerText[indicator] = GridIndicatorCornerText:CreateIndicatorFontString(f)
		f.CornerText[indicator]:SetJustifyH("RIGHT")
		f.CornerText[indicator]:SetJustifyV("BOTTOM")
	end
end

function GridIndicatorCornerText:CreateIndicatorFontString(f)
  local fs = f.Bar:CreateFontString(nil, "ARTWORK")
  local font = media and media:Fetch("font", GridIndicatorCornerText.db.profile.font) or STANDARD_TEXT_FONT
  fs:SetFontObject(GameFontHighlightSmall)
  fs:SetFont(font, GridIndicatorCornerText.db.profile.fontSize)
  fs:SetPoint("TOPRIGHT", f, "TOPRIGHT", 0, 0)
  fs:SetPoint("BOTTOMLEFT", f, "BOTTOMLEFT", 0, 0)
  fs:Hide()
  return fs
end

function GridIndicatorCornerText:OnEnable()
	local opt = GridFrame.options.args.advanced.args
	opt["cornertext"] = {
		type = "group",
		name = L["Corner Text"],
		desc = L["Corner Text options."],
		order = 110,
		args = {
			["cornertextfontsize"] = {
				type = "range",
				name = L["Corner Text Font Size"],
				desc = L["Adjust the font size for Corner Text."],
				order = 111,
				min = 6,
				max = 24,
				step = 1,
				get = function ()
					return GridIndicatorCornerText.db.profile.fontSize
				end,
				set = function (v)
					GridIndicatorCornerText.db.profile.fontSize = v
					local font = media and media:Fetch('font', GridIndicatorCornerText.db.profile.font) or STANDARD_TEXT_FONT
					GridFrame:WithAllFrames(function (f) f:SetFrameFont(font, v, "cornertext") end)
				end,
			}
		}
	}
	if media then
		opt["cornertext"].args["font"] = {
			type = "text",
			name = L["Corner Text Font"],
			desc = L["Adjust the font setting for Corner Text."],
			order = 112,
			validate = media:List("font"),
			get = function ()
				return GridIndicatorCornerText.db.profile.font
			end,
			set = function (v)
				GridIndicatorCornerText.db.profile.font = v
				local font = media:Fetch("font", v)
				local fontsize = GridIndicatorCornerText.db.profile.fontSize
				GridFrame:WithAllFrames(function (f) f:SetFrameFont(font, fontsize, "cornertext") end)
			end,
		}
	end
end

function GridIndicatorCornerTextFrameClass.prototype:SetIndicator(indicator, color, text, value, maxValue, texture, start, duration, stack)
	GridIndicatorCornerTextFrameClass.super.prototype.SetIndicator(self, indicator, color, text, value, maxValue, texture, start, duration, stack)
	local f = self.frame
	if indicator == "tcorner1" or indicator == "tcorner2" or indicator == "tcorner3" or indicator == "tcorner4" then
		if not f.CornerText or not f.CornerText[indicator] then
			self:CreateIndicator(indicator)
		end
		local text = text:utf8sub(1, GridFrame.db.profile.textlength)
		f.CornerText[indicator]:SetText(text)
		if text and text ~= "" then
			f.CornerText[indicator]:Show()
		else
			f.CornerText[indicator]:Hide()
		end
		if color then
			f.CornerText[indicator]:SetTextColor(color.r, color.g, color.b, color.a)
		end
	end
end

function GridIndicatorCornerTextFrameClass.prototype:ClearIndicator(indicator)
	GridIndicatorCornerTextFrameClass.super.prototype.ClearIndicator(self, indicator)
	local f = self.frame
	if indicator == "tcorner1" or indicator == "tcorner2" or indicator == "tcorner3" or indicator == "tcorner4" then
		if f.CornerText and f.CornerText[indicator] then
			f.CornerText[indicator]:Hide()
		end
	end
end

function GridIndicatorCornerTextFrameClass.prototype:SetFrameFont(font, size, check)
	if check and check == "cornertext" then
		local f = self.frame
		if f.CornerText then
      for n=1, 4 do
        if f.CornerText["tcorner"..n] then
        	f.CornerText["tcorner"..n]:SetFont(font, size)
        end
      end
		end
	else
		GridIndicatorCornerTextFrameClass.super.prototype.SetFrameFont(self, font, size, check)
	end
end