-- This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
-- To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
-- or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
local MereHealingFrames, privateVars = ...

local RoleIcon = {
    RoleFrame = nil,
    RoleInnerFrame = nil,
    RoleText = nil,
    ReadyState = nil,

    RoleIconWidth = 16,
    RoleIconHeight = 16,
    RoleIconBorder = 1,

    parentBorderSize = 1,

    CurrentRole = nil,
    CurrentReadyState = "nil",
    ParentFrame = nil,

    LayoutOffset = 0,

    FramesLoaded = false,
    HealingPanel = nil
}

function RoleIcon:new (settings, ParentFrame, healingPanel)
    if not ParentFrame then
        error("No ParentFrame?")
    end

    local this = settings or {}

    setmetatable(this, self)
    self.__index = self

    this:QueueAsyncFrames(ParentFrame)
    this.HealingPanel = healingPanel
    return this
end

function RoleIcon:QueueAsyncFrames(parentFrame)
    self.ParentFrame = parentFrame

    local framesRequest = {
        Frame = 2,
        Text = 1,
        Texture = 1,
    }

    Library.LibAsyncFrames.AyncLoadFrames(framesRequest,
        function (...) RoleIcon.FramesLoadedCallback(self, ...) end,
        parentFrame, "MereHealingFrames")
end

function RoleIcon:FramesLoadedCallback(frames)
    self.RoleFrame = frames:CreateFrame("Frame", "RoleBorder", self.ParentFrame)
    self.RoleFrame:SetPoint("TOPLEFT", self.ParentFrame, "TOPLEFT", 1, 1)
    self.RoleFrame:SetWidth(self.RoleIconWidth)
    self.RoleFrame:SetHeight(self.RoleIconHeight)

    self.RoleInnerFrame = frames:CreateFrame("Frame", "RoleBackground", self.RoleFrame)
    self.RoleInnerFrame:SetPoint("CENTER", self.RoleFrame, "CENTER")
    self.RoleInnerFrame:SetWidth(self.RoleIconWidth - (self.RoleIconBorder * 2))
    self.RoleInnerFrame:SetHeight(self.RoleIconHeight - (self.RoleIconBorder * 2))

    self.RoleText = frames:CreateFrame("Text", "Role", self.RoleInnerFrame)
    self.RoleText:SetPoint("CENTER", self.RoleInnerFrame, "CENTER")

    self.ReadyState = frames:CreateFrame("Texture", "ReadyState", self.RoleInnerFrame)
    self.ReadyState:SetPoint("TOPLEFT", self.RoleInnerFrame, "TOPLEFT")
    self.ReadyState:SetPoint("BOTTOMRIGHT", self.RoleInnerFrame, "BOTTOMRIGHT")
    self.ReadyState:SetVisible(false)
    self.FramesLoaded = true

    self:SetLayout(self.LayoutOffset)
    self.RoleFrame:SetBackgroundColor(MereHealingFrames.Colours.FetchRGBA("RoleBorder"))
    self:SetRole(self.CurrentRole)
    self:MoveIcon(self.parentBorderSize)
    self.HealingPanel.NameLabel:SetPoint("TOPLEFT", self.RoleFrame, "TOPRIGHT")
end

function RoleIcon:SetLayout(LayoutOffset)
    self.LayoutOffset = LayoutOffset

    if self.FramesLoaded then
        self.RoleFrame:SetLayer(MereHealingFrames.Layers.CalculateLayer(LayoutOffset, "RoleBorder"))
        self.RoleInnerFrame:SetLayer(MereHealingFrames.Layers.CalculateLayer(LayoutOffset, "RoleBackground"))
        self.RoleText:SetLayer(MereHealingFrames.Layers.CalculateLayer(LayoutOffset, "RoleText"))
    end
end

function RoleIcon:MoveIcon(borderWidth)
    self.parentBorderSize = borderWidth
    if self.FramesLoaded then
        self.RoleFrame:SetPoint("TOPLEFT", self.ParentFrame, "TOPLEFT", borderWidth, borderWidth)
    end
end

function RoleIcon:SetRole(role)
    self.CurrentRole = role

    if not self.FramesLoaded then
        return
    end

    if not role then
        self.RoleInnerFrame:SetBackgroundColor(MereHealingFrames.Colours.FetchRGBA("RoleUnknown"))
        self.RoleText:SetText("?")
    elseif (role == "tank") then
        self.RoleInnerFrame:SetBackgroundColor(MereHealingFrames.Colours.FetchRGBA("RoleTank"))
        self.RoleText:SetText("T")
    elseif (role == "heal") then
        self.RoleInnerFrame:SetBackgroundColor(MereHealingFrames.Colours.FetchRGBA("RoleHealer"))
        self.RoleText:SetText("H")
    elseif (role == "dps") then
        self.RoleInnerFrame:SetBackgroundColor(MereHealingFrames.Colours.FetchRGBA("RoleDps"))
        self.RoleText:SetText("D")
    elseif (role == "support") then
        self.RoleInnerFrame:SetBackgroundColor(MereHealingFrames.Colours.FetchRGBA("RoleSupport"))
        self.RoleText:SetText("S")
    end
    self.RoleFrame:SetVisible(true)
end

function RoleIcon:SetReadyStatus(readyState)
    self.CurrentReadyState = readyState

    if not self.FrameLoaded then
        return
    end

    if readyState == "nil" then
        self.ReadyState:SetVisible(false)
        self:SetRole(self.CurrentRole)
        return
    end

    self.RoleText:SetText("")
    self.RoleInnerFrame:SetBackgroundColor(0, 0, 0, 1)
    if readyState then
        self.ReadyState:SetTexture("MereHealingFrames", "graphics\\icons\\tick3.png")
    else
        self.ReadyState:SetTexture("MereHealingFrames", "graphics\\icons\\cross3.png")
    end
    self.ReadyState:SetVisible(true)
end

function MereHealingFrames.HealingPanel:CreateRoleIcon()
    self.RoleIcon = RoleIcon:new({}, self.Frame, self)
end

function MereHealingFrames.HealingPanel:InitializeRoleIcon(LayoutOffset)
    return self.RoleIcon:SetLayout(LayoutOffset)
end

function MereHealingFrames.HealingPanel:MoveRoleIcon(borderWidth)
    return self.RoleIcon:MoveIcon(borderWidth)
end

function MereHealingFrames.HealingPanel:Ready(readyState)
    return self.RoleIcon:SetReadyStatus(readyState)
end