Prat_ChannelNames = Prat:NewModule('channelnames')

local a = {
    ['Guild'] = CHAT_MSG_GUILD,
    ['Party'] = CHAT_MSG_PARTY,
    ['Raid'] = CHAT_MSG_RAID,
    ['Raidleader'] = CHAT_MSG_RAID_LEADER,
    ['Raidwarning'] = CHAT_MSG_RAID_WARNING,
    ['Officer'] = CHAT_MSG_OFFICER,
    ['Battleground'] = CHAT_MSG_BATTLEGROUND,
    ['Battlegroundleader'] = CHAT_MSG_BATTLEGROUND_LEADER
}
local names = {}
for k,v in a do
    names[v] = k
end
a = nil

function Prat_ChannelNames:OnInitialize()
    self.db = Prat:AcquireDBNamespace('ChannelNames')

    Prat:RegisterDefaults('ChannelNames', 'profile', {
        on = true,
        Guild = 'G',
        Party = 'P',
        Raid = 'R',
        Raidleader = 'RL',
        Raidwarning = 'RW',
        Officer = 'O',
        Battleground = 'B',
        Battlegroundleader = 'BL',
        Channel1 = 'C1',
        Channel2 = 'C2',
        Channel3 = 'C3',
        Channel4 = 'C4',
        Channel5 = 'C5',
        Channel6 = 'C6',
        Channel7 = 'C7',
        Channel8 = 'C8',
        Channel9 = 'C9',
        Channel10 = 'C10',
    })

    Prat.Options.args.channelnames = {
        name = 'ChannelNames',
        desc = 'ChannelNames',
        type = 'group',
        args = {
            toggle = {
                name = 'Toggle',
                desc = 'Toggle the module on and off',
                type = 'toggle',
                get = function() return self.db.profile.on end,
                set = function(v)
                    if v then
                        self:OnEnable()
                    else
                        self:OnDisable()
                    end
                    self.db.profile.on = v
                end
            },
            guild = {
                name = 'Guild',
                desc = 'Guild settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat guild text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Guild end,
                        set = function(v) self.db.profile.Guild = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Guild = '' end
                    }
                }
            },
            party = {
                name = 'Party',
                desc = 'Party settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat party text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Party end,
                        set = function(v) self.db.profile.Party = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Party = '' end
                    }
                }
            },
            raid = {
                name = 'Raid',
                desc = 'Raid settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat raid text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Raid end,
                        set = function(v) self.db.profile.Raid = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Raid = '' end
                    }
                }
            },
            raidleader = {
                name = 'Raidleader',
                desc = 'Raidleader settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat raidleader text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Raidleader end,
                        set = function(v) self.db.profile.Raidleader = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Raidleader = '' end
                    }
                }
            },
            raidwarning = {
                name = 'Raidwarning',
                desc = 'Raidwarning settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat raidwarning text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Raidwarning end,
                        set = function(v) self.db.profile.Raidwarning = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Raidwarning = '' end
                    }
                }
            },
            officer = {
                name = 'Officer',
                desc = 'Officer settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat officer text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Officer end,
                        set = function(v) self.db.profile.Officer = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Officer = '' end
                    }
                }
            },
            battleground = {
                name = 'Battleground',
                desc = 'Battleground settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat battleground text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Battleground end,
                        set = function(v) self.db.profile.Battleground = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Battleground = '' end
                    }
                }
            },
            battlegroundleader = {
                name = 'Battlegroundleader',
                desc = 'Battlegroundleader settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat battlegroundleader text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Battlegroundleader end,
                        set = function(v) self.db.profile.Battlegroundleader = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Battlegroundleader = '' end
                    }
                }
            },
            channel1 = {
                name = 'Channel1',
                desc = 'Channel1 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat channel1 text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Channel1 end,
                        set = function(v) self.db.profile.Channel1 = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Channel1 = '' end
                    }
                }
            },
            channel2 = {
                name = 'Channel2',
                desc = 'Channel2 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat channel2 text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Channel2 end,
                        set = function(v) self.db.profile.Channel2 = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Channel2 = '' end
                    }
                }
            },
            channel3 = {
                name = 'Channel3',
                desc = 'Channel3 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat channel3 text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Channel3 end,
                        set = function(v) self.db.profile.Channel3 = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Channel3 = '' end
                    }
                }
            },
            channel4 = {
                name = 'Channel4',
                desc = 'Channel4 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat channel4 text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Channel4 end,
                        set = function(v) self.db.profile.Channel4 = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Channel4 = '' end
                    }
                }
            },
            channel5 = {
                name = 'Channel5',
                desc = 'Channel5 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat channel5 text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Channel5 end,
                        set = function(v) self.db.profile.Channel5 = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Channel5 = '' end
                    }
                }
            },
            channel6 = {
                name = 'Channel6',
                desc = 'Channel6 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat channel6 text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Channel6 end,
                        set = function(v) self.db.profile.Channel6 = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Channel6 = '' end
                    }
                }
            },
            channel7 = {
                name = 'Channel7',
                desc = 'Channel7 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat channel7 text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Channel7 end,
                        set = function(v) self.db.profile.Channel7 = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Channel7 = '' end
                    }
                }
            },
            channel8 = {
                name = 'Channel8',
                desc = 'Channel8 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat channel8 text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Channel8 end,
                        set = function(v) self.db.profile.Channel8 = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Channel8 = '' end
                    }
                }
            },
            channel9 = {
                name = 'Channel9',
                desc = 'Channel9 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat channel9 text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Channel9 end,
                        set = function(v) self.db.profile.Channel9 = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Channel9 = '' end
                    }
                }
            },
            channel10 = {
                name = 'Channel10',
                desc = 'Channel10 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Set',
                        desc = 'Use a custom replacement for the chat channel10 text.',
                        type = 'text',
                        usage = '<string>',
                        get = function() return self.db.profile.Channel10 end,
                        set = function(v) self.db.profile.Channel10 = v end
                    },
                    off = {
                        name = 'Off',
                        desc = 'Turn this channel text off.',
                        type = 'execute',
                        func = function() self.db.profile.Channel10 = '' end
                    }
                }
            }
        }
    }
end

function Prat_ChannelNames:OnEnable()
    for i=1,7 do
        self:Hook(getglobal('ChatFrame'..i), 'AddMessage')
    end
end

function Prat_ChannelNames:OnDisable()
    for i=1,7 do
        self:Unhook(getglobal('ChatFrame'..i), 'AddMessage')
    end
end

function Prat_ChannelNames:AddMessage(frame, text, red, green, blue, id)
    local ChannelName
    local DatabaseName
    local Replacement

    ChannelName = string.gsub(text, '.+%[(.-)%].*|Hplayer.+', '%1')

    if string.find(ChannelName, '%d+%..+') then
        DatabaseName = string.gsub(ChannelName, '(%d+)%..+', 'Channel%1')
    else
        if names[ChannelName] then
            DatabaseName = names[ChannelName]
        end
    end

    if DatabaseName then
        Replacement = self.db.profile[DatabaseName]
        if Replacement then
            if Replacement == '' then
                text = string.gsub(text, ' %[.+%](.*|Hplayer)', '%1')
            else
                text = string.gsub(text, '%[.+%](.*|Hplayer)', Replacement..'%1')
            end
        end
    end

    self.hooks[frame].AddMessage.orig(frame, text, red, green, blue, id)
end

