require "lfs"
--require "json"
module(..., package.seeall)

function CopyFile (source, target)
	local commandLine = ("cp %s %s"):format(source, target)
	LogInfo("[HELPERS.filesystem:CopyFile] Copying file. Command %s.", commandLine)
	os.execute(commandLine)
end

function CopyTreeDirectoryWithContent (source, target)
	local commandLine = ("cp -avr %s %s"):format(source, target)
	LogInfo("[HELPERS.filesystem:CopyTreeDirectoryWithContent] Copying directory structure with content. Command %s.", commandLine)
	os.execute(commandLine)
end

function CreateDirectory (directoryName)
	local alreadyExist = IsDirectory(directoryName)
	if alreadyExist then
		return false, {result = "UNABLE TO CREATE DIRECTORY. DIRECTORY ALREADY EXIST", directory = directoryName}
	end
	local commandLine = "mkdir -p " .. directoryName
	--LogInfo("[HELPERS.filesystem:CreateDirectory] Creating directory %s.", commandLine)
	os.execute(commandLine)
	return true
end

function CreateFile (fullFileName, content)
	assert(fullFileName)
	assert(content)
	--LogInfo("[HELPERS.filesystem:CreateFile] fullFileName file. %s.", fullFileName)
	--LogDebug("[HELPERS.filesystem:CreateFile] fullFileName file. %s content %s.", fullFileName, content)
	local f, result = io.open(fullFileName, "wb")
	if not f then
		local response = {result = "ERROR CREATING TXT FILE", file = fullFileName, err = result}
		return false, response
	end
	f:write(content)
	f:close()
	return true, {}
end

function FileRead (fullFileName, returnType)
	assert(type(fullFileName) == "string")
	--LogDebug("[HELPERS.filesystem:FileRead] Read file '%s' return type: '%s'.", fullFileName, returnType)

	local f = io.open(fullFileName)
	if not f then
		LogWarning("[HELPERS.filesystem:FileRead] Unable to open file: %s", fullFileName)
		return false, {errType = "UNABLE TO OPEN FILE"}
	end

	local f = io.open(fullFileName, "rb")
	local content

	if (returnType or ""):lower() == "table" then
		content = {}
		local k = 1
		for line in f:lines() do
			content[k] = line
			k = k + 1
		end
		--LogDebug(json.encode(content))
	else
		content = f:read("*a")
		--LogDebug("[HELPERS.filesystem:FileRead] content: %s", content)
	end

	f:close()
	return true, content
end

--[[
	Retorna el archivo en un array, y cada item del array es un parte de N Bytes.
]]--
function FileReadInParts (fullFileName)
	local blockBytes = (5 * 1024) * 1024 -- 5 Mb en bytes (5.242.880)
	local f = assert(io.open(fullFileName, "rb"))
	local content = {}
	local i = 0

	while true do
		i = i + 1
		local bytes = f:read(blockBytes)
		--LogDebug("%d - bytes %s", i, bytes)
		if not bytes then
			break
		end
		table.insert(content, bytes)
	end

	LogDebug("[HELPERS.filesystem:FileReadInParts] Readed file in parts. Block bytes '%d', total parts '%d', File: '%s' .", blockBytes, #content, fullFileName)

	--LogDebug("%s", #content, content[1])
	-- local tmpConten = table.concat(content)
	-- CreateFile("/var/lib/inconcert/rasa/vccs/vcc1/app1/models/borrame1.algo", tmpConten)

	return true, content
end

function IsDirectory (directoryName)
	assert(type(directoryName) =="string")
	local currentdir = lfs.currentdir()
	local is = lfs.chdir(directoryName) and true or false
	lfs.chdir(currentdir)
	--LogDebug("[HELPERS.filesystem:IsDirectory] Is Directory = %s. directoryName: %s.", is, directoryName)
	return is
end

function MoveDirectory (source, target)
	local commandLine = ("mv %s* %s"):format(source, target)
	LogInfo("[HELPERS.filesystem:MoveDirectory] Moving directory. %s.", commandLine)
	os.execute(commandLine)
end

function RemoveDirectory (directoryName)
	if not directoryName then
		return false, {}
	end
	local exist = IsDirectory(directoryName)
	if not exist then
		return false, {result = "UNABLE TO REMOVE DIRECTORY. DIRECTORY DOES NOT EXIST", directory = directoryName}
	end
	local commandLine = ("rm -rf %s"):format(directoryName)
	LogInfo("[HELPERS.filesystem:RemoveDirectory] Removing directory. %s.", commandLine)
	os.execute(commandLine)
	return true
end

function RemoveFile (fullFileName)
	if not fullFileName then
		return false, {}
	end
	local commandLine = ("rm %s"):format(fullFileName)
	LogInfo("[HELPERS.filesystem:RemoveFile] Removing file. %s.", commandLine)
	os.execute(commandLine)
	return true
end

function WriteTxtFile (fullFileName, content)
	return CreateFile(fullFileName, content)
end
