//! ## Components
//!
//! file transfer activity components

use tui_realm_stdlib::components::Phantom;
use tuirealm::component::{AppComponent, Component};
use tuirealm::event::{Event, Key, KeyEvent, KeyModifiers, NoUserEvent};

use super::{Msg, TransferMsg, UiMsg};

// -- export
mod log;
mod misc;
mod popups;
mod selected_files;
mod terminal;
mod transfer;

pub use misc::FooterBar;
pub use popups::{
    ATTR_FILES, ChmodPopup, CopyPopup, DeletePopup, DisconnectPopup, ErrorPopup, FatalPopup,
    FileInfoPopup, FilterPopup, GotoPopup, KeybindingsPopup, MkdirPopup, NewfilePopup,
    OpenWithPopup, QuitPopup, RenamePopup, ReplacePopup, SaveAsPopup, SortingPopup, StatusBarLocal,
    StatusBarRemote, SymlinkPopup, SyncBrowsingMkdirPopup, TransferProgressBar, WaitPopup,
    WalkdirWaitPopup, WatchedPathsList, WatcherPopup,
};
pub use transfer::{ExplorerFind, ExplorerFuzzy, ExplorerLocal, ExplorerRemote};

pub use self::log::Log;
pub use self::selected_files::SelectedFilesList;
pub use self::terminal::Terminal;

#[derive(Default, Component)]
pub struct GlobalListener {
    component: Phantom,
}

impl AppComponent<Msg, NoUserEvent> for GlobalListener {
    fn on(&mut self, ev: &Event<NoUserEvent>) -> Option<Msg> {
        match ev {
            Event::Keyboard(KeyEvent { code: Key::Esc, .. }) => {
                Some(Msg::Ui(UiMsg::ShowDisconnectPopup))
            }
            Event::Keyboard(KeyEvent {
                code: Key::Char('q') | Key::Function(10),
                modifiers: KeyModifiers::NONE,
            }) => Some(Msg::Ui(UiMsg::ShowQuitPopup)),
            Event::Keyboard(KeyEvent {
                code: Key::Char('h') | Key::Function(1),
                modifiers: KeyModifiers::NONE,
            }) => Some(Msg::Ui(UiMsg::ShowKeybindingsPopup)),
            Event::WindowResize(_, _) => Some(Msg::Ui(UiMsg::WindowResized)),
            _ => None,
        }
    }
}
