use crate::Dance;
use crate::State;
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers, poll};
use std::time::Duration;

impl Dance {
    pub fn controls(&mut self) -> std::io::Result<bool> {
        if poll(Duration::ZERO)? {
            match self.state {
                State::Normal | State::Rename => {
                    if let Event::Key(KeyEvent {
                        code, modifiers, ..
                    }) = event::read()?
                    {
                        match (code, modifiers) {
                            // quit
                            (KeyCode::Esc, _) => {
                                self.state = State::Quit;
                                return Ok(false);
                            }

                            (KeyCode::Enter, _) => {
                                self.state = State::Dance;
                                return Ok(false);
                            }

                            (KeyCode::Up, _) => {
                                self.loc_list.select(true);
                                return Ok(true);
                            }
                            (KeyCode::Down, _) => {
                                self.loc_list.select(false);
                                return Ok(true);
                            }
                            (
                                KeyCode::Char('c'),
                                KeyModifiers::CONTROL,
                            ) => {
                                self.state = State::Quit;
                                return Ok(false);
                            }

                            (KeyCode::Char(c), _) => {
                                self.entry_buff.push(c);
                                self.loc_list.apply_filter(
                                    &self.entry_buff,
                                );
                                return Ok(true);
                            }

                            (
                                KeyCode::Backspace,
                                KeyModifiers::ALT
                                | KeyModifiers::CONTROL,
                            ) => {
                                _ = self.entry_buff.pop();
                                let buff = self.entry_buff.clone();

                                let mut count = 0;
                                let mut i = 0;
                                let mut space_found = false;
                                for c in buff.chars().rev() {
                                    if !space_found {
                                        if c == ' ' {
                                            space_found = true;
                                            i = buff.chars()
                                                .count()
                                                - count;
                                        }
                                    }
                                    count += 1;
                                }

                                if !space_found {
                                    self.entry_buff.clear();
                                } else {
                                    self.entry_buff.truncate(i);
                                }
                                self.loc_list.apply_filter(
                                    &self.entry_buff,
                                );
                                return Ok(true);
                            }

                            (KeyCode::Backspace, _) => {
                                self.entry_buff.pop();
                                self.loc_list.apply_filter(
                                    &self.entry_buff,
                                );
                                return Ok(true);
                            }

                            _ => {}
                        }
                    }
                }

                _ => {}
            }
        }
        Ok(false)
    }
}
