dump = function (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

dumpf = function (o)
	outfile=io.open("dump-file.txt", "w")

	outfile:write(dump(o))
	outfile:close()
end

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


write_saved = function (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

_size = function (t)		-- return number of elements in table
	local i = 0
	for _,_ in pairs(t) do
		i = i +1
	end
	return i
end


blank_detail = function()
			detail_name.title = ""
			detail_desc.title = ""
end

load_visibility=function()

	local infile = io.open(Settings_s, "r")

	if infile == nil then
		print (Settings_s .. "  not there, that's ok.")
		return false
	end
	io.close(infile)

	local acc, playerID, visible

	for line in io.lines(Settings_s) do

		acc,  playerID, visible =  string.match(line, "(%S+) (%S+) (%S+)")
		-- char might have been deleted or might be new setup.

		if(accounts[acc] == nil) then return false end
		if(accounts[acc].player == nil) then return false end

		if(accounts[acc].player[playerID] ~= nil) then
			if visible == "true" then
				accounts[acc].player[playerID].visible= true
			else
				accounts[acc].player[playerID].visible= false
			end
		end
	end
end


save_visibility=function()
	local outfile = io.open(Settings_s, "w")
	for acc,Account_t in pairs (accounts) do
		for playerID, player_t in pairs (Account_t.player) do

			if outfile == nil then
				print ("Couldn't open " .. Settings_s .. "  file for writing.")
				return false
			end

			outfile:write(acc .. " " .. playerID)

			if (player_t.visible) then
				outfile:write(" true\n")
			else
				outfile:write(" false\n")
			end
		end	 --player
	end --ac
	io.close(outfile)
end

generate_id=function()
--write a combined list of achievement id we look for to add to the in-game part (cut and paste) for filtering
    local unique_id= {}   -- (id,true}
	print("Generating ids.lua")
	local outfile=io.open("data/ids.lua", "w")

	if outfile == nil then
		print ("Couldn't open data/ids.lua file for writing.")
	end
	outfile:write("hist.IDVersion=" .. quote(version) .. "\n")

	outfile:write("hist.IDs = {" .. "\n")

	for _,i in ipairs(Group_Order) do
		outfile:write("--  " .. i ..  "\n")
		for _,j in ipairs (Group_Dat[i].id) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end

		end
	end

	for _,i in ipairs(Trials_Order) do
		outfile:write("-- Trial " .. i .. "\n")
		for _,j in ipairs (Trials_Dat[i].id) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
		end
	end


	for i,j in pairs (DLC_Dat) do   -- i is DLC cname
		outfile:write("-- DLC: " ..i .. "\n")
		print(i)
		for k,l in ipairs (j.line) do
		--		print(k .. "  " ..l)
				if unique_id[l] == nil then
					unique_id[l] = true
					outfile:write("[" .. l .. "] = true,\n")
				end
		end
	end


	outfile:write("-- SQ " .. "\n")
	for j,_ in pairs (SQ_dat) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
	end

	outfile:write("-- Pub " .. "\n")
	for _,j in ipairs (Pub_Dat_Char.id) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
	end

	outfile:write("-- WB " .. "\n")
	for j,_ in pairs (WB_dat) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
	end

	outfile:write("-- Specials " .. "\n")
	for j,_ in pairs (Special_dat) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
	end

	outfile:write("}" .. "\n")
	outfile:close()
end


--Run through Location data to get the info needed to dimension the WB and SQ boxes
Location_Box = function (name)

	log("Location Box ")
	local cols = {}
	local lines = {}
	local Areas = {}
	--Summarise
	for i,entry in ipairs(Locations2Key) do
		if lines[entry.L] == nil then  lines[entry.L] = true end
		if cols[entry.C] == nil then  cols[entry.C] = entry.Area end	-- Note the Area for the Col
		if Areas[entry.Area] == nil then  Areas[entry.Area] = true end
	end
	--[[
	print("Cols:  " .. tostring(#cols))
	print("Lines: " .. #lines)
	print("Areas: " .. #Areas)
	--]]

	local return_t = iup.matrix{}
	return_t.numlin=#lines
	return_t.numcol=#cols
	return_t.widthdef=100     -- Not detail..

	iup.SetAttribute(return_t, "READONLY", "YES")
	iup.SetAttribute(return_t, "ALIGNMENT0", "ACENTER")
	-- Set Headings
	for col, Area in ipairs (cols) do
	--	print("Col:  " .. col .. ",  Area:  "  .. Area )
		return_t:setcell(0,col, Area_names[Area].long)
	--	print("Set Heading: " .. Area_names[Area].long)
	end

	--Set Lines
	return_t:setcell(0,0, L.Zone)
	for line,_ in ipairs(lines) do
		return_t:setcell(line,0, tostring(line))
	--	iup.SetAttribute(return_t,  "FGCOLOR*:".. tostring(line), FG_Colour_Not_Complete)
	--	iup.SetAttribute(return_t,  "BGCOLOR*:".. tostring(line), BG_Colour_Not_Complete)
	end

	--optionally set Contents
	if (type(name) ~= "string") then	return return_t end
--	get

	return return_t
end


-- take WB_Dat or SQ_Dat, which are indexed on id and return array
-- lines_t = table[key]  of  {done,of ids{id=false, id=false}}    then we can scan and set id to true if found
Reverse_Dat = function (dat_t,ids_t)
	if type(dat_t) ~= "table" then
		print("Reverse_Dat dat_t is not a table.")
		return
	end

	if type(ids_t) ~= "table" then
		print("Reverse_Dat ids_t is not a table.")
		return
	end



	local lines_t = {}	-- accumulate as LC Key
	for id, location_t in pairs (dat_t) do		--location is index into Locations2key and Locations2
		if (Locations2Key[location_t.Location] ==nil) then
			print("Reverse_dat")
			print("Location " .. Location .. " not found in Locations2Key")
			return
		end

		local key = Locations2Key[location_t.Location].key
		local link= location_t["link1"] ~= nil
		local gotid = (type(ids_t[id]) == "table")		-- do we have this achievement? nil is no. table is yes
		if lines_t[key] == nil then	--	add new line
			lines_t[key] = {}
			lines_t[key].L = Locations2Key[location_t.Location].L
			lines_t[key].C = Locations2Key[location_t.Location].C
			lines_t[key].ids={}
			lines_t[key].ids[id] = gotid
			lines_t[key].of =1
			lines_t[key].link=link		-- true if this key has Link1 (SQ)
			if gotid then lines_t[key].done = 1 else lines_t[key].done = 0 end
		else
			lines_t[key].ids[id] = gotid	--	add id to existing line
			lines_t[key].of = lines_t[key].of + 1
			if gotid then lines_t[key].done = lines_t[key].done + 1  end
		end
	end
	return lines_t
end

Populate_Dat = function(matrix, dat_t,ids_t)
		local detail_box={}
		local Rev = Reverse_Dat(dat_t,ids_t)
		matrix.Detail_zbox = iup.zbox{}		-- to which we append detail_box[key]


		for i,key_t  in ipairs (Locations2Key) do		--	{ key="1:1",L=1,C=1,Area=1},		--	name="Stonefalls"}
			local name = Locations2[i].name
			if name == nil then
				print("Populate_Dat: Locations2[i].name returns nil for i: " .. i)
				return
			end
			local line =Rev[key_t.key]
			if line == nil then 	-- this is ok. Location without a WB or SQ
			else
				matrix:setcell(line.L,line.C,name .. " (" .. line.done .. "/" .. line.of .. ")")
				if line.done == line.of then
					iup.SetAttribute(matrix, "FGCOLOR" .. key_t.key , FG_Colour_Complete)  -- Yup
					iup.SetAttribute(matrix, "BGCOLOR" .. key_t.key , BG_Colour_Complete)
				end
				if line.link then
					detail_box[key_t.key]=iup.matrix{numcol=3, numcol_visible=3,numlin=line.of}		--Detail box for that key "1:1"
					detail_box[key_t.key]:setcell(0,3, L.Link)
					iup.SetAttribute(detail_box[key_t.key], "WIDTH3", "200")
					iup.SetAttribute(detail_box[key_t.key], "ALIGNMENT3", "ALEFT")
				else
					detail_box[key_t.key]=iup.matrix{numcol=2, numcol_visible=2,numlin=line.of}		--Detail box for that key "1:1"
				end
				detail_box[key_t.key]:setcell(0,0, L.Ach_ID)
				detail_box[key_t.key]:setcell(0,1, L.Name)
				detail_box[key_t.key]:setcell(0,2, L.Detail)
				iup.SetAttribute(detail_box[key_t.key], "READONLY", "YES")
				iup.SetAttribute(detail_box[key_t.key], "ALIGNMENT0", "ACENTER")
				iup.SetAttribute(detail_box[key_t.key], "ALIGNMENTLIN0", "ALEFT")
				iup.SetAttribute(detail_box[key_t.key], "ALIGNMENT1", "ALEFT")
				iup.SetAttribute(detail_box[key_t.key], "ALIGNMENT2", "ALEFT")
				iup.SetAttribute(detail_box[key_t.key], "WIDTH0", "30")
				iup.SetAttribute(detail_box[key_t.key], "WIDTH1", "170")		-- name
				iup.SetAttribute(detail_box[key_t.key], "WIDTH2", "400")        --Detail
				-- Fill in the details
				local j=1		--count the lines
				for id,isdone in pairs (line.ids) do
					if Ach_Detail[id] == nil then
						name = "UNKN"
						Desc = ""
					else
						name = Ach_Detail[id].name
						Desc = Ach_Detail[id].description
					end
					detail_box[key_t.key]:setcell(j,0,id)
					detail_box[key_t.key]:setcell(j,1, name)
					detail_box[key_t.key]:setcell(j,2, Desc)
					if line.link then
						detail_box[key_t.key]:setcell(j,3, dat_t[id]["link1"])
					end
					if isdone then
						iup.SetAttribute(detail_box[key_t.key],"BGCOLOR" .. tostring(j) .. ":*", BG_Colour_Complete)		-- Yes
					end
					j=j+1
				end

				local thisDetail =detail_box[key_t.key]
				function thisDetail:click_cb (L,C)
					if C == 3 then
						iup.Help(self:getcell(L,C))		-- Launch Browser
					else
						return IUP_IGNORE
					end
				end
				iup.Append(matrix.Detail_zbox, detail_box[key_t.key])
			end
		end
		function matrix:enteritem_cb(L,C)
			matrix.Detail_zbox.value = detail_box[tostring(L) ..":" .. tostring(C)]
		end
end

append=function (destination_t,source_t)
--takes 2 arrays. source_t is appended to destination_t
	if type(destination_t) ~= "table" then
		print("error (append), destination_t is not a table")
		return
	end

	if type(source_t) ~= "table" then
		print("error (append), source_t is not a table")
		return
	end

	for line, Ach in ipairs (source_t) do
		table.insert(destination_t,Ach)
	end
end


--for filtering
select_box=function (account_t)

-- account_t   is accounts[acc]

local marks_t = {}
local names_t = {}
for _,playerID in  ipairs(account_t.AllplayerIDs) do

	if account_t.player[playerID].visible then
		table.insert(marks_t,1)
	else
		table.insert(marks_t,0)
	end
	table.insert(names_t,account_t.player[playerID].name)

end

local size = #names_t

local error = iup.ListDialog(2,"Show Characters",size, names_t,0,1,size, marks_t)


if error == -1 then
	return false
end


for i,playerID in  ipairs(account_t.AllplayerIDs) do

	if (marks_t[i] == 0) then
		account_t.player[playerID].visible = false
	else
		account_t.player[playerID].visible = true
	end

end




for _, ADung in pairs (account_t.alldungeons) do	-- for each dungeon

	for col,playerID in  ipairs(account_t.AllplayerIDs) do

		if account_t.player[playerID].visible then
			iup.SetAttribute(ADung.box, "WIDTH"..tostring(col) ,"100")
		else
			iup.SetAttribute(ADung.box, "WIDTH"..tostring(col) ,"0")
		end
	end
end

save_visibility()

return true
end