#[derive(Debug, Clone, Default)]
pub struct ResponseViewer {
    pub status: u16,
    pub elapsed_ms: u64,
    pub headers: Vec<(String, String)>,
    pub body: String,
    pub show_headers: bool,
    pub scroll: usize,
}

impl ResponseViewer {
    pub fn fill(
        &mut self,
        status: u16,
        elapsed: u64,
        headers: Vec<(String, String)>,
        body: String,
    ) {
        self.status = status;
        self.elapsed_ms = elapsed;
        self.headers = headers;
        self.body = body;
        self.scroll = 0;
    }

    pub fn scroll_up(&mut self) {
        self.scroll = self.scroll.saturating_sub(1);
    }

    pub fn scroll_down(&mut self) {
        let lines = self.body.lines().count();
        if self.scroll + 1 < lines {
            self.scroll += 1;
        }
    }

    pub fn status_label(&self) -> &'static str {
        match self.status {
            200 => "200 OK",
            201 => "201 Created",
            204 => "204 No Content",
            400 => "400 Bad Request",
            401 => "401 Unauthorized",
            403 => "403 Forbidden",
            404 => "404 Not Found",
            422 => "422 Unprocessable",
            500 => "500 Server Error",
            _ => "???",
        }
    }

    pub fn content_type(&self) -> &str {
        self.headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case("content-type"))
            .map(|(_, v)| v.as_str())
            .unwrap_or("")
    }
}
