local ss = analiz.libs.serv


local key_prefics="DB_PREF:"

local emessage_loc = ""
-- ------------------------------------------------------------------
-- ------------------------------------------------------------------
-- ------------------------------------------------------------------
ss.l("global_from_db.lua.start")

global_system_prop = nil 

local function load_prop(root)
  local sql = "select name as NAME, value as VALUE from OrgClientParams where ProfileName='"..root.."'"
  local m = analiz:map_new("tmp_prop")
  ss.s("tmp",sql)
  ss.e("ANALIZ_SAFE_EXECUTE tmp_r emessage ADD_MAP_FROM_SQL_V1 "..m.name.." PLIB NAME VALUE $(tmp)") 
  local  emessage = ss.g("emessage")
  if emessage~="" then ss.l("failed:"..emessage.." sql:"..sql) end
--  ss.l("all.prop:"..root)
--  ss.e("SHOW_STRING_MAP "..m.name)
  return ss.map2obj(m.name),emessage
end

-- ------------------------------------------------------------------
-- ------------------------------------------------------------------
-- ------------------------------------------------------------------

local function ispref_(v)
  if type(v)~="string" then return false end
  return v:find(key_prefics)==1
end

local function subset_(prop,name)
  local r = {}
   for k,v in pairs(prop) do
     if k:find(name)==1 then
        k=k:sub(name:len()+1,k:len())
        r[k]=v
     end
   end
  return r
end

-- --------------------------------------------------------------------
-- --------------------------------------------------------------------
-- --------------------------------------------------------------------
local parsers={}

function parsers.array_(prop,name,fun)
 local r={}
 local i=1
 while true do
   local nn = name.."."..i
--   ss.l(nn)
   local o = fun(prop,nn)
   if o==nil then break end
   r[#r+1]=o
   i=i+1
 end
 ss.l("parsers.array:"..name)
 ss.tprint(r)
-- ss.a(1)
 return r
end


function parsers.map_(prop,name)
 local obj = subset_(prop,name..".")
 ss.l("parsers.map_:"..name)
 ss.tprint(obj)
 ss.l("___________")
 return obj
end

function parsers.CustomItemList(prop,name)

 local obj = subset_(prop,name..".")
-- ss.l("CustomItemList")
-- ss.tprint(obj)
-- ss.l("__")
 if obj.NAME==nil then return nil end
 local r ={}
 local function d_(obj,prop_name)
  local v = obj[prop_name]
  if v=="" or v==nil then return "not found param:"..name..prop_name end
  return v
 end
 r.name        = d_(obj,"NAME")
 r.orgClient   = d_(obj,"ORG_CLIENT")
 r.link_format = d_(obj,"QETXT")
 r.link        = d_(obj,"LINK")
 return r
end

function StrToBool_ST(str,def_value)
 if str==nil then return ss.def(def_value,false) end
 if str=="1" or str=="YES" or str=="yes" then return true end
 return false
end

local function get_value_(prop,v,comment)
  local v = v:sub(key_prefics:len()+1,v:len()) -- remove pref
  local k = ss.mysplit(v,":")
  local dtype="string"
  local key
  if #k==1 then 
        key=v 
  else
    dtype=k[1]:lower()
    key=k[2] 
  end


  if dtype=="custom_list" then return parsers.array_(prop,key,parsers.CustomItemList) end
  if dtype=="map" then return parsers.map_(prop,key) end


  local val = prop[key]
  if val==nil then
    emessage_loc = emessage_loc.."gldb.not found key :"..key.."type:"..dtype.." for:"..ss.def(comment,"<undef>").."\n"
    return v
  end

  if dtype=="string" then return val end
  if dtype=="double" then return analiz:tonumber(val) end
  if dtype=="int" then return math.floor(analiz:tonumber(val)) end
  if dtype=="bool" then return val=="YES" end



    emessage_loc = emessage_loc.."gldb.invalid type:"..ss.def(dtype,"nil").." key :"..key.." for:"..ss.def(comment,"<undef>").."\n"
    return v

end

-- ------------------------------------------------------------------
-- ------------------------------------------------------------------
-- ------------------------------------------------------------------

local function common_load_(obj,prop,name)
 if obj==nil then
    emessage_loc = emessage_loc.."gldb.obj:"..name.." is nil".."\n"
 end
-- ss.l(name)
 local keys={}
 local is_sort=true
 local key_type=nil
 for k,v in pairs(obj) do
   if key_type==nil then 
     key_type=type(k)
   end
   if key_type~=type(k) then 
     is_sort=false
   end
   keys[#keys+1]=k
 end

 if is_sort then 
 table.sort(keys)
 end

 for i=1,#keys do
   local k = keys[i]
   local v = obj[k]
   local fname = name.."."..k
   if type(v)=="table" then 
     common_load_(v,prop,fname)
   else
    if ispref_(v) then
      local v =get_value_(prop,v,fname)
      obj[k] = v
      if type(v)=="string" then    ss.l(k..":"..v) end
    end
   end
 end
end
-- ------------------------------------------------------------------
-- ------------------------------------------------------------------
-- ------------------------------------------------------------------

local function load_lex_local_(obj,prop,name)
  common_load_(obj,prop,name)
end
-- ------------------------------------------------------------------
-- ------------------------------------------------------------------
-- ------------------------------------------------------------------
local function load_global_local_(obj,prop,name)
  common_load_(obj,prop,name)
end

-- ------------------------------------------------------------------
-- ------------------------------------------------------------------
-- ------------------------------------------------------------------

function LoadProfilePropertyFor(name)
  local prop,emessage = load_prop(name)
  return prop,emessage
end

-- ------------------------------------------------------------------
-- ------------------------------------------------------------------
-- ------------------------------------------------------------------

--load prop
 local prop_name = ss.def(global_prospero_setting.profile_name,"SYSTEM")
 ss.l("global_prospero_setting.profile_name:"..prop_name)
 local prop,emessage = load_prop(prop_name)
 global_system_prop=prop

 if emessage~="" then
   ss.l("global_from_db.lua(SYSTEM) failed")
   ss.l("emessage:"..emessage)
-- ao: (min 10)-> (sec 15)
   ss.e("sleep sec 15")

   ss.a(11)
 end

 load_lex_local_(global_oracle_setting_local,prop,"global_oracle_setting_local")
 load_global_local_(global_prospero_setting,prop,"global_prospero_setting")


 if emessage_loc~="" then
   ss.l("global_from_db.lua failed")
   ss.l("emessage_loc:"..emessage_loc)
-- ao: (min 10)-> (sec 15)

   ss.e("sleep sec 15")
   ss.a(11)
 end

--detect typeList
ss.l("detect lexType")
 do
  local lex = global_oracle_setting_local.lex
  local lextype = lex.lexType
  if lextype~=nil then
     lex.default_profile=lex["default_profile_"..lextype]
  else
   ss.l("global_oracle_setting_local.lex.lexType is NULL")
  end
--  ss.tprint(lex)
--  ss.a(1)

 end





