using Adw;
using Gtk;

public class MainWindow : Adw.ApplicationWindow
{
    private ConversationListView conversation_list_view;
    private TranscriptContainerView transcript_container_view;
    private NavigationSplitView split_view;

    private EventControllerMotion _motion_controller = new EventControllerMotion();
    private bool _motion_queued = false;

    public MainWindow () {
        Object (title: "Kordophone");

        split_view = new NavigationSplitView ();
        split_view.set_min_sidebar_width (400);
        split_view.show_content = false;
        set_content (split_view);

        var breakpoint = new Breakpoint (BreakpointCondition.parse ("max-width: 750sp"));
        breakpoint.add_setter (split_view, "collapsed", true);
        add_breakpoint (breakpoint);

        conversation_list_view = new ConversationListView ();
        conversation_list_view.conversation_selected.connect (conversation_selected);
        conversation_list_view.conversation_activated.connect (open_conversation_in_new_window);

        var conversation_list_page = new NavigationPage (conversation_list_view, "Conversations");
        split_view.sidebar = conversation_list_page;

        transcript_container_view = new TranscriptContainerView ();
        var transcript_page = new NavigationPage (transcript_container_view, "Transcript");
        split_view.content = transcript_page;

        var show_settings_action = new SimpleAction ("settings", null);
        show_settings_action.activate.connect(show_settings);
        add_action(show_settings_action);

        _motion_controller.motion.connect((x, y) => {
            queue_motion();
        });
        _motion_controller.set_propagation_phase(PropagationPhase.CAPTURE);
        split_view.add_controller(_motion_controller);
    }

    private void queue_motion() {
        if (_motion_queued) {
            return;
        }

        if (!is_active) {
            return;
        }

        _motion_queued = true;
        GLib.Timeout.add(500, () => {
            _motion_queued = false;
            on_motion();
            return false;
        });
    }

    private void on_motion() {
        var selected_conversation = transcript_container_view.transcript_view.model?.conversation;
        if (selected_conversation == null) {
            return;
        }

        var list_model = conversation_list_view.conversation_model;
        var conversation_in_list = list_model.get_conversation(selected_conversation.guid);
        if (conversation_in_list == null) {
            return;
        }
        
        if (conversation_in_list.unread_count == 0) {
            return;
        }

        try {
            GLib.message("Marking conversation as read on motion: %s", selected_conversation.guid);
            Repository.get_instance().mark_conversation_as_read(selected_conversation.guid);
        } catch (Error e) {
            GLib.warning("Failed to mark conversation as read: %s", e.message);
        }
    }

    private void show_settings () {
        var dialog = new PreferencesWindow (this);
        dialog.present (this);
    }

    private void conversation_selected(Conversation conversation) {
        TranscriptView transcript_view = transcript_container_view.transcript_view;
        if (conversation == null) {
            transcript_view.model = null;
        } else {
            if (transcript_view.model == null || transcript_view.model.conversation.guid != conversation.guid) {
                transcript_view.model = new MessageListModel (conversation);
                transcript_view.title = conversation.display_name;
                
                try {
                    Repository.get_instance().mark_conversation_as_read(conversation.guid);
                    Repository.get_instance().sync_conversation(conversation.guid);
                } catch (Error e) {
                    GLib.warning("Failed to sync conversation: %s", e.message);
                }
            }

            if (split_view.collapsed) {
                split_view.show_content = true;
            }
        }
    }

    private void open_conversation_in_new_window(Conversation conversation) {
        var view = new TranscriptContainerView();
        view.transcript_view.model = new MessageListModel (conversation);
        view.transcript_view.title = conversation.display_name;

        var window = new Adw.Window();
        window.set_default_size(750, 990);
        window.set_content(view);
        window.present();
    }
}
