/* Copyright 2016 Software Freedom Conservancy Inc.
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

public class LibraryWindow : AppWindow {
    public const int SIDEBAR_MIN_WIDTH = 120;
    public const int EXTENDED_INFO_MIN_WIDTH = 360;
    
    public static int PAGE_MIN_WIDTH {
        get {
            return Thumbnail.MAX_SCALE + (CheckerboardLayout.COLUMN_GUTTER_PADDING * 2);
        }
    }
    
    public const string SORT_EVENTS_ORDER_ASCENDING = "ascending";
    public const string SORT_EVENTS_ORDER_DESCENDING = "descending";
    
    private const string[] SUPPORTED_MOUNT_SCHEMES = {
        "gphoto2:",
        "disk:",
        "file:",
        "mtp:"
    };
    

    // If we're not operating on at least this many files, don't display the progress
    // bar at all; otherwise, it'll go by too quickly, giving the appearance of a glitch.
    const int MIN_PROGRESS_BAR_FILES = 1;
    
    // This lists the order of the toplevel items in the sidebar.  New toplevel items should be
    // added here in the position they should appear in the sidebar.  To re-order, simply move
    // the item in this list to a new position.  These numbers should *not* persist anywhere
    // outside the app.
    private enum SidebarRootPosition {
        LIBRARY,
        CAMERAS,
        SAVED_SEARCH,
        EVENTS,
        IMPORT_ROLL,
        FOLDERS,
        FACES,
        TAGS
    }

    // In fullscreen mode, want to use LibraryPhotoPage, but fullscreen has different requirements,
    // esp. regarding when the widget is realized and when it should first try and throw them image
    // on the page.  This handles this without introducing lots of special cases in
    // LibraryPhotoPage.
    private class FullscreenPhotoPage : LibraryPhotoPage {
        private CollectionPage collection;
        private Photo start;
        private ViewCollection? view;
        
        public FullscreenPhotoPage(CollectionPage collection, Photo start, ViewCollection? view) {
            this.collection = collection;
            this.start = start;
            this.view = view;
        }
        
        public override void switched_to() {
            display_for_collection(collection, start, view);
            
            base.switched_to();
        }

        public override void switching_from() {
        }

        protected override void init_collect_ui_filenames(Gee.List<string> ui_filenames) {
            base.init_collect_ui_filenames(ui_filenames);
            ui_filenames.add("photo_context.ui");
        }
    }

    private GLib.File import_dir = File.new_for_commandline_arg(Environment.get_home_dir());
    private bool import_recursive = true;

    private Gtk.Paned sidebar_paned = new Gtk.Paned(Gtk.Orientation.VERTICAL);
    private Gtk.Paned client_paned = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
    private Gtk.Frame bottom_frame = new Gtk.Frame(null);
    
    private OneShotScheduler properties_scheduler = null;
    private bool notify_library_is_home_dir = true;
    
    // Sidebar tree and roots (ordered by SidebarRootPosition)
    private Sidebar.Tree sidebar_tree;
    private Library.Branch library_branch = new Library.Branch();
    private Tags.Branch tags_branch = new Tags.Branch();
    private Folders.Branch folders_branch = new Folders.Branch();
    private Faces.Branch faces_branch = new Faces.Branch();    
    private Events.Branch events_branch = new Events.Branch();
    private Camera.Branch camera_branch = new Camera.Branch();
    private Searches.Branch saved_search_branch = new Searches.Branch();
    private ImportRoll.Branch import_roll_branch = new ImportRoll.Branch();

    private bool page_switching_enabled = true;

    private Gee.HashMap<Page, Sidebar.Entry> page_map = new Gee.HashMap<Page, Sidebar.Entry>();
    
    private LibraryPhotoPage photo_page = null;
    
    // this is to keep track of cameras which initiate the app
    private static Gee.HashSet<string> initial_camera_uris = new Gee.HashSet<string>();
    
    private bool is_search_toolbar_visible = false;
    
    private SearchFilterActions search_actions = new SearchFilterActions();
    private Gtk.Revealer search_toolbar;
    
    private Gtk.Box top_section = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
    private Gtk.Revealer background_progress_frame = new Gtk.Revealer();
    private BackgroundProgressBar background_progress_bar = new BackgroundProgressBar();
    
    // Instantiate later in constructor because the map support loads its icons in there and we need
    // to have the global app instance available for that
    private BasicProperties basic_properties;
    private ExtendedProperties extended_properties = new ExtendedProperties();
    private Gtk.Revealer extended_properties_revealer = new Gtk.Revealer();
    
    private Gtk.Stack stack = new Gtk.Stack();
    private Gtk.Box layout = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
    private Gtk.Box right_vbox;
    private Gtk.Revealer toolbar_revealer = new Gtk.Revealer ();
    
    public LibraryWindow(ProgressMonitor progress_monitor) {
        base();

        // prep sidebar and add roots
        sidebar_tree = new Sidebar.Tree();
        
        sidebar_tree.page_created.connect(on_page_created);
        sidebar_tree.destroying_page.connect(on_destroying_page);
        sidebar_tree.entry_selected.connect(on_sidebar_entry_selected);
        sidebar_tree.selected_entry_removed.connect(on_sidebar_selected_entry_removed);
        
        sidebar_tree.graft(library_branch, SidebarRootPosition.LIBRARY);
        sidebar_tree.graft(camera_branch, SidebarRootPosition.CAMERAS);
        sidebar_tree.graft(saved_search_branch, SidebarRootPosition.SAVED_SEARCH);
        sidebar_tree.graft(events_branch, SidebarRootPosition.EVENTS);
        sidebar_tree.graft(import_roll_branch, SidebarRootPosition.IMPORT_ROLL);
        sidebar_tree.graft(folders_branch, SidebarRootPosition.FOLDERS);
        sidebar_tree.graft(faces_branch, SidebarRootPosition.FACES);
        sidebar_tree.graft(tags_branch, SidebarRootPosition.TAGS);
        sidebar_tree.finish();
        
        properties_scheduler = new OneShotScheduler("LibraryWindow properties",
            on_update_properties_now);

        // setup search bar and add its accelerators to the window
        search_toolbar = new Gtk.Revealer();
        search_toolbar.set_child (new SearchFilterToolbar(search_actions));

        // create the main layout & start at the Library page
        basic_properties = new BasicProperties();
        create_layout(library_branch.photos_entry.get_page());
        
        // settings that should persist between sessions
        load_configuration();
        
        foreach (MediaSourceCollection media_sources in MediaCollectionRegistry.get_instance().get_all()) {
            media_sources.trashcan_contents_altered.connect(on_trashcan_contents_altered);
            media_sources.items_altered.connect(on_media_altered);
        }
        
        MetadataWriter.get_instance().progress.connect(on_metadata_writer_progress);
        
        LibraryMonitor? monitor = LibraryMonitorPool.get_instance().get_monitor();
        if (monitor != null)
            on_library_monitor_installed(monitor);
        
        LibraryMonitorPool.get_instance().monitor_installed.connect(on_library_monitor_installed);
        LibraryMonitorPool.get_instance().monitor_destroyed.connect(on_library_monitor_destroyed);
        
        CameraTable.get_instance().camera_added.connect(on_camera_added);
        
        // Need to re-install F8 here as it will overwrite the binding created
        // by the menu
        const string[] accels = { "<Primary>f", "F8", null };
        Application.set_accels_for_action("win.CommonDisplaySearchbar", accels);


        // Drop target for import through drag and drop
        var drop_target = new Gtk.DropTargetAsync(new Gdk.ContentFormats.for_gtype(typeof(Gdk.FileList)),
                Gdk.DragAction.COPY | Gdk.DragAction.LINK | Gdk.DragAction.ASK);
        drop_target.drop.connect (on_drop);
        drop_target.accept.connect (on_accept_drop);
        ((Gtk.Widget)this).add_controller(drop_target);
    }

    ~LibraryWindow() {
        sidebar_tree.page_created.disconnect(on_page_created);
        sidebar_tree.destroying_page.disconnect(on_destroying_page);
        sidebar_tree.entry_selected.disconnect(on_sidebar_entry_selected);
        sidebar_tree.selected_entry_removed.disconnect(on_sidebar_selected_entry_removed);
        
        unsubscribe_from_basic_information(get_current_page());

        foreach (MediaSourceCollection media_sources in MediaCollectionRegistry.get_instance().get_all()) {
            media_sources.trashcan_contents_altered.disconnect(on_trashcan_contents_altered);
            media_sources.items_altered.disconnect(on_media_altered);
        }
        
        MetadataWriter.get_instance().progress.disconnect(on_metadata_writer_progress);
        
        LibraryMonitor? monitor = LibraryMonitorPool.get_instance().get_monitor();
        if (monitor != null)
            on_library_monitor_destroyed(monitor);
        
        LibraryMonitorPool.get_instance().monitor_installed.disconnect(on_library_monitor_installed);
        LibraryMonitorPool.get_instance().monitor_destroyed.disconnect(on_library_monitor_destroyed);
        
        CameraTable.get_instance().camera_added.disconnect(on_camera_added);
    }

    private void on_library_monitor_installed(LibraryMonitor monitor) {
        debug("on_library_monitor_installed: %s", monitor.get_root().get_path());
        
        monitor.discovery_started.connect(on_library_monitor_discovery_started);
        monitor.discovery_completed.connect(on_library_monitor_discovery_completed);
        monitor.closed.connect(on_library_monitor_discovery_completed);
        monitor.auto_update_progress.connect(on_library_monitor_auto_update_progress);
        monitor.auto_import_preparing.connect(on_library_monitor_auto_import_preparing);
        monitor.auto_import_progress.connect(on_library_monitor_auto_import_progress);
    }
    
    private void on_library_monitor_destroyed(LibraryMonitor monitor) {
        debug("on_library_monitor_destroyed: %s", monitor.get_root().get_path());
        
        monitor.discovery_started.disconnect(on_library_monitor_discovery_started);
        monitor.discovery_completed.disconnect(on_library_monitor_discovery_completed);
        monitor.closed.disconnect(on_library_monitor_discovery_completed);
        monitor.auto_update_progress.disconnect(on_library_monitor_auto_update_progress);
        monitor.auto_import_preparing.disconnect(on_library_monitor_auto_import_preparing);
        monitor.auto_import_progress.disconnect(on_library_monitor_auto_import_progress);
    }

    private const GLib.ActionEntry[] common_actions = {
        // Normal actions
        { "CommonFileImport", on_file_import },
        { "CommonPreferences", on_preferences },
        { "CommonEmptyTrash", on_empty_trash },
        { "CommonJumpToEvent", on_jump_to_event },
        { "CommonFind", on_find },
        { "CommonNewSearch", on_new_search },

        // Toggle actions
        { "CommonDisplayBasicProperties", on_action_toggle, null, "false", on_display_basic_properties },
        { "CommonDisplayExtendedProperties", on_action_toggle, null, "false", on_display_extended_properties },

        { "CommonDisplaySearchbar", null, null, "false", on_display_searchbar },
        { "CommonDisplaySidebar", on_action_toggle, null, "true", on_display_sidebar },
        { "CommonDisplayToolbar", null, null, "true", on_display_toolbar },

        { "CommonSortEvents", on_action_radio, "s", "'ascending'", on_events_sort_changed }
    };

    protected override void add_actions () {
        base.add_actions ();
        this.add_action_entries (common_actions, this);
        this.add_action_entries (search_actions.get_actions (), search_actions);

        lookup_action ("CommonDisplaySearchbar").change_state (Config.Facade.get_instance().get_display_search_bar());
        lookup_action ("CommonDisplaySidebar").change_state (is_sidebar_visible ());
        lookup_action ("CommonDisplayToolbar").change_state (is_toolbar_visible ());
    }

    protected override void switched_pages(Page? old_page, Page? new_page) {
        base.switched_pages(old_page, new_page);

        // monitor when the ViewFilter is changed in any page
        if (old_page != null) {
            old_page.get_view().view_filter_installed.disconnect(on_view_filter_installed);
            old_page.get_view().view_filter_removed.disconnect(on_view_filter_removed);
        }

        if (new_page != null) {
            new_page.get_view().view_filter_installed.connect(on_view_filter_installed);
            new_page.get_view().view_filter_removed.connect(on_view_filter_removed);
        }

        search_actions.monitor_page_contents(old_page, new_page);
    }

    private void on_view_filter_installed(ViewFilter filter) {
        filter.refresh.connect(on_view_filter_refreshed);
    }

    private void on_view_filter_removed(ViewFilter filter) {
        filter.refresh.disconnect(on_view_filter_refreshed);
    }

    private void on_view_filter_refreshed() {
        // if view filter is reset to show all items, do nothing (leave searchbar in current
        // state)
        if (!get_current_page().get_view().are_items_filtered_out())
            return;

        // always show the searchbar when items are filtered
        var action = this.lookup_action ("CommonDisplaySearchbar");

        if (action != null)
            action.change_state (true);
    }

    // show_all() may make visible certain items we wish to keep programmatically hidden
    public new void present() {
        base.present();

        var basic_properties_action = get_current_page ().get_common_action
            ("CommonDisplayBasicProperties");
        assert(basic_properties_action != null);

        if (!basic_properties_action.get_state().get_boolean())
            bottom_frame.set_visible(false);

        // Make sure rejected pictures are not being displayed on startup
        CheckerboardPage? current_page = get_current_page() as CheckerboardPage;
        if (current_page != null)
            init_view_filter(current_page);

        toggle_search_bar(should_show_search_bar(), current_page);

        // Sidebar
        set_sidebar_visible(is_sidebar_visible());
    }

    public static LibraryWindow get_app() {
        assert(instance is LibraryWindow);

        return (LibraryWindow) instance;
    }

    // This may be called before Debug.init(), so no error logging may be made
    public static bool is_mount_uri_supported(string uri) {
        foreach (string scheme in SUPPORTED_MOUNT_SCHEMES) {
            if (uri.has_prefix(scheme))
                return true;
        }

        return false;
    }

    public override string get_app_role() {
        return Resources.APP_LIBRARY_ROLE;
    }
    
    public void rename_tag_in_sidebar(Tag tag) {
        Tags.SidebarEntry? entry = tags_branch.get_entry_for_tag(tag);
        if (entry != null)
            sidebar_tree.rename_entry_in_place(entry);
        else
            debug("No tag entry found for rename");
    }

    public void rename_event_in_sidebar(Event event) {
        Events.EventEntry? entry = events_branch.get_entry_for_event(event);
        if (entry != null)
            sidebar_tree.rename_entry_in_place(entry);
        else
            debug("No event entry found for rename");
    }

    public void rename_search_in_sidebar(SavedSearch search) {
        Searches.SidebarEntry? entry = saved_search_branch.get_entry_for_saved_search(search);
        if (entry != null)
            sidebar_tree.rename_entry_in_place(entry);
        else
            debug("No search entry found for rename");
    }
    
    public void rename_face_in_sidebar(Face face) {
        Faces.SidebarEntry? entry = faces_branch.get_entry_for_face(face);
        if (entry != null)
            sidebar_tree.rename_entry_in_place(entry);
        else
            debug("No face found for rename");
    }
    
    protected override void on_quit() {
        Config.Facade.get_instance().set_library_window_state(maximized, dimensions);

        Config.Facade.get_instance().set_sidebar_position(client_paned.position);

        base.on_quit();
    }

    private Photo? get_start_fullscreen_photo(CollectionPage page) {
        ViewCollection view = page.get_view();

        // if a selection is present, use the first selected LibraryPhoto, otherwise do
        // nothing; if no selection present, use the first LibraryPhoto
        Gee.List<DataSource>? sources = (view.get_selected_count() > 0)
            ? view.get_selected_sources_of_type(typeof(LibraryPhoto))
            : view.get_sources_of_type(typeof(LibraryPhoto));

        return (sources != null && sources.size != 0)
            ? (Photo) sources[0] : null;
    }

    private bool get_fullscreen_photo(Page page, out CollectionPage collection, out Photo start,
        out ViewCollection? view_collection = null) {
        collection = null;
        start = null;
        view_collection = null;
        
        // fullscreen behavior depends on the type of page being looked at
        if (page is CollectionPage) {
            collection = (CollectionPage) page;
            Photo? photo = get_start_fullscreen_photo(collection);
            if (photo == null)
                return false;

            start = photo;
            view_collection = null;
            
            return true;
        }
        
        if (page is EventsDirectoryPage) {
            ViewCollection view = page.get_view();
            if (view.get_count() == 0)
                return false;
            
            Event? event = (Event?) ((DataView) view.get_at(0)).get_source();
            if (event == null)
                return false;

            Events.EventEntry? entry = events_branch.get_entry_for_event(event);
            if (entry == null)
                return false;

            collection = (EventPage) entry.get_page();
            Photo? photo = get_start_fullscreen_photo(collection);
            if (photo == null)
                return false;
            
            start = photo;
            view_collection = null;
            
            return false;
        }

        if (page is LibraryPhotoPage) {
            LibraryPhotoPage photo_page = (LibraryPhotoPage) page;
            
            CollectionPage? controller = photo_page.get_controller_page();
            if (controller == null)
                return false;
            
            if (!photo_page.has_photo())
                return false;

            collection = controller;
            start = photo_page.get_photo();
            view_collection = photo_page.get_view();

            return true;
        }
        
        return false;
    }
    
    protected override void on_fullscreen() {
        Page? current_page = get_current_page();
        if (current_page == null)
            return;
        
        CollectionPage collection;
        Photo start;
        ViewCollection? view = null;
        if (!get_fullscreen_photo(current_page, out collection, out start, out view))
            return;
        
        FullscreenPhotoPage fs_photo = new FullscreenPhotoPage(collection, start, view);

        go_fullscreen(fs_photo);
    }

    async void do_file_import(ListModel files, bool recursive) {
        var result = yield copy_files_dialog();
        if (result != Gtk.ResponseType.CANCEL) {
            yield dispatch_import_jobs(files, "folders",
                result == Gtk.ResponseType.ACCEPT, recursive);
        }
    }

    private void on_file_import() {
        var import_dialog = new Gtk.FileChooserNative(_("Import From Folder"), null,
            Gtk.FileChooserAction.SELECT_FOLDER, Resources.OK_LABEL, Resources.CANCEL_LABEL);

        import_dialog.set_modal(true);
        import_dialog.set_transient_for(this);
        import_dialog.set_select_multiple(true);
        try {
            import_dialog.set_current_folder(import_dir);
        } catch (Error err) {
            debug("Failed to set dialog to old import dir : %s", err.message);
        }

        import_dialog.add_choice("recursive-import", _("Recurse Into Subfolders"), null, null);
        import_dialog.set_choice("recursive-import", import_recursive.to_string());
        import_dialog.show();
        import_dialog.response.connect((dialog, response) => {
            import_dir = import_dialog.get_current_folder();
            import_recursive = bool.parse(import_dialog.get_choice("recursive-import"));
            if (response == Gtk.ResponseType.ACCEPT) {
                do_file_import.begin(import_dialog.get_files(), import_recursive);
            }
            import_dialog.destroy();
        });
    }
    
    protected override void update_common_action_availability(Page? old_page, Page? new_page) {
        base.update_common_action_availability(old_page, new_page);
        
        bool is_checkerboard = new_page is CheckerboardPage;

        set_common_action_sensitive("CommonDisplaySearchbar", is_checkerboard);
        set_common_action_sensitive("CommonFind", is_checkerboard);
    }
    
    protected override void update_common_actions(Page page, int selected_count, int count) {
        // see on_fullscreen for the logic here ... both CollectionPage and EventsDirectoryPage
        // are CheckerboardPages (but in on_fullscreen have to be handled differently to locate
        // the view controller)
        CollectionPage collection;
        Photo start;
        bool can_fullscreen = get_fullscreen_photo(page, out collection, out start);
        
        set_common_action_sensitive("CommonEmptyTrash", can_empty_trash());
        set_common_action_visible("CommonJumpToEvent", true);
        set_common_action_sensitive("CommonJumpToEvent", can_jump_to_event());
        set_common_action_sensitive("CommonFullscreen", can_fullscreen);
        
        base.update_common_actions(page, selected_count, count);
    }
    
    private void on_trashcan_contents_altered() {
        set_common_action_sensitive("CommonEmptyTrash", can_empty_trash());
    }
    
    private bool can_empty_trash() {
        return (LibraryPhoto.global.get_trashcan_count() > 0) || (Video.global.get_trashcan_count() > 0);
    }

    private void on_empty_trash() {
        Gee.ArrayList<MediaSource> to_remove = new Gee.ArrayList<MediaSource>();
        to_remove.add_all(LibraryPhoto.global.get_trashcan_contents());
        to_remove.add_all(Video.global.get_trashcan_contents());
        
        remove_from_app.begin(to_remove, _("Empty Trash"),  _("Emptying Trash…"), (source, res) => {
            remove_from_app.end(res);
            AppWindow.get_command_manager().reset();
        });
    }
    
    private void on_new_search() {
        (new SavedSearchDialog()).present();
    }
    
    private bool can_jump_to_event() {
        ViewCollection view = get_current_page().get_view();
        if (view.get_selected_count() == 1) {
            DataSource selected_source = view.get_selected_source_at(0);
            if (selected_source is Event)
                return true;
            else if (selected_source is MediaSource)
                return ((MediaSource) view.get_selected_source_at(0)).get_event() != null;
            else
                return false;
        } else {
            return false;
        }
    }
    
    private void on_jump_to_event() {
        ViewCollection view = get_current_page().get_view();
        
        if (view.get_selected_count() != 1)
            return;
        
        MediaSource? media = view.get_selected_source_at(0) as MediaSource;
        if (media == null)
            return;
        
        if (media.get_event() != null)
            switch_to_event(media.get_event());
    }
    
    private void on_find() {
        var action = this.lookup_action ("CommonDisplaySearchbar");
        action.change_state (true);

        // give it focus (which should move cursor to the text entry control)
        ((SearchFilterToolbar)search_toolbar.get_child()).take_focus();
    }
    
    private void on_media_altered() {
        set_common_action_sensitive("CommonJumpToEvent", can_jump_to_event());
    }
    
    private void on_clear_search() {
        if (is_search_toolbar_visible)
            search_actions.reset();
    }
    
    public int get_events_sort() {
        var action = this.lookup_action ("CommonSortEvents") as GLib.SimpleAction;
        
        return (action != null) ? (action.state.get_string () == SORT_EVENTS_ORDER_ASCENDING)
            ? 0 : 1
            : 1;
    }

    private void on_events_sort_changed(GLib.SimpleAction action, Variant? value) {
        
        Config.Facade.get_instance().set_events_sort_ascending(
            value.get_string () == SORT_EVENTS_ORDER_ASCENDING);

        action.set_state (value);
    }
    
    private void on_preferences() {
        PreferencesDialog.show_preferences();
    }
    
    private void on_display_basic_properties(GLib.SimpleAction action, Variant? value) {
        bool display = value.get_boolean ();

        if (display) {
            basic_properties.update_properties(get_current_page());
            bottom_frame.set_visible(true);
        } else {
            if (sidebar_paned.get_end_child() != null) {
                bottom_frame.set_visible(false);
            }
        }

        // sync the setting so it will persist
        Config.Facade.get_instance().set_display_basic_properties(display);
        action.set_state (value);
    }

    private void on_action_toggle (GLib.Action action, Variant? value) {
        Variant new_state = ! (bool) action.get_state ();
        action.change_state (new_state);
    }

    private void on_action_radio (GLib.Action action, Variant? value) {
        action.change_state (value);
    }

    private void on_display_extended_properties(GLib.SimpleAction action, Variant? value) {
        bool display = value.get_boolean ();

        if (display) {
            extended_properties.update_properties(get_current_page());
        }
        extended_properties_revealer.set_reveal_child(display);

        action.set_state (value);
        Config.Facade.get_instance().set_display_extended_properties(display);
    }
    
    private void on_display_searchbar(GLib.SimpleAction action, Variant? value) {
        bool is_shown = value.get_boolean ();

        Config.Facade.get_instance().set_display_search_bar(is_shown);
        show_search_bar(is_shown);
        action.set_state (is_shown);
    }
    
    public void show_search_bar(bool display) {
        if (!(get_current_page() is CheckerboardPage))
            return;
            
        is_search_toolbar_visible = display;
        toggle_search_bar(should_show_search_bar(), get_current_page() as CheckerboardPage);
        if (!display)
            search_actions.reset();
    }
    
    private void on_display_sidebar(GLib.SimpleAction action, Variant? variant) {
        set_sidebar_visible(variant.get_boolean ());

        action.set_state (variant);
    }

    private void set_sidebar_visible(bool visible) {
        sidebar_paned.set_visible(visible);
        Config.Facade.get_instance().set_display_sidebar(visible);
    }
    
    private bool is_sidebar_visible() {
        return Config.Facade.get_instance().get_display_sidebar();
    }
    
    private void on_display_toolbar (GLib.SimpleAction action, Variant? variant) {
        set_toolbar_visible (variant.get_boolean ());

        action.set_state (variant);
    }

    private void set_toolbar_visible (bool visible) {
        if (get_current_page() == null) {
            return;
        }

        var toolbar = get_current_page ().get_toolbar ();
        if (toolbar != null) {
            this.toolbar_revealer.set_reveal_child (visible);
        }
        Config.Facade.get_instance().set_display_toolbar (visible);
    }

    private bool is_toolbar_visible () {
        return Config.Facade.get_instance ().get_display_toolbar ();
    }

    public void enqueue_batch_import(BatchImport batch_import, bool allow_user_cancel) {
        library_branch.import_queue_entry.enqueue_and_schedule(batch_import, allow_user_cancel);
    }
    
    private void import_reporter(ImportManifest manifest) {
        ImportUI.report_manifest.begin(manifest, true);
    }
    
    private async void dispatch_import_jobs(ListModel uris, string job_name, bool copy_to_library, bool recurse) {
        if (AppDirs.get_import_dir().get_path() == Environment.get_home_dir() && notify_library_is_home_dir) {
            var dialog = new Gtk.AlertDialog(
                _("Shotwell is configured to import photos to your home directory.\n" + 
                "We recommend changing this in Edit %s Preferences.\n" + 
                "Do you want to continue importing photos?"), "▸");
            dialog.set_buttons({_("_Import"), _("_Cancel")});
            dialog.set_modal(true);
            int result = -1;
            try {
                result = yield dialog.choose(AppWindow.get_instance(), null);
            } catch (Error error) {
                // Do nothing
            }
            
            if (result == -1 || result == 1)
                return;
            
            notify_library_is_home_dir = false;
        }
        
        Gee.ArrayList<FileImportJob> jobs = new Gee.ArrayList<FileImportJob>();
        for (var i = 0; i < uris.get_n_items(); i++) {
            var file_or_dir = (File) uris.get_item(i);
            if (file_or_dir.get_path() == null) {
                // TODO: Specify which directory/file.
                AppWindow.error_message(_("Photos cannot be imported from this directory."));
                
                continue;
            }

            jobs.add(new FileImportJob(file_or_dir, copy_to_library && !AppDirs.is_in_import_dir(file_or_dir), recurse));
        }
        
        if (jobs.size > 0) {
            BatchImport batch_import = new BatchImport(jobs, job_name, import_reporter);
            enqueue_batch_import(batch_import, true);
            switch_to_import_queue_page();
        }
    }
    
    #if 0
    private Gdk.DragAction get_drag_action() {
        Gdk.ModifierType mask;

        var seat = Gdk.Display.get_default().get_default_seat();
        get_window().get_device_position(seat.get_pointer(), null, null, out mask);

        bool ctrl = (mask & Gdk.ModifierType.CONTROL_MASK) != 0;
        bool alt = (mask & Gdk.ModifierType.MOD1_MASK) != 0;
        bool shift = (mask & Gdk.ModifierType.SHIFT_MASK) != 0;
        
        if (ctrl && !alt && !shift)
            return Gdk.DragAction.COPY;
        else if (!ctrl && alt && !shift)
            return Gdk.DragAction.ASK;
        else if (ctrl && !alt && shift)
            return Gdk.DragAction.LINK;
        else
            return Gdk.DragAction.DEFAULT;
    }
    
    public override bool drag_motion(Gdk.DragContext context, int x, int y, uint time) {
        Gdk.Atom target = Gtk.drag_dest_find_target(this, context, Gtk.drag_dest_get_target_list(this));
        if (target == Gdk.Atom.NONE) {
            debug("drag target is GDK_NONE");
            Gdk.drag_status(context, 0, time);
            
            return true;
        }
        
        // internal drag
        if (Gtk.drag_get_source_widget(context) != null) {
            Gdk.drag_status(context, Gdk.DragAction.PRIVATE, time);
            
            return true;
        }

        // since we cannot set a default action, we must set it when we spy a drag motion
        Gdk.DragAction drag_action = get_drag_action();
        
        if (drag_action == Gdk.DragAction.DEFAULT)
            drag_action = Gdk.DragAction.ASK;
        
        Gdk.drag_status(context, drag_action, time);

        return true;
    }
    #endif
    

    private bool on_drop(Gtk.DropTargetAsync drop_target, Gdk.Drop drop, double x, double y) {
        external_drop_handler.begin(drop_target, drop);

        return true;
    }

    private bool on_accept_drop(Gtk.DropTargetAsync drop_target, Gdk.Drop drop) {
        if (drop.get_formats().contain_gtype(typeof(DragAndDropHandler.MediaListWrapper))) {
            debug ("This drop is our own drop, will not accept");
            return false;
        }
        
        return true;
    }

    private class FileListModelWrapper : Object, ListModel {
        Gdk.FileList list;
        SList<weak File> files;

        public FileListModelWrapper(Gdk.FileList list) {
            Object();
            this.list = list;
            this.files = list.get_files();
        }

		public GLib.Object? get_item (uint position) {
            return position < this.files.length() ? this.files.nth_data(position) : null;
        }
		public GLib.Type get_item_type () {
            return typeof(File);
        }
		public uint get_n_items () {
            return files.length();
        }
    }
    
    private async void external_drop_handler(Gtk.DropTargetAsync drop_target, Gdk.Drop drop) {
        var selected_action = drop.get_actions();

        if (selected_action == Gdk.DragAction.ASK || !selected_action.is_unique()) {
            // Default action is to link, unless one or more URIs are external to the library
            var result = yield copy_files_dialog();
            
            switch (result) {
                case Gtk.ResponseType.ACCEPT:
                    selected_action = Gdk.DragAction.COPY;
                break;
                
                case Gtk.ResponseType.REJECT:
                    selected_action = Gdk.DragAction.LINK;
                break;
                
                default:
                    return;
            }
        }
        try {
            var value = yield drop.read_value_async(typeof(Gdk.FileList), Priority.DEFAULT, null);
            var file_list = (Gdk.FileList)value.get_boxed();
            
            yield dispatch_import_jobs(new FileListModelWrapper(file_list), "drag-and-drop", selected_action == Gdk.DragAction.COPY, true);
            drop.finish(selected_action);
        } catch (Error err) {
            warning("Failed to read the drop data: %s", err.message);
            drop.finish(Gdk.DragAction.NONE);
        }
    }
    
    public void switch_to_library_page() {
        switch_to_page(library_branch.photos_entry.get_page());
    }
    
    public void switch_to_event(Event event) {
        Events.EventEntry? entry = events_branch.get_entry_for_event(event);
        if (entry != null)
            switch_to_page(entry.get_page());
    }
    
    public void switch_to_tag(Tag tag) {
        Tags.SidebarEntry? entry = tags_branch.get_entry_for_tag(tag);
        if (entry != null)
            switch_to_page(entry.get_page());
    }
    
    public void switch_to_saved_search(SavedSearch search) {
        Searches.SidebarEntry? entry = saved_search_branch.get_entry_for_saved_search(search);
        if (entry != null)
            switch_to_page(entry.get_page());
    }
    
    public void switch_to_photo_page(CollectionPage controller, Photo current) {
        assert(controller.get_view().get_view_for_source(current) != null);
        if (photo_page == null) {
            photo_page = new LibraryPhotoPage();
            add_to_stack(photo_page);
            
            // need to do this to allow the event loop a chance to map and realize the page
            // before switching to it
            spin_event_loop();
        }
        
        photo_page.display_for_collection(controller, current);
        switch_to_page(photo_page);
    }
    
    public void switch_to_import_queue_page() {
        switch_to_page(library_branch.import_queue_entry.get_page());
    }
    
    private void on_camera_added(DiscoveredCamera camera) {
        Camera.SidebarEntry? entry = camera_branch.get_entry_for_camera(camera);
        if (entry == null)
            return;
        
        ImportPage page = (ImportPage) entry.get_page();
        
        // find the VFS mount point
        var mount = camera.get_mount();
        
        // don't unmount mass storage cameras, as they are then unavailable to gPhoto
        if (mount != null && !camera.uri.has_prefix("file://")) {
            if (page.unmount_camera(mount))
                switch_to_page(page);
            else
                error_message("Unable to unmount the camera at this time.");
        } else {
            switch_to_page(page);
        }
    }

    // This should only be called by LibraryWindow and PageStub.
    public void add_to_stack(Page page) {
        // need to show all before handing over to stack
        page.set_visible(true);
        
        stack.add_child(page);
        // need to show_all() after pages are added and removed
        stack.set_visible(true);
    }
    
    private void remove_from_stack(Page page) {
        stack.remove(page);
        
        // need to show_all() after pages are added and removed
        stack.set_visible(true);
    }
    
    // check for settings that should persist between instances
    private void load_configuration() {
        var basic_display_action = lookup_action("CommonDisplayBasicProperties");
        assert(basic_display_action != null);
        basic_display_action.change_state (Config.Facade.get_instance().get_display_basic_properties());

        var extended_display_action = lookup_action("CommonDisplayExtendedProperties");
        assert(extended_display_action != null);
        extended_display_action.change_state(Config.Facade.get_instance().get_display_extended_properties());
        
        var search_bar_display_action = lookup_action("CommonDisplaySearchbar");
        assert(search_bar_display_action != null);
        search_bar_display_action.change_state(Config.Facade.get_instance().get_display_search_bar());

        var sort_events_action = lookup_action("CommonSortEvents");
        assert(sort_events_action != null);
        
        // Ticket #3321 - Event sorting order wasn't saving on exit.
        // Instead of calling set_active against one of the toggles, call
        // set_current_value against the entire radio group...
        string event_sort_val = Config.Facade.get_instance().get_events_sort_ascending() ? SORT_EVENTS_ORDER_ASCENDING :
            SORT_EVENTS_ORDER_DESCENDING;
        
        sort_events_action.change_state (event_sort_val);
    }

    private void on_library_monitor_discovery_started() {
        background_progress_bar.start(_("Updating library…"), BackgroundProgressBar.Priority.STARTUP_SCAN);
    }
    
    private void on_library_monitor_discovery_completed() {
        background_progress_bar.stop(BackgroundProgressBar.Priority.STARTUP_SCAN, true);
    }
    
    private void on_library_monitor_auto_update_progress(int completed_files, int total_files) {
        if (total_files < MIN_PROGRESS_BAR_FILES)
            background_progress_bar.clear(BackgroundProgressBar.Priority.REALTIME_UPDATE);
        else {
            background_progress_bar.update(_("Updating library…"), BackgroundProgressBar.Priority.REALTIME_UPDATE,
                completed_files, total_files);
        }
    }
    
    private void on_library_monitor_auto_import_preparing() {
        background_progress_bar.start(_("Preparing to auto-import photos…"),
            BackgroundProgressBar.Priority.REALTIME_UPDATE);
    }
    
    private void on_library_monitor_auto_import_progress(uint64 completed_bytes, uint64 total_bytes) {
        background_progress_bar.update(_("Auto-importing photos…"),
            BackgroundProgressBar.Priority.REALTIME_UPDATE, completed_bytes, total_bytes);
    }
    
    private void on_metadata_writer_progress(uint completed, uint total) {
        if (total < MIN_PROGRESS_BAR_FILES)
            background_progress_bar.clear(BackgroundProgressBar.Priority.METADATA_WRITER);
        else {
            background_progress_bar.update(_("Writing metadata to files…"),
                BackgroundProgressBar.Priority.METADATA_WRITER, completed, total);
        }
    }
    
    private void create_layout(Page start_page) {
        
        // put the sidebar in a scrolling window
        Gtk.ScrolledWindow scrolled_sidebar = new Gtk.ScrolledWindow();
        scrolled_sidebar.vexpand = true;
        scrolled_sidebar.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
        scrolled_sidebar.set_child(sidebar_tree);
        
        background_progress_frame.set_child(background_progress_bar);
        background_progress_frame.set_transition_type(Gtk.RevealerTransitionType.SLIDE_UP);
        background_progress_frame.halign = Gtk.Align.FILL;
        background_progress_frame.valign = Gtk.Align.END;
        background_progress_frame.vexpand = false;
        background_progress_frame.hexpand = true;
        background_progress_bar.bind_property("should-be-visible", background_progress_frame, "reveal-child", GLib.BindingFlags.DEFAULT);

        // pad the bottom frame (properties)
        basic_properties.halign = Gtk.Align.FILL;
        basic_properties.valign = Gtk.Align.CENTER;
        basic_properties.hexpand = true;
        basic_properties.margin_top = 10;
        basic_properties.margin_bottom = 10;
        basic_properties.margin_start = 6;
        basic_properties.margin_end = 6;

        bottom_frame.set_child(basic_properties);
        bottom_frame.remove_css_class("frame");
        
        // "attach" the progress bar to the sidebar tree, so the movable ridge is to resize the
        // top two and the basic information pane
        top_section.prepend(scrolled_sidebar);
        top_section.append(background_progress_frame);

        sidebar_paned.set_start_child(top_section);
        sidebar_paned.set_end_child(bottom_frame);
        sidebar_paned.set_shrink_end_child(false);
        sidebar_paned.set_resize_end_child(false);
        
        right_vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
        right_vbox.set_name("mainwindow-content");
        right_vbox.append(search_toolbar);
        var stack_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
        stack_box.append(stack);
        right_vbox.append(stack_box);
        right_vbox.append(toolbar_revealer);
        var toast_host = toast_overlay.attach(right_vbox);
        
        client_paned = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
        client_paned.set_start_child(sidebar_paned);
        sidebar_tree.set_size_request(SIDEBAR_MIN_WIDTH, -1);
        client_paned.set_end_child(toast_host);
        client_paned.set_position(Config.Facade.get_instance().get_sidebar_position());
        client_paned.set_shrink_start_child(false);
        client_paned.set_shrink_end_child(false);
        // TODO: Calc according to layout's size, to give sidebar a maximum width
        stack.set_size_request(PAGE_MIN_WIDTH, -1);
        var scrolled = new Gtk.ScrolledWindow();
        scrolled.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
        scrolled.set_child(extended_properties);
        extended_properties_revealer.set_child(scrolled);
        extended_properties_revealer.set_transition_type(Gtk.RevealerTransitionType.SLIDE_LEFT);
        extended_properties_revealer.halign = Gtk.Align.END;
        extended_properties_revealer.valign = Gtk.Align.FILL;

        extended_properties.vexpand = true;
        extended_properties.set_margin_top (9);
        extended_properties.set_margin_bottom (9);
        extended_properties.set_margin_start (9);
        extended_properties.set_margin_end (9);
        scrolled.set_size_request(EXTENDED_INFO_MIN_WIDTH, -1);

        stack_box.append(extended_properties_revealer);
        extended_properties_revealer.halign = Gtk.Align.END;
        extended_properties_revealer.hexpand = false;
        if (Config.Facade.get_instance().get_display_extended_properties()) {
            extended_properties_revealer.set_reveal_child(true);
        } else {
            extended_properties_revealer.set_reveal_child(false);
        }

        layout.append(client_paned);
        
        set_child(layout);

        switch_to_page(start_page);
        start_page.grab_focus();
    }
    
    public override void set_current_page(Page page) {
        // switch_to_page() will call base.set_current_page(), maintain the semantics of this call
        switch_to_page(page);
    }
    
    public void set_page_switching_enabled(bool should_enable) {
        page_switching_enabled = should_enable;
    }
    
    public void switch_to_page(Page page) {
        if (!page_switching_enabled)
            return;
        
        if (page == get_current_page())
            return;
        
        Page current_page = get_current_page();
        if (current_page != null) {
            set_menubar(null);

            var toolbar = current_page.get_toolbar();
            if (toolbar != null)
                toolbar_revealer.set_child(null);

            current_page.switching_from();
            
            // see note below about why the sidebar is uneditable while the LibraryPhotoPage is
            // visible
            if (current_page is LibraryPhotoPage)
                sidebar_tree.enable_editing();
            
            // old page unsubscribes to these signals (new page subscribes below)
            unsubscribe_from_basic_information(current_page);
        }
        
        stack.set_visible_child(page);
        
        // do this prior to changing selection, as the change will fire a cursor-changed event,
        // which will then call this function again
        base.set_current_page(page);
        
        // if the visible page is the LibraryPhotoPage, we need to prevent single-click inline
        // renaming in the sidebar because a single click while in the LibraryPhotoPage indicates
        // the user wants to return to the controlling page ... that is, in this special case, the
        // sidebar cursor is set not to the 'current' page, but the page the user came from
        if (page is LibraryPhotoPage)
            sidebar_tree.disable_editing();
        
        // Update search filter to new page.
        toggle_search_bar(should_show_search_bar(), page as CheckerboardPage);
        
        // Not all pages have sidebar entries
        Sidebar.Entry? entry = page_map.get(page);
        if (entry != null) {
            // if the corresponding sidebar entry is an expandable entry and wants to be
            // expanded when it's selected, then expand it
            Sidebar.ExpandableEntry expandable_entry = entry as Sidebar.ExpandableEntry;
            if (expandable_entry != null && expandable_entry.expand_on_select())
                sidebar_tree.expand_to_entry(entry);

            sidebar_tree.place_cursor(entry, true);
        }
        
        on_update_properties();
        
        if (page is CheckerboardPage)
            init_view_filter((CheckerboardPage)page);
        
        page.set_visible(true);
        
        // subscribe to these signals for each event page so basic properties display will update
        subscribe_for_basic_information(get_current_page());
        
        page.switched_to();

        set_menubar (page.get_menubar ());
        var old = get_settings().gtk_shell_shows_menubar;
        get_settings().gtk_shell_shows_menubar = !old;
        get_settings().gtk_shell_shows_menubar = old;
        
        var toolbar = page.get_toolbar();
        if (toolbar != null) {
            toolbar_revealer.set_child(toolbar);
            toolbar.set_visible(true);
            toolbar_revealer.set_reveal_child (this.is_toolbar_visible ());
        }

        page.ready();
    }

    private void init_view_filter(CheckerboardPage page) {
        ((SearchFilterToolbar)search_toolbar.get_child()).set_view_filter(page.get_search_view_filter());
        page.get_view().install_view_filter(page.get_search_view_filter());
    }

    private bool should_show_search_bar() {
        return (get_current_page() is CheckerboardPage) ? is_search_toolbar_visible : false;
    }
    
    // Turns the search bar on or off.  Note that if show is true, page must not be null.
    private void toggle_search_bar(bool show, CheckerboardPage? page = null) {
        search_toolbar.set_reveal_child(show);
        if (show) {
            assert(null != page);
            ((SearchFilterToolbar)search_toolbar.get_child()).set_view_filter(page.get_search_view_filter());
            page.get_view().install_view_filter(page.get_search_view_filter());
        } else {
            if (page != null)
                page.get_view().install_view_filter(new DisabledViewFilter());
        }
    }
    
    private void on_page_created(Sidebar.PageRepresentative entry, Page page) {
        assert(!page_map.has_key(page));
        page_map.set(page, entry);
        
        add_to_stack(page);
    }
    
    private void on_destroying_page(Sidebar.PageRepresentative entry, Page page) {
        // if page is the current page, switch to fallback before destroying
        if (page == get_current_page())
            switch_to_page(library_branch.photos_entry.get_page());
        
        remove_from_stack(page);
        
        bool removed = page_map.unset(page);
        assert(removed);
    }
    
    private void on_sidebar_entry_selected(Sidebar.SelectableEntry selectable) {
        Sidebar.PageRepresentative? page_rep = selectable as Sidebar.PageRepresentative;
        if (page_rep != null)
            switch_to_page(page_rep.get_page());
    }
    
    private void on_sidebar_selected_entry_removed(Sidebar.SelectableEntry selectable) {
        // if the currently selected item is removed, want to jump to fallback page (which
        // depends on the item that was selected)
        Library.LastImportSidebarEntry last_import_entry = library_branch.last_imported_entry;
        
        // Importing... -> Last Import (if available)
        if (selectable is Library.ImportQueueSidebarEntry && last_import_entry.visible) {
            switch_to_page(last_import_entry.get_page());
            
            return;
        }
        
        // Event page -> Events (master event directory)
        if (selectable is Events.EventEntry && events_branch.get_show_branch()) {
            switch_to_page(events_branch.get_master_entry().get_page());
            
            return;
        }
        
        // Any event directory -> Events (master event directory)
        if (selectable is Events.DirectoryEntry && events_branch.get_show_branch()) {
            switch_to_page(events_branch.get_master_entry().get_page());
            
            return;
        }
        
        // basic all-around default: jump to the Library page
        switch_to_page(library_branch.photos_entry.get_page());
    }
    
    private void subscribe_for_basic_information(Page page) {
        ViewCollection view = page.get_view();
        
        view.items_state_changed.connect(on_update_properties);
        view.items_altered.connect(on_update_properties);
        view.contents_altered.connect(on_update_properties);
        view.items_visibility_changed.connect(on_update_properties);
    }
    
    private void unsubscribe_from_basic_information(Page page) {
        ViewCollection view = page.get_view();
        
        view.items_state_changed.disconnect(on_update_properties);
        view.items_altered.disconnect(on_update_properties);
        view.contents_altered.disconnect(on_update_properties);
        view.items_visibility_changed.disconnect(on_update_properties);
    }
    
    private void on_update_properties() {
        properties_scheduler.at_idle();
    }
    
    private void on_update_properties_now() {
        if (bottom_frame.visible)
            basic_properties.update_properties(get_current_page());

        if (extended_properties.visible)
            extended_properties.update_properties(get_current_page());
    }
    
    public void mounted_camera_shell_notification(string uri, bool at_startup) {
        debug("mount point reported: %s", uri);
        
        // ignore unsupport mount URIs
        if (!is_mount_uri_supported(uri)) {
            debug("Unsupported mount scheme: %s", uri);
            
            return;
        }
        
        File uri_file = File.new_for_uri(uri);
        
        // find the VFS mount point
        Mount mount = null;
        try {
            mount = uri_file.find_enclosing_mount(null);
        } catch (Error err) {
            debug("%s", err.message);
            
            return;
        }
        
        // convert file: URIs into gphoto disk: URIs
        string alt_uri = null;
        if (uri.has_prefix("file://"))
            alt_uri = CameraTable.get_port_uri(uri.replace("file://", "disk:"));
        
        // we only add uris when the notification is called on startup
        if (at_startup) {
            if (!is_string_empty(uri))
                initial_camera_uris.add(uri);

            if (!is_string_empty(alt_uri))
                initial_camera_uris.add(alt_uri);
        }
    }
    
    public override bool key_press_event(Gtk.EventControllerKey event, uint keyval, uint keycode, Gdk.ModifierType modifiers) {
        if (sidebar_tree.has_focus && sidebar_tree.is_keypress_interpreted(event, keyval, keycode, modifiers)
            && event.forward(sidebar_tree)) {
            return true;
        }
        
        if (base.key_press_event(event, keyval, keycode, modifiers))
            return true;

        if (Gdk.keyval_name(keyval) == "Escape") {
            on_clear_search();
            return true;
        }
        
        return false;
    }
}

