global _sx
global _dx

global _next_channel
global _versions
global _info_field
global _current_file

----------------------------------------
--
----------------------------------------
on startMovie
	(the stage).title = "Player!"
	_versions = ["6.0", "6.5", "7.0", "8.0", "8.5", "9.0", "10.1", "11.5", "12.0"]

	_next_channel = 1
	y = 20

	m = new(#field)
	m.rect = rect(0, 0, 280, 0)
	m.alignment = "center"
	m.fontsize = 12
	m.font = "MS Sans Serif"
	m.wordWrap = FALSE
	m.fontstyle = "bold"
	m.text = "Drop DCR/DXR/DIR file into window"
	puppetSprite(_next_channel, TRUE)
	sprite(_next_channel).member = m
	sprite(_next_channel).loc = point(20, y)
	y = y + 32
	_next_channel = _next_channel + 1

	repeat with v in _versions
		m = new(#button)
		m.alignment = "center"
		m.fontsize = 10
		m.fontStyle = "bold"
		m.font = "MS Sans Serif"
		m.rect = rect(0, 0, 160 - 4, 0)
		m.text = "Player " & v
		m.scriptText = "on mouseUp" & RETURN & "runFile(" & QUOTE & v & QUOTE & ")" & RETURN & "end"
		puppetSprite(_next_channel, TRUE)
		sprite(_next_channel).member = m
		sprite(_next_channel).loc = point(80, y)
		sprite(_next_channel).visible = FALSE
		_next_channel = _next_channel + 1
		y = y + 28
	end repeat

	m = new(#button)
	m.alignment = "center"
	m.fontsize = 10
	m.fontStyle = "bold"
	m.font = "MS Sans Serif"
	m.rect = rect(0, 0, 160 - 4, 0)
	m.text = "Decompile"
	m.scriptText = "on mouseUp" & RETURN & "decompile()" & RETURN & "end"
	puppetSprite(_next_channel, TRUE)
	sprite(_next_channel).member = m
	sprite(_next_channel).loc = point(80, y)
	sprite(_next_channel).visible = FALSE
	_next_channel = _next_channel + 1
	y = y + 32

	_info_field = new(#field)
	_info_field.rect = rect(0, 0, 280, 0)
	_info_field.alignment = "left"
	_info_field.fontsize = 11
	_info_field.font = "MS Sans Serif"
	_info_field.wordWrap = FALSE
	puppetSprite(_next_channel, TRUE)
	sprite(_next_channel).member = _info_field
	sprite(_next_channel).loc = point(20, y)
	y = y + 40

	(the stage).rect = rect(0, 0, 320, y)
	the centerStage = 1
	(the stage).visible = 1

	_sx = xtra("shell").new()

	-- start listening for drop events
	_dx = xtra("DropXtra").new(#itemsDropped)
	_dx.dropStart()
end

----------------------------------------
--
----------------------------------------
on stopMovie
	if not voidP(_dx) then
		_dx.dropStop()
		_dx = VOID
	end if
	_sx = VOID
end

----------------------------------------
--
----------------------------------------
on showGUI
	repeat with i = 2 to _next_channel
		sprite(i).visible = TRUE
	end repeat
	updateStage()
end

----------------------------------------
--
----------------------------------------
on runFile (v)
	exe = the moviePath & "resources\Director " & v & "\run.cmd"
	_sx.shell_exec(exe, ["parameters": QUOTE & _current_file & QUOTE, "show_cmd": 0])
end

----------------------------------------
--
----------------------------------------
on showVersion (dir_file)
	the itemDelimiter = "."
	ext = the last item of dir_file
	_current_file = dir_file
	_sx.shell_setcurrentdir( the applicationPath & "resources")
	cmd = "projectorrays.exe version " & QUOTE & dir_file & QUOTE
	file_version_str = _sx.shell_cmd(cmd)
	the itemDelimiter = the last char of the moviePath
	basename = the last item of dir_file
	info_str = "File: " & basename & RETURN
	if file_version_str = "" then
		_info_field.text = info_str & "Format: Unknown"
	else
		_info_field.text = info_str & "Format: " & file_version_str
		cmd = "projectorrays.exe version --style integer " & QUOTE & dir_file & QUOTE
		file_version = integer(_sx.shell_cmd(cmd))
		repeat with v in _versions
			if float(v) * 100 >= file_version then
				runFile(v)
				exit repeat
			end if
		end repeat
	end if
end

----------------------------------------
--
----------------------------------------
on decompile
	if voidP(_current_file) then
		return
	end if
	the itemDelimiter = "."
	ext = the last item of _current_file
	if ["dir", "dxr", "dcr"].getPos(ext) then
		dest_file = _current_file.char[1.._current_file.length-4] & "_decompiled.dir"
	else if ["cst", "cxt", "cct"].getPos(ext) then
		dest_file = _current_file.char[1.._current_file.length-4] & "_decompiled.cst"
	else
		dest_file = _current_file & "_decompiled"
	end if
	cmd = "cmd /cprojectorrays.exe decompile " & QUOTE & _current_file & QUOTE && "-o" && QUOTE & dest_file & QUOTE & "&pause"
	_sx.shell_setcurrentdir( the applicationPath & "resources")
	res = _sx.shell_cmd(cmd)
	showVersion(dest_file)
end

----------------------------------------
-- @callback
----------------------------------------
on itemsDropped (tItems)
	if count(tItems.files) then
		showGUI()
		showVersion(tItems.files[1])
	end if
end
