-- Licence: MIT Licence
-- Some code from the luatz project (MIT Licence)
--[[
Utility to delete old Accounts

]]



-- Configuration ==========
local dateformat = "%Y-%m-%d, %H:%M"
local dateformat_log = "%Y-%m-%d-%H-%M-%S"

--iup.SetGlobal("UTF8MODE","YES")
-- ========================
version= "21"
require( "iuplua" )
require( "iupluacontrols" )
dofile "../../SavedVariables/History.lua"

function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end


-- Minimally effective quoting
function quote(astring)
    local quoted1 = string.gsub(astring,'\"', '\\"')
    local quoted2 = '"' .. string.gsub(quoted1,"%'", "\\'") .. '"'
	local linefeed = string.gsub(quoted2,"\n", "\\n")
	return linefeed
end


function write_saved(o)
--	local escaped
   if type(o) == 'table' then
      local s = '{\n'
      for k,v in pairs(o) do
         if type(k) ~= 'number' then
		 k = '"'..k..'"'
		 end
         s = s .. '['..k..'] = ' .. write_saved(v) .. ',\n'
      end
      return s .. '}\n'
   elseif
		type(o) == 'string' then
		return quote(o)
   else
		return tostring(o)
   end
end



accounts_list = {}		-- String list for selection dialog
naccounts = 0
for i,j in pairs(History_SV["Default"]) do
    print(i)
	lang = j["$AccountWide"].lang
	print(lang)
	table.insert(accounts_list, i)
end

if lang == nil then
lang = "en"
end

local path = "./data/" .. lang .. ".lua"
dofile (path)




function select_account()
	local selected
		if naccounts > 0 then
			selected = iup.ListDialog (1, L.ChooseAccounttoDelete,
					naccounts,	--Size
					accounts_list,
					1, --Initial
					1,naccounts	--MaxCol MaxLine
					)

			if selected <0 then
				return nil		-- Cancelled
			else
				return accounts_list[selected+1]
			end
		else
		return accounts_list[1]		-- only 1 account, no need for Dialog
		end
end

--[[  -- Need two or more accounts
if naccounts < 2  then
	iup.Message("",L.NoAccount)
	iup.Close()
	os.exit()
end

--]]


-- Create dialog to choose account
account=select_account()
-- Not selected, quit.
if account == nil then
	iup.Close()
	os.exit()
end


confirm=  iup.messagedlg{
  dialogtype = "QUESTION",
  buttons = "OKCANCEL",
  buttondefault = 2,
  title = L.Delete .. "  " .. L.Account,
  value = L.Delete .. " " .. account
  }

confirm:popup()
-- Button "1" is OK.

if iup.GetAttribute(confirm, "BUTTONRESPONSE") ~= "1" then
print ("Cancelled")
--	iup.Close()
--	os.exit()
end

backfile=os.date("History-"..dateformat_log .. ".lua")
-- os.execute( "md ..\\..\\SavedVariables\\History_Bak" )  -- fails silently if already exists.
-- os.execute("copy  ..\\..\\SavedVariables\\History.lua ..\\..\\SavedVariables\\History_Bak\\" .. backfile)

print ("Deleted")

newf = assert(io.open("../../SavedVariables/History.new", "w"))
-- ==============

--Dump it back out
newf:write("History_SV=" .. write_saved(History_SV))
newf:close()

--[[
-- ============== Setup storage for old characters.
archivef = io.open("../../SavedVariables/History.Archive", "r")
if archivef ==nil then	--create empty file
	archivef = io.open("../../SavedVariables/History.Archive", "w")
	archivef:write("Archive = {} \n")
end

archivef:close()
dofile "../../SavedVariables/History.Archive"

-- newf = assert(io.open("../../SavedVariables/History.new", "w"))
-- ==============
--]]

--Dump it back out
-- newf:write("History_SV=" .. write_saved(History_SV))
-- newf:close()