#include "file_output.h"

#include <utility>

#include "constants.h"

file_output::file_output(std::ofstream &&ofs):
        out{std::move(ofs)} {}

void file_output::render() {
        for (std::size_t idx = 0; idx < contents.size(); ++idx) {
                if (idx % DISPLAY_WIDTH == 0 && idx)
                        out << std::endl;

                out << static_cast<char>(contents[idx] ? contents[idx] : ' ');
        }

        out << std::endl;
}

void file_output::print_char(const position &pos, const char ch,
                             const int attrs) {
        if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT)
                return;

        contents[pos.y * DISPLAY_WIDTH + pos.x] = ch;
}

void file_output::print_str(const position &pos, const std::string &str,
                            const int attrs) {
        if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT)
                return;

        int head = pos.y * DISPLAY_WIDTH + pos.x;

        for (std::size_t i = 0; i < str.length(); ++i)
                contents[i + head] = str[i];
}
