mod commands;
mod config;
mod pdf_protocol;
pub mod state;

use state::AppState;

pub fn run() {
    tauri::Builder::default()
        .manage(AppState::new())
        .setup(|app| {
            #[cfg(debug_assertions)]
            {
                use tauri::Manager;
                if let Some(win) = app.get_webview_window("main") {
                    win.open_devtools();
                }
            }
            Ok(())
        })
        .register_uri_scheme_protocol("brittle", |ctx, req| {
            pdf_protocol::handle(ctx.app_handle(), &req)
        })
        .invoke_handler(tauri::generate_handler![
            // config
            commands::config::load_global_config,
            commands::config::save_global_config,
            commands::config::load_project_config,
            commands::config::save_project_config,
            commands::config::get_theme,
            commands::config::set_theme,
            commands::config::get_keybindings,
            commands::config::get_layout_config,
            commands::config::get_last_project,
            commands::config::get_session,
            commands::config::save_session,
            // repository
            commands::repository::create_repository,
            commands::repository::open_repository,
            commands::repository::close_repository,
            commands::repository::repository_root,
            // reference
            commands::reference::create_reference,
            commands::reference::get_reference,
            commands::reference::update_reference,
            commands::reference::delete_reference,
            commands::reference::list_references,
            commands::reference::set_field,
            commands::reference::remove_field,
            commands::reference::search_references,
            commands::reference::search_library_references,
            commands::reference::list_library_references,
            commands::reference::list_library_references_recursive,
            // library
            commands::library::create_library,
            commands::library::get_library,
            commands::library::rename_library,
            commands::library::move_library,
            commands::library::delete_library,
            commands::library::force_delete_library,
            commands::library::list_root_libraries,
            commands::library::list_child_libraries,
            commands::library::get_library_ancestors,
            commands::library::add_to_library,
            commands::library::remove_from_library,
            commands::library::list_reference_libraries,
            // annotation
            commands::annotation::create_annotation,
            commands::annotation::get_annotations,
            commands::annotation::update_annotation,
            commands::annotation::delete_annotation,
            // pdf
            commands::pdf::attach_pdf,
            commands::pdf::get_pdf_path,
            // snapshot
            commands::snapshot::create_snapshot,
            commands::snapshot::list_snapshots,
            commands::snapshot::restore_snapshot,
            commands::snapshot::has_uncommitted_changes,
            commands::snapshot::discard_changes,
            // bibtex
            commands::bibtex::export_bibtex,
            commands::bibtex::export_library_bibtex,
            // window
            commands::window::open_pdf_window,
            commands::window::close_pdf_window,
            commands::window::list_pdf_windows,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
