Documentation
// Copyright 2022 Parthka. All rights reserved. MIT license.

use super::types::Headers;
use std::collections::HashMap;

// response
pub struct Res {
    reply: String,
    status: u16,
    headers: Headers,
}

impl Res {
    pub fn new() -> Self {
        Res {
            status: 200,
            reply: "".to_string(),
            headers: HashMap::new(),
        }
    }

    pub fn new_with(status: Option<u16>, reply: Option<String>, headers: Option<Headers>) -> Self {
        Res {
            status: match status {
                Some(s) => s,
                None => 200,
            },
            reply: match reply {
                Some(r) => r,
                None => "".to_string(),
            },
            headers: match headers {
                Some(h) => h,
                None => HashMap::new(),
            },
        }
    }

    pub fn to_part(&self) -> (u16, String, Headers) {
        (
            self.status.clone(),
            self.reply.clone(),
            self.headers.clone(),
        )
    }

    pub fn reply(&mut self, body: String) {
        self.reply = body
    }

    pub fn reply_str(&mut self, body: &'static str) {
        self.reply = body.to_string()
    }

    pub fn status(&mut self, status: u16) {
        self.status = status
    }

    pub fn set_header(&mut self, key: &'static str, val: &'static str) {
        self.headers.insert(key, val);
    }
}