require("pm")
local titleStr = "Password Manager"
----------------------------------------------------------------------
local function escapeJSString(str)
 if str == nil then return nil end
 str = string.gsub(str,'\\','\\\\')
 str = string.gsub(str,'"','\\"')
 str = string.gsub(str,'\n','\\n')
 str = string.gsub(str,'\r','\\r')
 return str
end
----------------------------------------------------------------------
local function fillTable(Object)
 
 m_simpleTV.Dialog.ExecScript(Object,'clearLogins()')   
 local t = pm.getLoginTable()  
 for i=1,#t do
  local scr = 'addEntry('
                         .. '"' .. escapeJSString(t[i].id) .. '"'
                        .. ',"' .. escapeJSString(t[i].name) .. '"' 
						.. ',"' .. escapeJSString(t[i].login) .. '"' 
						.. ',"' .. escapeJSString(t[i].password) .. '"' 
						.. ')'
  m_simpleTV.Dialog.ExecScript(Object,scr)  
 end
end 
----------------------------------------------------------------------
local function translateHtml(Object)
  m_simpleTV.Dialog.ExecScript(Object,'m_Version = ' .. pm.GetVersion())
    
  local scr = 'm_editStr="' .. escapeJSString(pm.tr("edit")) .. '";'
              ..  'm_showPasswordStr="'  .. escapeJSString(pm.tr("show password")) .. '";'
              ..  'm_deleteStr="'  .. escapeJSString(pm.tr("delete")) .. '";'

  m_simpleTV.Dialog.ExecScript(Object,scr)
  m_simpleTV.Dialog.ExecScript(Object,'doTranslate();')
end
----------------------------------------------------------------------
function baseInit(Object)
 local scr = 'm_showPassword=' .. tostring(pm.getConfigVal('showPassword') or false) .. ';'
 m_simpleTV.Dialog.ExecScript(Object,scr)   
 m_simpleTV.Dialog.ExecScript(Object,'updateEditorShowPasswordButton();')  
 
 scr = 'm_showDialogPassword=' .. tostring(pm.isAllowShowEnterPasswordDialog()) .. ';'    
 m_simpleTV.Dialog.ExecScript(Object,scr)  
 fillTable(Object) 
end
----------------------------------------------------------------------
function OnNavigateComplete(Object)
  translateHtml(Object)
  baseInit(Object)
end
----------------------------------------------------------------------
function OnOk(Object)
 
 local showPass = m_simpleTV.Dialog.ExecScriptParam(Object,'(function(){return m_showPassword;})();')  
 if type(showPass) == 'boolean' then pm.setConfigVal('showPassword',showPass) end
 pm.setAllowShowEnterPasswordDialog(m_simpleTV.Dialog.ExecScriptParam(Object
				                    ,'(function(){return m_showDialogPassword;})();'))
   
 local tFromJs = m_simpleTV.Dialog.ExecScriptParam(Object,'getLogins();')  
 if tFromJs==nil or type(tFromJs)~='table' then return end
 
 local t = {}

 for k,v in pairs(tFromJs) do
	t[#t+1] = v 
 end   
 
  pm.save (t)
  pm.reload();
end
----------------------------------------------------------------------
--from v0.19
----------------------------------------------------------------------
local function oldPMDecodeStr(str, code)
  local s = string.gsub(str, "^" .. code, "")
  local n = tonumber(string.sub(s, 1, 1))
  s = string.sub(s, 2)
  local x = ""
  for i = 1, n do
    x = x .. "="
  end
  return x .. s
end
----------------------------------------------------------------------
local function importFromOld(line)
 if line==nil or not string.match(line, "^Rd%d") then
     m_simpleTV.Interface.MessageBox(pm.tr("Incorrect file"),titleStr,0x10)
	 return false
 end		
 line = oldPMDecodeStr(line,"Rd")
 line = decode64(string.reverse(line))
 --debug_in_file(line .. '\n')
 local logins = {}
 for w in line:gmatch('|(.-)|') do
   --debug_in_file(w .. '\n')   
   local t = split(w, '/')
   local id   =   t[1] or ''
   local name =   t[2] or ''
   local login  = t[3] or ''
   local pass =   t[4] or ''
   
   if id~='' and name~='' and login~='' and pass~='' then
     local i = #logins+1
	 logins[i]={}
	 logins[i].id       = id
	 logins[i].name     = name
	 logins[i].login    = login
	 logins[i].password = pass
	 
	 --debug_in_file('id:' .. logins[i].id .. '\nname:' .. logins[i].name 
		--			.. '\nloggin:' .. logins[i].login .. '\npassword:' .. logins[i].password .. '\n')
   end
 end  
 if #logins < 1 then return false end 
 pm.save(logins)
 return true
end
----------------------------------------------------------------------
----------------------------------------------------------------------
function requestImport(Object)
 if m_simpleTV.Interface.FilePicker == nil then return end
 
 if #(pm.getLoginTable())>0 then 
  if m_simpleTV.Interface.MessageBoxT then 
    if 'Yes' ~= m_simpleTV.Interface.MessageBoxT({message=pm.tr("All current logins/passwords will be removed.\nContinue ?")												
										 ,caption=titleStr,buttons='Yes|No',icon='Question',defButton = 'No'}) then return end
  else 
   if 6 ~= m_simpleTV.Interface.MessageBox(pm.tr("All current logins/passwords will be removed.\nContinue ?")												
										 ,titleStr,0x24) then return end
  end  
 end 										 
 
 local t = {}
 --t.name = pm.tr('Choose file')
 --t.startFolder = ''
 t.filters  = 'Data (*.txt *.ini);All (*)'
 --t.options    = 'ReadOnly'
 --t.fileMode   =  'ExistingFile'
 --t.acceptMode =  'AcceptOpen'
 local path = m_simpleTV.Interface.FilePicker(t)
 if path == nil then return end
 path = m_simpleTV.Common.UTF8ToMultiByte(path)
 
 local fhandle = io.open(path, "r")
 if fhandle == nil then 
    m_simpleTV.Interface.MessageBox(pm.tr("Can't open file"),titleStr,0x10)
   return 
 end
 if fhandle:seek("end") > 1024*512 then
	 fhandle:close()
	 m_simpleTV.Interface.MessageBox(pm.tr("Incorrect file"),titleStr,0x10)
     return 
 end 	 
 
 fhandle:seek("set", 0) 
 if path:match('(.-)pm_keys%.txt$') then 
    --import from old
	local data = fhandle:read("*l")
	fhandle:close()
	if importFromOld(data) ~= true then return end 
 else 
  local data = fhandle:read("*a")
  fhandle:close()
  if not data:match('(.-)%[General%](.-)maindata') then 
	  m_simpleTV.Interface.MessageBox(pm.tr("Incorrect file"),titleStr,0x10)
     return 
  end 	 
  path = m_simpleTV.Common.UTF8ToMultiByte(m_simpleTV.Common.GetMainPath(1)) 
		 .. '/LuaConfig/' .. pm.getConfigFile()
  m_simpleTV.Config.Sync(pm.getConfigFile())  
  
  local destFile = io.open(path, "w+")
  if destFile == nil then 
    m_simpleTV.Interface.MessageBox(pm.tr("Can't open destination file"),titleStr,0x10)
   return 
  end
  destFile:write (data)
  destFile:close()  
  m_simpleTV.Config.Sync(pm.getConfigFile())
 end
   
 pm.reload()
 baseInit(Object)
 m_simpleTV.Interface.MessageBox(pm.tr("Export completed successfully"),titleStr,0x00)
end
----------------------------------------------------------------------
function requestExport(Object)
 
 if m_simpleTV.Interface.FilePicker == nil then return end
  
 if #(pm.getLoginTable())==0 then 
   m_simpleTV.Interface.MessageBox(pm.tr("No data for export"),titleStr,0x10)
   return 
 end   
 
 local t = {}
  t.selectFile = pm.getConfigFile()
  t.filters  = 'Data (*.ini);All (*)'
 --t.name = pm.tr('Choose file')
 --t.startFolder = '' 
 if m_simpleTV.Common.GetVersion() >= 880 then 
  t.options    =  0 --QFileDialog::NO
  t.fileMode   =  'AnyFile'
  t.acceptMode =  'AcceptSave'
 else
  t.options    =  0 --QFileDialog::NO
  t.fileMode   =  0 --QFileDialog::AnyFile
  t.acceptMode =  1 --QFileDialog::AcceptSave
 end
 
 local destPath = m_simpleTV.Interface.FilePicker(t)
 if    destPath == nil then return end
 destPath = m_simpleTV.Common.UTF8ToMultiByte(destPath)
 
 local sourcePath = m_simpleTV.Common.UTF8ToMultiByte(m_simpleTV.Common.GetMainPath(1)) 
					.. '/LuaConfig/' .. pm.getConfigFile()
 
 m_simpleTV.Config.Sync(pm.getConfigFile())   
 local fhandle = io.open(sourcePath, "r")
 if fhandle == nil then 
    m_simpleTV.Interface.MessageBox(pm.tr("No data for export"),titleStr,0x10)
   return 
 end
 fhandle:seek("set", 0) 
 local data = fhandle:read("*a")
 fhandle:close()
  
 fhandle = io.open(destPath, "w+")
  if fhandle == nil then 
    m_simpleTV.Interface.MessageBox(pm.tr("Can't open destination file"),titleStr,0x10)
   return 
  end
 fhandle:write (data)
 fhandle:close()   

 destPath = m_simpleTV.Common.multiByteToUTF8(destPath)
 m_simpleTV.Interface.MessageBox(pm.tr("File saved in ") .. "'" .. destPath .. "'"  
								 ,titleStr,0x00)
end
----------------------------------------------------------------------
