local L = AceLibrary("AceLocale-2.0"):new("PerfectRaid")

PerfectRaid.options = {
    type = "group",
    args = {
        simulate = {
            name = L["Raid Simulation"],
            type = "execute",
            desc = L["Raid Simulation"],
            func = function() PerfectRaid:Simulate() end,
        },
        align = {
            name = L["Frame Alignment"],
            type = "toggle",
            desc = L["Horizontal aligmment of frames"],
            get = "ToggleAlignRight",
            set = "ToggleAlignRight",
            map = {[false] = "Left", [true] = "Right"},
            guiNameIsMap = true,
        },
        valign = {
            name = L["Vertical Alignment"],
            type = "toggle",
            desc = L["Vertical Alignment of frames"],
            get = "ToggleAlignBottom",
            set = "ToggleAlignBottom",
            map = {[false] = "Top", [true] = "Bottom"},
            guiNameIsMap = true,
        },
        highlight = {
            name = L["Mouseover Highlighting"],
            type = "toggle",
            desc = L["Show/Hide the mouseover texture"],
            get = "ToggleHighlight",
            set = "ToggleHighlight",
        },
        range = {
            name = L["Proximity detection"],
            type = "toggle",
            desc = L["Dim frames that are out of your range"],
            get = "ToggleRange",
            set = "ToggleRange",
        },
        truncate = {
            name = L["Name Truncation"],
            type = "toggle",
            desc = L["Truncate player names"],
            get = "ToggleTruncate",
            set = "ToggleTruncate",
        },
        scale = {
            name = L["Scale"],
            type = "range",
            desc = L["Set PerfectRaid's Scale"],
            min = 0.1,
            max = 3.0,
            step = 0.05,
            get = "OptionScale",
            set = "OptionScale",
        },
        separator = {
            name = L["Group Separator"],
            type = "range",
            desc = L["Set the height of the separator"],
            min = 0,
            max = 30,
            get = "OptionSeparator",
            set = "OptionSeparator",
        },
        inverse = {
            name = L["Inverse Bars"],
            type = "toggle",
            desc = L["Show depletion bars"],
            get = "ToggleInverse",
            set = "ToggleInverse",
        },
        deficit = {
            name = L["Show Health Deficit"],
            type = "toggle",
            desc = L["Show health deficit"],
            get = "ToggleDeficit",
            set = "ToggleDeficit",
        },
        texture = {
            name = L["Bar Texture"],
            type = "text",
            desc = L["Change the status bar texture"],
            get = "OptionTexture",
            set = "OptionTexture",
            validate = {"default", "banto", "halcyone", "otravi", "perl", "smooth", "striped", "charcoal"},
        },
        lock = {
            name = L["Lock"],
            type = "toggle",
            desc = L["Lock/Unlock PerfectRaid"],
            get = "ToggleLocked",
            set = "ToggleLocked",
            map = {[false] = "Unlocked", [true] = "Locked"},
            guiNameIsMap = true,
        },
        sort = {
            name = L["Sort"],
            type = "text",
            desc = L["Sort the PerfectRaid frames"],
            set = "OptionSort",
            get = "OptionSort",
            usage = "{custom || name || class || group}",
        },
        manabars = {
            name = L["ManaBars"],
            type = "text",
            desc = L["Toggle mana bar overlays"],
            get = "ToggleManaBars",
            set = "ToggleManaBars",
            validate = {"off", "mana", "all"},
        },
    }
}

function PerfectRaid:ToggleAlignRight(value)
    if type(value) == "nil" then return self.opt.AlignRight end
    self.opt.AlignRight = value
    self:UpdateLayout()
end

function PerfectRaid:ToggleAlignBottom(value)
    if type(value) == "nil" then return self.opt.AlignBottom end
    self.opt.AlignBottom= value
    self:UpdateLayout()
end

function PerfectRaid:ToggleHighlight(value)
    if type(value) == "nil" then return self.opt.Highlight end
    self.opt.Highlight = value
end

function PerfectRaid:ToggleRange(value)
    if type(value) == "nil" then return self.opt.RangeCheck end
    self.opt.RangeCheck = value
    
    for unit in pairs(self.visible) do
        self.frames[unit]:SetAlpha(1.0)
    end
end

function PerfectRaid:ToggleTruncate(value)
    if type(value) == "nil" then return self.opt.Truncate end
    self.opt.Truncate = value
    self:UpdateLayout()
end

function PerfectRaid:OptionScale(value)
    if not value then return self.opt.Scale end
    self.opt.Scale = value
    PerfectRaidFrame:SetScale(value)
    self:SavePosition()
end

function PerfectRaid:OptionSeparator(value)
    if not value then return self.opt.Separator end
    self.opt.Separator = value
    PerfectRaid:UpdateLayout()
end

function PerfectRaid:ToggleInverse(value)
    if type(value) == "nil" then return self.opt.Inverse end
    self.opt.Inverse = value
end

function PerfectRaid:ToggleDeficit(value)
    if type(value) == "nil" then return self.opt.Deficit end
    self.opt.Deficit = value
end

function PerfectRaid:OptionTexture(value)
    if not value then return self.opt.Texture end
    self.opt.Texture = self.textures[value]
    -- PerfectRaid:UpdateLayout()
    
    for k,v in pairs(self.frames) do
        v.Bar:SetStatusBarTexture(self.opt.Texture)
        v.MBar:SetStatusBarTexture(self.opt.Texture)
        self:UpdateUnit(k)
        self:UpdateMana(k)
    end
end

function PerfectRaid:ToggleLocked(value)
    if type(value) == "nil" then return self.opt.Locked end
    self.opt.Locked = value
end

function PerfectRaid:ToggleManaBars(value)
    if type(value) == "nil" then return self.opt.ManaBars end
    self.opt.ManaBars = value
    PerfectRaid:UpdateRoster()
end

-- Sort functions
local funcs = {
    ["WARRIOR"] = function(unit) local _,class = UnitClass(unit) return class == "WARRIOR" end,
    ["PRIEST"] = function(unit) local _,class = UnitClass(unit) return class == "PRIEST" end,
    ["SHAMAN"] = function(unit) local _,class = UnitClass(unit) return class == "SHAMAN" end,
    ["MAGE"] = function(unit) local _,class = UnitClass(unit) return class == "MAGE" end,
    ["HUNTER"] = function(unit) local _,class = UnitClass(unit) return class == "HUNTER" end,
    ["PALADIN"] = function(unit) local _,class = UnitClass(unit) return class == "PALADIN" end,
    ["ROGUE"] = function(unit) local _,class = UnitClass(unit) return class == "ROGUE" end,
    ["WARLOCK"] = function(unit) local _,class = UnitClass(unit) return class == "WARLOCK" end,
    ["DRUID"] = function(unit) local _,class = UnitClass(unit) return class == "DRUID" end,
    ["HEALER"] = function(unit) local _,class = UnitClass(unit) return class == "DRUID" or class == "PRIEST" or class == "PALADIN" or class == "SHAMAN" end,
    ["MELEE"] = function(unit) local _,class = UnitClass(unit) return class == "ROGUE" or class == "WARRIOR" end,
    ["RANGEDDPS"] = function(unit) local _,class = UnitClass(unit) return class == "MAGE" or class == "WARLOCK" or class == "HUNTER" end,
    ["MYGROUP"] = function(unit) return PerfectRaid.frames[unit].group == PerfectRaid.mygroup end,
    ["1"] = function(unit) return PerfectRaid.frames[unit].group == 1 end,
    ["2"] = function(unit) return PerfectRaid.frames[unit].group == 2 end,
    ["3"] = function(unit) return PerfectRaid.frames[unit].group == 3 end,
    ["4"] = function(unit) return PerfectRaid.frames[unit].group == 4 end,
    ["5"] = function(unit) return PerfectRaid.frames[unit].group == 5 end,
    ["6"] = function(unit) return PerfectRaid.frames[unit].group == 6 end,
    ["7"] = function(unit) return PerfectRaid.frames[unit].group == 7 end,
    ["8"] = function(unit) return PerfectRaid.frames[unit].group == 8 end,
    ["ODD"] = function(unit) return math.mod(PerfectRaid.frames[unit].group, 2) == 1 end,
    ["EVEN"] = function(unit) return math.mod(PerfectRaid.frames[unit].group, 2) == 0 end,
    ["MYSIDE"] = function(unit) return math.mod(PerfectRaid.frames[unit].group, 2) == math.mod(PerfectRaid.mygroup, 2) end,
    ["ALL"] = function(unit) return true end,
	["MAINTANK"] = function(unit) 
		local tank = MRCTRA
        local name = UnitName(unit)
		if oRA then 
			tank = oRA.maintanktable
		end
        for k,v in pairs(tank) do
            if v == name then return true end
        end
	end,
}

local validSort = {CUSTOM = true, GROUP = true, NAME = true, CLASS = true}

function PerfectRaid:OptionSort(value)
    if not value then return self.opt.Sort end

    value = string.upper(value)   
    local s,e,type = string.find(value, "^([^%s]+)")
    local sort = {}
    
    if not validSort[type] then
        local usage = "|cffffff7fUsage|r: /praid sort {custom | group | name | class}"
        self:Print(usage)
        return
    end
    
    if type == "CUSTOM" then
        for token in string.gfind(string.sub(value, e + 1), "[^%s]+") do
            if funcs[token] then
                table.insert(sort, funcs[token])
            end
        end
        
        if not next(sort) then
            -- We didn't get any valid options here)
            
            local s = {}
            for k,v in pairs(funcs) do table.insert(s, k) end
            table.sort(s)
            
            local output = ""
            for k,v in ipairs(s) do
                output = string.format("%s%s | ", output, v)
            end
            
            output = string.lower(string.sub(output, 1, -3))
            self:Print("Valid sort options for CUSTOM are:")
            self:Print(output)
            return
        end        
        -- Make everyone else show up afterwards
        
        -- Export this back to PR core
        self.sortfunctions = sort
    elseif type == "NAME" then
        self.sortfunctions = {function() return true end}
    elseif type == "GROUP" then
        for i=1,8 do
            table.insert(sort, funcs[tostring(i)])
        end
        self.sortfunctions = sort
    elseif type == "CLASS" then
        self.sortfunctions = {funcs.WARRIOR, funcs.PRIEST, funcs.DRUID, funcs.PALADIN, funcs.ROGUE, funcs.SHAMAN, funcs.WARLOCK, funcs.MAGE, funcs.HUNTER}
    end

    self.opt.Sort = value
    table.insert(sort, funcs.ALL)
    
    self:Sort()
    self:UpdateLayout()
end
