Prat_TellTarget = Prat:NewModule('telltarget')

function Prat_TellTarget:OnInitialize()
    local db = Prat.db.profile

    if not db.TellTarget then
        db.TellTarget = {
            on = true
        }
    end

    Prat.Options.args.telltarget = {
        name = 'TellTarget',
        desc = 'TellTarget',
        type = 'group',
        args = {
            toggle = {
                name = 'Toggle',
                desc = 'Toggle the module on and off',
                type = 'toggle',
                get = function() return db.TellTarget.on end,
                set = function(v)
                    if v then
                        self:OnEnable()
                    else
                        self:OnDisable()
                    end
                    db.TellTarget.on = v
                end
            }
        }
    }
end

function Prat_TellTarget:OnEnable()
    SlashCmdList['IDCHAT_SLASH_TELLTARGET'] = function(str) self:tellTarget(str) end
    SLASH_IDCHAT_SLASH_TELLTARGET1 = '/tt'
end

function Prat_TellTarget:OnDisable()
    SlashCmdList['IDCHAT_SLASH_TELLTARGET'] = nil
    SLASH_IDCHAT_SLASH_TELLTARGET1 = nil
end

function Prat_TellTarget:tellTarget(str)
    if not (UnitExists('target') and UnitName('target') and UnitIsPlayer('target') and GetDefaultLanguage('player')==GetDefaultLanguage('target')) then
        Prat:Print('Invalid target.')
        return
    end
    if not (str and strlen(str)>0) then
        Prat:Print('No message supplied.')
    end

    local name, realm = UnitName('target')
    if realm and realm ~= GetRealmName() then
        name = string.format('%s-%s', name, realm)
    end
    SendChatMessage(str, 'WHISPER', nil, name)
end

