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 = '',
        Party = '',
        Raid = '',
        Raidleader = '',
        Raidwarning = '[WARN]',
        Officer = '[OFF]',
        Battleground = '[bg]',
        Battlegroundleader = '[BG]',
        Channel1 = '1',
        Channel2 = '2',
        Channel3 = '3',
        Channel4 = '4',
        Channel5 = '5',
        Channel6 = '6',
        Channel7 = '7',
        Channel8 = '8',
        Channel9 = '9',
        Channel10 = '10',
    })

    Prat.Options.args.channelnames = {
        name = 'ChannelNames',
        desc = 'ChannelNames',
        type = 'group',
        args = {
            guild = {
                name = 'Guild',
                desc = 'Guild settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Guild Channel',
                        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 = '' Prat:Print("Guild text now turned off") end
                    }
                }
            },
            party = {
                name = 'Party',
                desc = 'Party settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Party',
                        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 = '' Prat:Print("Party text now turned off") end
                    }
                }
            },
            raid = {
                name = 'Raid',
                desc = 'Raid settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Raid',
                        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 = '' Prat:Print("Raid text now turned off") end
                    }
                }
            },
            raidleader = {
                name = 'Raidleader',
                desc = 'Raidleader settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Raid Leader',
                        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 = '' Prat:Print("Raid Leader text now turned off") end
                    }
                }
            },
            raidwarning = {
                name = 'Raidwarning',
                desc = 'Raidwarning settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Raid Warning',
                        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 = '' Prat:Print("Raid Warning text now turned off") end
                    }
                }
            },
            officer = {
                name = 'Officer',
                desc = 'Officer settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Officier',
                        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 = '' Prat:Print("Officer text now turned off") end
                    }
                }
            },
            battleground = {
                name = 'Battleground',
                desc = 'Battleground settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Battleground',
                        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 = '' Prat:Print("Battleground text now turned off") end
                    }
                }
            },
            battlegroundleader = {
                name = 'Battlegroundleader',
                desc = 'Battlegroundleader settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Battleground Leader',
                        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 = '' Prat:Print("Battleground Leader text now turned off") end
                    }
                }
            },
            channel1 = {
                name = 'Channel1',
                desc = 'Channel1 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Channel 1',
                        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 = '' Prat:Print("Channel 1 text now turned off") end
                    }
                }
            },
            channel2 = {
                name = 'Channel2',
                desc = 'Channel2 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Channel 2',
                        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 = '' Prat:Print("Channel 2 text now turned off") end
                    }
                }
            },
            channel3 = {
                name = 'Channel3',
                desc = 'Channel3 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Channel 3',
                        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 = '' Prat:Print("Channel 3 text now turned off") end
                    }
                }
            },
            channel4 = {
                name = 'Channel4',
                desc = 'Channel4 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Channel 4',
                        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 = '' Prat:Print("Channel 4 text now turned off") end
                    }
                }
            },
            channel5 = {
                name = 'Channel5',
                desc = 'Channel5 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Channel 5',
                        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 = '' Prat:Print("Channel 5 text now turned off") end
                    }
                }
            },
            channel6 = {
                name = 'Channel6',
                desc = 'Channel6 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Channel 6 ',
                        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 = '' Prat:Print("Channel 6 text now turned off") end
                    }
                }
            },
            channel7 = {
                name = 'Channel7',
                desc = 'Channel7 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Channel 7',
                        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 = '' Prat:Print("Channel 7 text now turned off") end
                    }
                }
            },
            channel8 = {
                name = 'Channel8',
                desc = 'Channel8 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Channel 8 ',
                        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 = '' Prat:Print("Channel 8 text now turned off") end
                    }
                }
            },
            channel9 = {
                name = 'Channel9',
                desc = 'Channel9 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Channel 9 ',
                        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 = '' Prat:Print("Channel 9 text now turned off") end
                    }
                }
            },
            channel10 = {
                name = 'Channel10',
                desc = 'Channel10 settings.',
                type = 'group',
                args = {
                    set = {
                        name = 'Channel 10 ',
                        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 = '' Prat:Print("Channel 10 text now turned off") end
                    }
                }
            },
            toggle = {
                name = 'Toggle',
                desc = 'Toggle the module on and off',
                type = 'toggle',
                get = function() return self.db.profile.on end,
                set = function(v) self.db.profile.on = Prat:ToggleModuleActive('channelnames') end
            },
        }
    }
end

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

function Prat_ChannelNames:AddMessage(frame, text, ...)
  if text then
	local pattern = '%]%s+(.*|Hplayer)'
    local channel = string.gsub(text, '.*%[(.-)' .. pattern ..'.+', '%1')
    local replacement = nil
    if string.find(channel, '%d%. ') then
        replacement = self.db.profile[string.gsub(channel, '(%d)%..+', 'Channel%1')]
        pattern = '%[%d%..-' .. pattern
    elseif names[channel] then
        replacement = self.db.profile[names[channel]]
        pattern = '%[' .. channel .. pattern
    end
    
    if not replacement  then
        text = text
	else
        text = string.gsub(text, pattern, replacement .. '%1')
    end
  
  end

  self.hooks[frame].AddMessage.orig(frame, text, unpack(arg))
end
