PLUGIN_NAME = 'test_plugin'
PLUGIN_AUTHOR = 'freemorger'
PLUGIN_VERSION = "v1.1"
PLUGIN_DESC = "Test plugin with counter and time"

ctr = 0

function onInit()
    frevi_stat_msg("Test Plugin loaded")
    frevi_reg_com("!testplug", test_counter)
    frevi_reg_com("!testplug_time", show_time)
    frevi_reg_com("!testplug_input", check_input)
    frevi_reg_com("!hex2int", hex_to_int)
    frevi_reg_com("!int2hex", int_to_hex)
    frevi_reg_com("!tabmsg", tab_message)
end

function test_counter()
    ctr = ctr + 1
    frevi_stat_msg("Counter: " .. ctr)
end

function show_time()
    local time = os.date("%Y-%m-%d %H:%M:%S")
    frevi_stat_msg("Current time: " .. time)
end

function check_input(args_table)
    input = table.concat(args_table, " ") -- args being passed to lua
    -- as a table
    frevi_stat_msg(input)
end

function hex_to_int(args_table)
    if #args_table < 1 then
        frevi_stat_msg("Usage: !hex2int num")
        return
    end
    local hexnum = args_table[1]
    local res = tonumber(hexnum:gsub("0x", ""), 16)
    frevi_stat_msg(tostring(res))
end

function int_to_hex(args_table)
    if #args_table < 1 then
        frevi_stat_msg("Usage: !hex2int num")
        return
    end
    local decnum = args_table[1]
    local res = string.format("%x", tonumber(decnum))
    frevi_stat_msg(res)
end

function tab_message()
    local msg = "Hello\nThis is dummy message,\nopened in new tab\nby plugin."
    frevi_open_msg(msg) -- This message will be opened in new tab and 
                        -- auto switched to it.
end
