
local app_args = {...}
local appFrame = win.create_app_frame ()

local APP_TITLE      = "email"

local ID_MENUBTN     = 101

local IDM_OPEN       = 1001
local IDM_PRINT      = 1002
local IDM_COPY       = 1003
local IDM_QUITAPP    = 1004


function appFrame:on_event (event, p1, p2, p3, p4, p5, ...)
	if event == "btn_click" then
		if p1:get_id () == ID_MENUBTN then
			self:on_menu ()
			return true
		end

	elseif event == "menu_cmd" then
		return self:on_command (p1)
	end

	return false
end


function appFrame:on_child_key (wnd, key, ctrl, alt, shift)
	if win.applicationFrame.on_child_key (self, wnd, key, ctrl, alt, shift) then
		return true
	end

	if ctrl and not alt and not shift then
		if key == keys.KEY_O then
			return self:on_command (IDM_OPEN)
		end

		if key == keys.KEY_P then
			return self:on_command (IDM_PRINT)
		end

		if key == keys.KEY_C then
			return self:on_command (IDM_COPY)
		end
	end

	return false
end


function appFrame:on_print_page (gdi, page, data)
	local width, height = gdi:get_page_size ()

	if not data.data.lines then
		data.data.lines = string.wrap (data.data.raw, width)
	end

	local top_line = ((page - 1) * height) + 1
	local last_line = ((top_line + height) > #data.data.lines and #data.data.lines) or
							top_line + height

	for line = top_line, last_line, 1 do
		gdi:write (data.data.lines[line], 0, line - top_line)
	end

	return (last_line < #data.data.lines)
end


function appFrame:on_print_data ()
	local str = self.app_to:get_text ().."\n"..
					self.app_from:get_text ().."\n"..
					self.app_sent:get_text ().."\n"..
					self.app_subject:get_text ().."\n\n"..
					self.app_message:get_text ()
	local width, height = string.wrap_size (string.wrap (str, 40))
	local data = { raw = str }
	local pages = math.ceil (height / 30)

	return win.applicationFrame.on_print_data (self, self:get_text (), data, pages)
end


function appFrame:on_menu ()
	local menu = win.menuWindow:new (self)

	menu:add_string ("Open  Ctrl+O",   IDM_OPEN)
	menu:add_string ("Print Ctrl+P",   IDM_PRINT)
	menu:add_string ("Copy  Ctrl+C",   IDM_COPY)
	menu:add_string ("------------")
	menu:add_string ("Quit App",       IDM_QUITAPP)

	menu:track (0, 1)
end


function appFrame:on_command (cmdId)
	if cmdId == IDM_OPEN then
		self:on_open_file ()
		return true
	end

	if cmdId == IDM_PRINT then
		self:print_doc ()
		return true
	end

	if cmdId == IDM_COPY then
		self:on_copy ()
		return true
	end

	if cmdId == IDM_QUITAPP then
		self:quit_app ()
		return true
	end

	return false
end


function appFrame:on_move ()
	self.app_to:move (0, 1, self.width)
	self.app_from:move (0, 2, self.width)
	self.app_sent:move (0, 3, self.width)
	self.app_subject:move (0, 4, self.width)
	self.app_message:move (0, 5, self.width, self.height - 5)

	return false
end


function appFrame:on_create ()
	self:dress (APP_TITLE)

	local menuBtn = win.buttonWindow:new (self, ID_MENUBTN, 0, 0, "Menu")
	menuBtn:set_colors (menuBtn:get_colors ().frame_text,
							  menuBtn:get_colors ().title_back,
							  menuBtn:get_colors ().frame_back)
	menuBtn:move (nil, nil, nil, nil, win.WND_TOP)

	self.app_to = win.labelWindow:new (self, 0, 0, 1, "")
	self.app_from = win.labelWindow:new (self, 0, 0, 2, "")
	self.app_sent = win.labelWindow:new (self, 0, 0, 3, "")
	self.app_subject = win.labelWindow:new (self, 0, 0, 4, "")
	self.app_message = win.textWindow:new (self, 0, 0, 5, self.width, self.height - 5, "")
	self.app_message:set_color (self:get_colors ().wnd_text)
	self.app_message:set_bg_color(self:get_colors ().wnd_back)

	self.app_cur_path = nil

	if #app_args > 0 then
		self:open_file (app_args[1])
	end

	self:set_active_top_frame ()

	return true
end


local function email_date_time (date_time)
	return os.date ("%y-%m-%d %H:%M", date_time)
end


function appFrame:open_file (path)
	local result = false
	local file = io.open (path, "r")

	if file then
		local email = utils.deserialize (file:read ("*a"))
		file:close ()

		if email then
			self.app_to:set_text (     "To     : "..tostring (email.recipient or ""))
			self.app_from:set_text (   "From   : "..tostring (email.sender or ""))
			self.app_sent:set_text (   "Sent   : "..email_date_time (email.time))
			self.app_subject:set_text ("Subject: "..tostring (email.subject or ""))
			self.app_message:set_text (tostring (email.message or ""))

			if type (email.subject) == "string" and email.subject:len () > 0 then
				self:set_title (email.subject..":"..APP_TITLE)
			else
				self:set_title ("no subject:"..APP_TITLE)
			end

			result = true
		end
	end

	if not result then
		self:msgbox ("Error", "Could not open email", term.colors.red)
	end
end


function appFrame:on_open_file ()
	local path = cmndlg.open_file (self, self.app_cur_path, true)

	if path then
		self:open_file (path)
	end
end


function appFrame:on_copy ()
	self:set_clipboard (string.format ("%s\r%s\r%s\r%s\r------------------------\r%s",
												  self.app_to:get_text (),
												  self.app_from:get_text (),
												  self.app_sent:get_text (),
												  self.app_subject:get_text (),
												  self.app_message:get_text ()),
							  CB_TEXT)
end


appFrame:run_app ()
