#!/usr/bin/env lua
--[[
Copyright 2018 Marcos Gutierrez <gmarcos87@gmail.com>
Copyright 2021 Santiago Piccinini <spiccinini@altermundi.net>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-3.0
]]--
local ubus = require "ubus"
local json = require 'luci.jsonc'
local uci = require 'uci'
local vouchera = require('voucher.vouchera')
local utils = require('lime.utils')
local config = require('lime.config')
local portal = require('portal.portal')

vouchera.init()

local uci_cursor = config.get_uci_cursor()

local conn = ubus.connect()
if not conn then
    error("Failed to connect to ubus")
end

local function get_portal_config()
    local portal_config = portal.get_config()
    portal_config.status = 'ok'
    return utils.printJson(portal_config)
end

local function set_portal_config(msg)
    local status, error_msg = portal.set_config(msg.activated, msg.with_vouchers)
    if not status then
        utils.printJson({ status = "error", message = error_msg })
    else
        utils.printJson({ status = "ok" })
    end
end

local function get_portal_page_content(msg)
    utils.printJson(portal.get_page_content())
end

local function set_portal_page_content(msg)
    portal.set_page_content(
        msg.title,
        msg.main_text,
        msg.logo,
        msg.link_title,
        msg.link_url,
        msg.background_color
    )
    utils.printJson({ status = 'ok'})
end

local function show_url(msg)
    utils.printJson({ status = 'ok', url = uci_cursor:get("pirania", "base_config", "portal_url") });
end

local function change_url(msg)
    local url = msg.url
    uci_cursor:set("pirania", "base_config", "portal_url", url)
    uci_cursor:commit("pirania")
    utils.printJson({status = 'ok', url = url});
end


local function add_vouchers(msg)
    local vouchers, errmsg = vouchera.create(msg.name, msg.qty, msg.duration_m,
                                             msg.activation_deadline, msg.permanent)
    if vouchers then
        return utils.printJson({ status = 'ok', vouchers = vouchers})
    else
        return utils.printJson({ status = 'error', message = errmsg})
    end
end

local function rename(msg)
    local voucher = vouchera.rename(msg.id, msg.name)
    return utils.printJson({ status = voucher and 'ok' or 'error' })
end

local function invalidate(msg)
    local voucher = vouchera.invalidate(msg.id)
    return utils.printJson({ status = voucher and 'ok' or 'error' })
end


local function list_vouchers(msg)
    local vouchers = vouchera.list()
    return utils.printJson({ status = vouchers and 'ok' or 'error', vouchers = vouchers })
end

local methods = {
  get_portal_config = { no_params = 0 },
  set_portal_config = { activated = 'bool', with_vouchers = 'bool' },
  disable = { no_params = 0 },
  show_url = { no_params = 0 },
  change_url = { url = 'value' },
  add_vouchers = { name='str', qty='int', duration_m='int', activation_deadline='timestamp', permanent='bool'},
  list_vouchers = { no_params = 0 },
  rename = { id = 'str', name = 'str' },
  invalidate = { id = 'str' },
  get_portal_page_content = { no_params = 0 },
  set_portal_page_content = {
        title = 'value',
        main_text = 'value',
        logo = 'value',
        link_title = 'value',
        link_url = 'value',
        background_color = 'value'
    },
}

if arg[1] == 'list' then
    utils.printJson(methods)
end

if arg[1] == 'call' then
  local msg = utils.rpcd_readline()
  msg = json.parse(msg)
  if       arg[2] == 'get_portal_config'	then get_portal_config(msg)
  elseif   arg[2] == 'set_portal_config'	then set_portal_config(msg)
  elseif   arg[2] == 'disable'				then disable(msg)
  elseif   arg[2] == 'show_url'	            then show_url(msg)
  elseif   arg[2] == 'change_url'	        then change_url(msg)
  elseif   arg[2] == 'list_vouchers'	    then list_vouchers(msg)
  elseif   arg[2] == 'add_vouchers'	        then add_vouchers(msg)
  elseif   arg[2] == 'invalidate'	        then invalidate(msg)
  elseif   arg[2] == 'rename'	        then rename(msg)
  elseif   arg[2] == 'get_portal_page_content'    then get_portal_page_content(msg)
  elseif   arg[2] == 'set_portal_page_content'   then set_portal_page_content(msg)
  else     utils.printJson({ error = "Method not found" })
  end
end
