--import config.lua
--import utils.lua
--import uiLib.lua
--import textEditor.lua

--import userConfig.lua

Terminal = {}

function start()
	Terminal.textEditor = TextEditor:new()
	
	Terminal.textEditor:init(TextEditor.TERMINAL_MODE)
	Terminal.textEditor.wrapLines = true

	Terminal.textEditor:setDepth(UILib.uiDepth_overViews)

	local programId = getProgramId()

	invokeProgram(MAIN_PROGRAMID, "registerShortcut", {desc = "copy text to clipboard", key = KEY_C, modifier = KEY_LEFT_CONTROL, callback = "ctrlCPressed", programId = programId})
	invokeProgram(MAIN_PROGRAMID, "registerShortcut", {desc = "paste text from clipboard", key = KEY_V, modifier = KEY_LEFT_CONTROL, callback = "ctrlVPressed", programId = programId})

	invokeProgram(MAIN_PROGRAMID, "setViewTitle", programId, "terminal")

	Terminal.textEditor:setNameHighlights({"pocket workstation"})

	Terminal.textEditor:setTerminalCommand("open", _openCommandExec)
	Terminal.textEditor:setTerminalCommand("cfg", _cfgCommandExec)
	Terminal.textEditor:setTerminalCommand("ditherON", _ditherONCommandExec)
	Terminal.textEditor:setTerminalCommand("ditherOFF", _ditherOFFCommandExec)
	Terminal.textEditor:setTerminalCommand("bilinearON", _bilinearONCommandExec)
	Terminal.textEditor:setTerminalCommand("bilinearOFF", _bilinearOFFCommandExec)
	Terminal.textEditor:setTerminalCommand("alphaON", _alphaONCommandExec)
	Terminal.textEditor:setTerminalCommand("alphaOFF", _alphaOFFCommandExec)
	Terminal.textEditor:setTerminalCommand("colorCompressionON", _colorCompressionONCommandExec)
	Terminal.textEditor:setTerminalCommand("colorCompressionOFF", _colorCompressionOFFCommandExec)

	local projects = listProjects()
	local projectListStr = ""
	for i, name in ipairs(projects) do
		projectListStr = projectListStr .. name .. "\n"
	end

	Terminal.textEditor:sendTerminalText(
"initializing pocket workstation\n" ..
"\n" ..
"type open projectName to work on a project\n" ..
"available projects:\n" ..
projectListStr ..
"\nuse any other name to start a new project\n\n" ..
"type cfg to list config commands"
		)
end

function _ditherONCommandExec()
	invokeProgram(MAIN_PROGRAMID, "setProjectVar", "video_ditherEnabled", true)
	return "setting stored"
end

function _ditherOFFCommandExec()
	invokeProgram(MAIN_PROGRAMID, "setProjectVar", "video_ditherEnabled", false)
	return "setting stored"
end

function _bilinearONCommandExec()
	invokeProgram(MAIN_PROGRAMID, "setProjectVar", "video_bilinearInterpEnabled", true)
	return "setting stored"
end

function _bilinearOFFCommandExec()
	invokeProgram(MAIN_PROGRAMID, "setProjectVar", "video_bilinearInterpEnabled", false)
	return "setting stored"
end

function _alphaONCommandExec()
	invokeProgram(MAIN_PROGRAMID, "setProjectVar", "video_ditherTransparencyEnabled", true)
	return "setting stored"
end

function _alphaOFFCommandExec()
	invokeProgram(MAIN_PROGRAMID, "setProjectVar", "video_ditherTransparencyEnabled", false)
	return "setting stored"
end

function _colorCompressionONCommandExec()
	invokeProgram(MAIN_PROGRAMID, "setProjectVar", "video_colorCompressionEnabled", true)
	return "setting stored"
end

function _colorCompressionOFFCommandExec()
	invokeProgram(MAIN_PROGRAMID, "setProjectVar", "video_colorCompressionEnabled", false)
	return "setting stored"
end

function _cfgCommandExec()
	local bilinearInterp = invokeProgram(MAIN_PROGRAMID, "getProjectVar", "video_bilinearInterpEnabled")
	local dither = invokeProgram(MAIN_PROGRAMID, "getProjectVar", "video_ditherEnabled")
	local ditherTransparency = invokeProgram(MAIN_PROGRAMID, "getProjectVar", "video_ditherTransparencyEnabled")
	local colorCompression = invokeProgram(MAIN_PROGRAMID, "getProjectVar", "video_colorCompressionEnabled")

	local boolStr = {}
	boolStr[true] = "ON"
	boolStr[false] = "OFF"

	local currentOptions = ""
	currentOptions = currentOptions .. "//bilinear " .. boolStr[bilinearInterp] .. "\n"
	currentOptions = currentOptions .. "//dither " .. boolStr[dither] .. "\n"
	currentOptions = currentOptions .. "//alpha " .. boolStr[ditherTransparency] .. "\n"
	currentOptions = currentOptions .. "//colorCompression " .. boolStr[colorCompression] .. "\n"
	currentOptions = currentOptions .. "\n"

	resStr = currentOptions ..
[[
ditherON //dither ON
ditherOFF //dither OFF
bilinearON //bilinear filtering ON
bilinearOFF //bilinear filtering OFF
alphaON //dither transparency ON
alphaOFF //dither transparency OFF
colorCompressionON //color compression ON
colorCompressionOFF //color compression OFF]]

	return resStr
end

function _openCommandExec(projectName)
	if projectName == nil then
		return "usage: open <projectName>"
	end
	setProject(projectName)
	invokeProgram(MAIN_PROGRAMID, "initProject")
	invokeProgram(MAIN_PROGRAMID, "saveAppConfig")

	return "project set to " .. getProjectName()
end

function ctrlCPressed()
	local selectedText = Terminal.textEditor:getSelectedText()
	setClipboardText(selectedText)
end

function ctrlVPressed()
	local clipboardText = getClipboardText()
	Terminal.textEditor:pasteToTerminal(clipboardText)
end

function update()
	Terminal.textEditor:update()
end
