# This file is a part of alpha.

import ambient
import gettext
import os.path
import sys
sys.path.append(os.path.dirname(sys.argv[0]) + '/python')

import ambient.bindings
global_input_scheme = ambient.bindings.InputMappingScheme('*global*')
global_input_scheme.set_as_mapping_scheme()

from cmds import *
from files import *
from windows import *


#gettext.bindtextdomain('', os.path.dirname(__file__) + '../share/locale', unicode = True)
gettext.textdomain('')
_ = gettext.gettext

class StandardUnexpectedFileTimeStampDirector(object):
	def query_about_unexpected_time_stamp(buffer, context):
		from ambient.ui import MessageBox
		active_buffer = ambient.active_buffer()
		buffer.activate()
		if context == UnexpectedFileTimeStampContext.first_modification:
			return MessageBox.show('%s\n\nThe file has been already opened. Cannot write to an editing file.' % buffer.location, buttons = MessageBox.Buttons.yes_no, icon = MessageBox.Icon.question) == MessageBox.Result.yes
		elif context == UnexpectedFileTimeStampContext.overwrite_file:
			return MessageBox.show('%s\n\nThe file was modified by other process. Do you want to continue to save?' % buffer.location, buttons = MessageBox.Buttons.yes_no, icon = MessageBox.Icon.question) == MessageBox.Result.yes
		elif context == UnexpectedFileTimeStampContext.client_invocation:
			if MessageBox.show('%s\n\nThe file was modified by other process. Do you want to reload?\nIf you reload, current editing contents will be disposed.' % buffer.location, buttons = MessageBox.Buttons.yes_no, icon = MessageBox.Icon.question) == MessageBox.Result.yes:
				_reopen_file(buffer, False)
			else:
				active_buffer.activate()
			return True
		return Flase


def initialize_menu():
	import ambient.intrinsics
	import ambient.ui
	from ambient.ui import MenuBar, PopupMenu

	# menu bar
	menu_bar = MenuBar()
	menu_bar.append(100, _('&File'), None)
	menu_bar.append(200, _('&Edit'), None)
	menu_bar.append(300, _('&Search'), None)
	menu_bar.append(400, _('&View'), None)
	menu_bar.append(500, _('&Macros'), None)
	menu_bar.append(600, _('&Tool'), None)
	menu_bar.append(700, _('&Window'), None)
	menu_bar.append(800, _('&Help'), None)

	# 'File'
	def _update_file_menu(unused, m):
		buffer = ambient.active_buffer()
		m.enable(104, buffer.is_bound_to_file())
		m.enable(105, buffer.is_bound_to_file())
		m.enable(108, len(ambient.buffers()) > 1)
		m.enable(110, buffer.is_modified())
		m.enable(112, len(list(filter(lambda b : b.is_modified(), ambient.buffers()))) > 0)
		m.enable(115, False)
	m = PopupMenu(_update_file_menu)
	m.append(101, _('&New'), new_file)
	m.append(102, _('New wi&th Format...'), new_file_dialog)
	m.append_separator()
	m.append(103, _('&Open...'), open_file_dialog)
	m.append(104, _('&Reopen'), reopen_file)
	m.append(105, _('Reopen with Different &Encoding...'), reopen_with_different_encoding)
	m.append(106, _('Most Recently Used &Files'), None)
	m.append_separator()
	m.append(107, _('&Close'), close_buffer)
	m.append(108, _('Close Ot&hers'), close_all_inactive_buffers)
	m.append(109, _('C&lose All Buffers'), close_all_buffers)
	m.append_separator()
	m.append(110, _('&Save'), save_buffer)
	m.append(111, _('Save &As...'), save_buffer_dialog)
	m.append(112, _('Sa&ve All'), save_all_buffers)
	m.append_separator()
	m.append(113, _('&Print...'), print_buffer_dialog)
	import ambient.printing
	m.append(114, _('Set&up Page...'), ambient.printing.setup_pages_dialog)
	m.append_separator()
	m.append(115, _('Sen&d...'), None)
	m.append_separator()
	m.append(116, _('E&xit Alpha...'), ambient.kill_alpha)
	menu_bar.set_child(100, m)

	m.set_child(106, MRUManager().get_menu())

	# 'Edit'
	def _update_edit_menu(unused, m):
		buffer = ambient.active_buffer()
		caret = ambient.active_window().selected_editor.caret
#		m.enable(201, buffer.number_of_undoable_changes != 0)
#		m.enable(202, buffer.number_of_redoable_changes != 0)
		m.enable(203, not caret.is_selection_empty())
		m.enable(204, not caret.is_selection_empty())
		m.enable(205, caret.can_paste())
		m.enable(210, not buffer.read_only)
	m = PopupMenu(_update_edit_menu)
	m.append(201, _('&Undo'), ambient.intrinsics.undo)
	m.append(202, _('&Redo'), ambient.intrinsics.redo)
	m.append_separator()
	m.append(203, _('Cu&t'), cut_selection)
	m.append(204, _('&Copy'), copy_selection)
	m.append(205, _('&Paste'), paste)
	m.append(206, _('Paste from K&ill Ring'), paste_from_killring)
	m.append(207, _('&Delete'), ambient.intrinsics.delete_forward_character)
	m.append(208, _('Select &All'), ambient.intrinsics.select_all)
	m.append_separator()
	m.append(209, _('Ad&vanced'), None)
	m.append(210, _('I&nput Candidates'), ambient.intrinsics.show_completion_proposals_popup)
	menu_bar.set_child(200, m)

	# 'Edit' - 'Advanced'
	def _update_advanced_edit_menu(unused, m):
		buffer = ambient.active_buffer()
		m.enable(211, not buffer.read_only)
		m.enable(212, not buffer.read_only)
		m.enable(213, not ambient.active_window().selected_editor.caret.is_selection_empty())
		m.enable(214, buffer.is_narrowed())
	p = m
	m = PopupMenu(_update_advanced_edit_menu)
	m.append(211, _('Character to Code Point'), ambient.intrinsics.convert_character_to_code_point)
	m.append(212, _('Code Point to Character'), ambient.intrinsics.convert_code_point_to_character)
	m.append_separator()
	m.append(213, _('&Narrow'), narrow_to_selected_region)
	m.append(214, _('&Widen'), widen)
	p.set_child(209, m)

	# 'Search'
	def _update_search_menu(unused, m):
		m.check(301, ambient.ui.SearchDialog.is_visible())
		m.enable(305, False)
		m.enable(310, False)
		m.enable(311, False)
		m.enable(312, False)
	m = PopupMenu(_update_search_menu)
	m.append(301, _('&Find and Replace...'), ambient.ui.SearchDialog.show)
	m.append(302, _('Find &Next'), search_forward_again)
	m.append(303, _('Find &Previous'), search_backward_again)
	m.append(304, _('&Incremental Search'), isearch_forward)
	m.append(305, _('Revo&ke Highlight'), None)
	m.append_separator()
	m.append(306, _('&Go to Line...'), ambient.ui.goto_line_dialog)
	m.append(307, _('&Bookmarks'), None)
	m.append_separator()
	m.append(308, _('Go to &Match Brace'), matching_paren)
	m.append(309, _('E&xtend to Match Brace'), extend_to_matching_paren)
	m.append_separator()
	m.append(310, _('Find Files...'), None)	# ambient.ui.show_find_files_dialog
	m.append(311, _('Search in Files'), None)	# ambient.ui.show_grep_dialog
	m.append(312, _('Replace in Files'), None)	# ambient.ui.show_gresreg_dialog
	menu_bar.set_child(300, m)

	# 'Search' - 'Bookmarks'
	def _update_bookmark_menu(unused, m):
		has_bookmark = len(ambient.active_buffer().bookmarker) != 0
		m.enable(322, has_bookmark)
		m.enable(323, has_bookmark)
		m.enable(324, has_bookmark)
		m.enable(325, False)
	p = m
	m = PopupMenu(_update_bookmark_menu)
	m.append(321, _('T&oggle Bookmark'), toggle_bookmark)
	m.append(322, _('Nex&t Bookmark'), ambient.intrinsics.forward_bookmark)
	m.append(323, _('Pre&vious Bookmark'), ambient.intrinsics.backward_bookmark)
	m.append(324, _('&Clear All Bookmarks'), clear_all_bookmarks)
	m.append(325, _('Manage &Bookmarks...'), None)	# ambient.ui.show_bookmarks_dialog
	p.set_child(307, m)

	# 'View'
	def _update_view_menu(unused, m):
		m.enable(405, len(ambient.buffers()) > 1)
		m.enable(406, len(ambient.buffers()) > 1)
		m.enable(408, False)
		m.enable(409, False)
		m.check(407, True)
	m = PopupMenu(_update_view_menu)
	m.append(401, _('&Toolbar'), None)
	m.append(402, _('&Status Bar'), None)
	m.append(403, _('&Buffer Bar'), None)
	m.append_separator()
	m.append(404, _('Bu&ffers'), None)
	m.append(405, _('&Next Buffer'), next_buffer)
	m.append(406, _('&Previous Buffer'), previous_buffer)
	m.append_separator()
	m.append(407, _('&No Wrap'), None, True)
	m.append(408, _('Wrap by &Specified Width'), None, True)
	m.append(409, _('Wrap by &Window'), None, True)
	m.append_separator()
	m.append(410, _('&Refresh'), None)
	menu_bar.set_child(400, m)

	# 'Macros'
	m = PopupMenu()
	m.append(501, _('E&xecute'), None)
	m.append(502, _('Start/End &Definition'), None)
	m.append(503, _('&Append'), None)
	m.append(504, _('&Pause/Restart'), None)
	m.append(505, _('Insert &Query'), None)
	m.append(506, _('A&bort'), None)
	m.append(507, _('Sa&ve'), None)
	m.append(508, _('&Load'), None)
	m.append_separator()
	m.append(509, _('&Scripts'), None)
	menu_bar.set_child(500, m)

	# 'Window'
	def _update_window_menu(unused, m):
		multiple_windows = len(ambient.windows()) > 1
		m.enable(703, multiple_windows)
		m.enable(704, multiple_windows)
		m.enable(705, multiple_windows)
		m.enable(706, multiple_windows)
		m.enable(707, False)
	m = PopupMenu(_update_window_menu)
	m.append(701, _('Sp&lit Window'), split_window)
	m.append(702, _('Split Window Si&de-by-Side'), split_window_side_by_side)
	m.append(703, _('Close Ot&her Windows'), close_other_windows)
	m.append(704, _('&Close Window'), close_window)
	m.append(705, _('&Next Window'), next_window)
	m.append(706, _('&Previous Window'), previous_window)
	m.append_separator()
	m.append(707, _('&Always in Foreground'), None)
	menu_bar.set_child(700, m)

	# 'Help'
	m = PopupMenu()
	m.append(801, _('&About...'), ambient.ui.about_dialog)
	menu_bar.set_child(800, m)

	MenuBar.set_as_menu_bar(menu_bar)

if __name__ == '__main__':
	ambient.buffers().unexpected_file_time_stamp_director = StandardUnexpectedFileTimeStampDirector()
	initialize_menu()
	ambient.about_to_be_killed_hook = close_all_buffers

del(initialize_menu)
