----------------------------------------------------------------
--	Locals
----------------------------------------------------------------
--~ local Aura = AceLibrary("SpecialEvents-Aura-2.0")
local L
if GRIDMENDING_LOCALS then
	L = setmetatable(GRIDMENDING_LOCALS, {
		__index = function(self, key)
			DEFAULT_CHAT_FRAME:AddMessage("Attention! GridMending is missing a translation for \""..key.."\".")
			rawset(self, key, key)
			return key
		end
	})
	GRIDMENDING_LOCALS = nil
else
	L = setmetatable({}, {
		__index = function(self, key)
			rawset(self, key, key)
			return key
		end
	})
end

----------------------------------------------------------------
--	Module
----------------------------------------------------------------
GridMending = GridStatus:NewModule("GridMending")

local mod = GridMending
mod.menuName = L["Prayer of Mending"]
mod.options = true
mod.defaultDB = {
	alert_pom = {
		text = L["Prayer of Mending"],
		enable = true,
		color = { r = 0, g = 0, b = 0, a = 1 },
		priority = 98,
		range = true,
	},
    selfonly = false,
    PoMcolors = {
        [1] = { r = 1,  g = 1,  b = .4, a = 1 },
        [2] = { r = 1,  g = .5, b = .2, a = 1 },
        [3] = { r = 1,  g = .2, b = .2, a = 1 },
        [4] = { r = 1,  g = .3, b = .7, a = 1 },
        [5] = { r = .6, g = 0,  b = 1,  a = 1 },
    }
}
local PoMoptions = {
    {
        type = 'toggle',
        name = L["Show only your PoM"],
        desc = L["Show only your PoM"],
        get = function()
            return GridMending.db.profile.selfonly
        end,
        set = function(v)
            GridMending.db.profile.selfonly = v
        end,
        map = { [false] = "Disabled", [true] = "Enabled" }
    },
	{
		type = "color",
		name = L["5"],
		desc = L["5 charges color"],
        hasAlpha = true,
        get = function ()
                    local s = GridMending.db.profile.PoMcolors[5]
                    return s.r, s.g, s.b, s.a
		      end,
		set = function (r, g, b, a)
                    local s = GridMending.db.profile.PoMcolors[5]
                    s.r, s.g, s.b, s.a = r, g, b, a;
		      end,
	},
    {
		type = "color",
		name = L["4"],
		desc = L["4 charges color"],
        hasAlpha = true,
        get = function ()
                    local s = GridMending.db.profile.PoMcolors[4]
                    return s.r, s.g, s.b, s.a
		      end,
		set = function (r, g, b, a)
                    local s = GridMending.db.profile.PoMcolors[4]
                    s.r, s.g, s.b, s.a = r, g, b, a;
		      end,
	},
    {
		type = "color",
		name = L["3"],
		desc = L["3 charges color"],
        hasAlpha = true,
        get = function ()
                    local s = GridMending.db.profile.PoMcolors[3]
                    return s.r, s.g, s.b, s.a
		      end,
		set = function (r, g, b, a)
                    local s = GridMending.db.profile.PoMcolors[3]
                    s.r, s.g, s.b, s.a = r, g, b, a;
		      end,
	},
    {
		type = "color",
		name = L["2"],
		desc = L["2 charges color"],
        hasAlpha = true,
        get = function ()
                    local s = GridMending.db.profile.PoMcolors[2]
                    return s.r, s.g, s.b, s.a
		      end,
		set = function (r, g, b, a)
                    local s = GridMending.db.profile.PoMcolors[2]
                    s.r, s.g, s.b, s.a = r, g, b, a;
		      end,
	},
    {
		type = "color",
		name = L["1"],
		desc = L["1 charges color"],
        hasAlpha = true,
        get = function ()
                    local s = GridMending.db.profile.PoMcolors[1]
                    return s.r, s.g, s.b, s.a
		      end,
		set = function (r, g, b, a)
                    local s = GridMending.db.profile.PoMcolors[1]
                    s.r, s.g, s.b, s.a = r, g, b, a;
		      end,
	},
}
--~ /script GridMending.db.profile.alert_pom.color = GridMending.db.profile.PoMcolors[5]
function mod:OnInitialize()
	self.super.OnInitialize(self)
	self:RegisterStatus("alert_pom", L["Prayer of Mending"], PoMoptions, true)
end

function mod:OnEnable()
	self:RegisterEvent("SpecialEvents_UnitBuffGained", "UnitAuraChanged")
	self:RegisterEvent("SpecialEvents_UnitBuffLost", "UnitAuraChanged")
end

function mod:CheckPoM(unit)
local C = nil;
local D = nil;
    for i=1, 40 do
        local name, rank, iconTexture, count, duration, timeLeft = UnitBuff(unit,i);
        if name then
            if name == L["Prayer of Mending"] then
                C = count;
                D = duration;
                break;
            end
        else break;
        end
    end
return C, D
end

function mod:UnitAuraChanged(unitid)
--~     local d = 0
--~     local index, count = Aura:UnitHasBuff(unitid, "Prayer of Mending")
--~     if count then d = 1; end
--~ 	if d > 0 then
--~         local settings = self.db.profile.alert_pom
--~         settings.color = PoMcolors[count]
--~ 		self.core:SendStatusGained(UnitName(unitid), "alert_pom", settings.priority, (settings.range and 40), settings.color, settings.text)
--~ 		return
--~ 	else
--~ 		self.core:SendStatusLost(UnitName(unitid), "alert_pom")
--~ 	end
    local d = 0
    local count, duration = self:CheckPoM(unitid);
    if count then
        if self.db.profile.selfonly == false then
            d = 1;
        else
            if duration then
                d = 1;
            end
        end
    end
	if d > 0 then
        local settings = self.db.profile.alert_pom
        local color = self.db.profile.PoMcolors[count]
		self.core:SendStatusGained(UnitName(unitid), "alert_pom", settings.priority, (settings.range and 40), color, L["PoM:"]..count,count,5)
		return
	else
		self.core:SendStatusLost(UnitName(unitid), "alert_pom")
	end
end