--[[
Config.lua (Charging_points module)
]]--
local path = luci.dispatcher.context.requestpath
local user = path[#path-3] -- Usuario seleccionado para niveles de acceso
local gmod, amod
local modules = {}

-- Load modules
if user ~= "user" then
	gmod = require "luci.model.global_variables"
	amod = require "luci.model.advanced_global_variables"
	modules = {gmod, amod}
else
	gmod = require "luci.model.user_global_variables"
	modules = {gmod}
end
local chp   = require "luci.model.charging_points"
local uci = require "luci.model.uci"
local cur = uci.cursor("/etc/config")
local fs = require "nixio.fs"
local ubus = require "ubus"

local m, s
local tabs = {"general", "advanced"}
-----------------------------
-- Selected Charging Station
-----------------------------
local path = luci.dispatcher.context.requestpath
local user, serialnumber, hw_model = chp.whoami(path)

---------------------------
-- Main code: config.lua
---------------------------
m = Map("comun",translate("Configuration").." - "..serialnumber, translate(""))

m.reset = false
m.submit = false

s = m:section(TypedSection, "common", translate("Common"))
--s.template = "cbi/tblsection"
s.anonymous = true
s.addremove = false

--s:tab("common", translate("Common"))
s:tab("general", translate("General Setup"))
if #modules > 1 then
	s:tab("advanced", translate("Advanced Settings"))
end
--
-- Ubus migration 
--


local all_options = cur:get_all(serialnumber, "common")

if all_options then
for i, mod in ipairs(modules) do
for k, v in pairs(mod.booleanValues) do
	if all_options[k] then
		o = s:taboption(tabs[i], Flag, k, translate(k))
		--o = s:option(Flag, k, translate(k))
		o.rmempty = false
		o.disabled = "off"    
		o.enabled = "on"
		o.default = all_options[k]
	end
end
local lst = mod.listValues
for k, v in pairs(lst) do
	if all_options[k] then
		o = s:taboption(tabs[i], ListValue, k, translate(k))
		--o = s:option(ListValue, k, translate(k))
		o.default = all_options[k]
		for i, opt in pairs(lst[k]) do
			o:value(i, translate(opt))
		end
	end
end
local mlt = mod.multiValues
for k, v in pairs(mlt) do
	if all_options[k] then
		o = s:taboption(tabs[i], MultiValue, k, translate(k))
		--o = s:option(MultiValue, k, translate(k))
		o.default = all_options[k]
		for i, opt in pairs(mlt[k]) do
			o:value(i, translate(opt))
		end
	end
end
for k, v in pairs(mod.readOnly) do
	if all_options[k] then
		o = s:taboption(tabs[i], DummyValue, k, translate(k))
		--o = s:option(DummyValue, k, translate(k))
	end
end
for k, v in pairs(mod.textValues) do
	if all_options[k] then
		o = s:taboption(tabs[i], Value, k, translate(k))
		--o = s:option(Value, k, translate(k))
	end
end
end
--require "luci.model.global_variables"
--create_page("general")
--require "luci.model.advanced_global_variables"
--create_page("advanced")
--[[	for k, v in pairs(all_options) do
		local exist = true
		if k:sub(1,1) ~= "." then
			if booleanValues[k] then
				--o = s:taboption("common", Flag, k, translate(k))
				o = s:option(Flag, k, translate(k))
				o.disabled = "off"    
				o.enabled = "on"
				o.default = v
			elseif listValues[k] then
				--o = s:taboption("common", ListValue, k, translate(k))
				o = s:option(ListValue, k, translate(k))
				o.default = v
				for i = 0, #listValues[k]-1, 1 do
					o:value(i, translate(listValues[k][i+1]))
				end
			elseif multiValues[k] then
				--o = s:taboption("common", MultiValue, k, translate(k))
				o = s:option(MultiValue, k, translate(k))
				o.default = v
				for i = 0, #multiValues[k]-1, 1 do
					o:value(i, translate(multiValues[k][i+1]))
				end
			elseif readOnly[k] then
				--o = s:taboption("common", DummyValue, k, translate(k))
				o = s:option(DummyValue, k, translate(k))
			elseif textValues[k] then
				--o = s:taboption("common", Value, k, translate(k))
				o = s:option(Value, k, translate(k))
			else
				exist = false
			end
			
			if exist then
				function o.write(self, msection, mvalue)
					local mytable = {}
					mytable[self.option] = mvalue

					local conn = ubus.connect()
					if not conn then
					    error("Failed to connect to ubusd")
					end
					conn:call("uci", "set", { config = "ocppd", section = "internal", values = mytable })
					conn:close()
				end
			end
		end
	end
]]--
end


 -- real world example from interpreter:
cur:foreach(serialnumber, "connector", function(opt)
	local name = opt['.name']
	local all_options = cur:get_all(serialnumber, name)
--s:tab("connector"..name, translate(name))
	s = m:section(NamedSection, name, "connector", translate(name))
	s.anonymous = true
	s.addremove = false

	s:tab("general", translate("General Setup"))
	if #modules > 1 then
		s:tab("advanced", translate("Advanced Settings"))
	end

for i, mod in ipairs(modules) do
for k, v in pairs(mod.booleanValues) do
	if all_options[k] then
		o = s:taboption(tabs[i], Flag, k, translate(k))
		--o = s:option(Flag, k, translate(k))
		o.rmempty = false
		o.disabled = "off"    
		o.enabled = "on"
		o.default = all_options[k]
	end
end
local lst = mod.listValues
for k, v in pairs(lst) do
	if all_options[k] then
		o = s:taboption(tabs[i], ListValue, k, translate(k))
		--o = s:option(ListValue, k, translate(k))
		o.default = all_options[k]
		for idx, key in pairs(lst[k]) do
			o:value(idx, translate(key))
		end
	end
end
local mlt = mod.multiValues
for k, v in pairs(mlt) do
	if all_options[k] then
		o = s:taboption(tabs[i], MultiValue, k, translate(k))
		--o = s:option(MultiValue, k, translate(k))
		o.default = all_options[k]
		for idx, key in pairs(mlt[k]) do
			o:value(idx, translate(key))
		end
	end
end
for k, v in pairs(mod.readOnly) do
	if all_options[k] then
		o = s:taboption(tabs[i], DummyValue, k, translate(k))
		--o = s:option(DummyValue, k, translate(k))
	end
end
for k, v in pairs(mod.textValues) do
	if all_options[k] then
		o = s:taboption(tabs[i], Value, k, translate(k))
		--o = s:option(Value, k, translate(k))
	end
end
end

--[[	for k, v in pairs(opt) do
		if k:sub(1,1) ~= "." then
			local exist = true
			if booleanValues[k] then
				--o = s:taboption("connector"..name, Flag, k, translate(k))
				o = s:option(Flag, k, translate(k))
				o.disabled = "off"    
				o.enabled = "on"
				o.default = v
			elseif listValues[k] then
				--o = s:taboption("connector"..name, ListValue, k, translate(k))
				o = s:option(ListValue, k, translate(k))
				o.default = v
				for i = 0, #listValues[k]-1, 1 do
					o:value(i, translate(listValues[k][i+1]))
				end
			elseif readOnly[k] then
				--o = s:taboption("connector"..name, DummyValue, k, translate(k))
				o = s:option(DummyValue, k, translate(k))
			elseif textValues[k] then
				--o = s:taboption("connector"..name, Value, k, translate(k))
				o = s:option(Value, k, translate(k))
			else
				exist = false
			end
			
			if exist then
				function o.write(self, msection, mvalue)
					local mytable = {}
					mytable[self.option] = mvalue

					local conn = ubus.connect()
					if not conn then
					    error("Failed to connect to ubusd")
					end
					conn:call("uci", "set", { config = "ocppd", section = "internal", values = mytable })
					conn:close()
				end
			end
		end
	end
]]--
end)
--[[
function m.on_commit(map)
local conn = ubus.connect()                                                          
if not conn then                                                                     
    error("Failed to connect to ubusd")                                              
end                                                                                  
conn:call("uci", "commit", { config = serialnumber })
conn:close()	
end
]]--
return m
