#include "text.h"
#include <iostream>

void
show_histogram_text(const std::vector<size_t>& bins, size_t screen_width) {
    const size_t MAX_ASTERISK = screen_width - 3 - 1;

    size_t max_count = 0;
    for (size_t count : bins) {
        if (count > max_count) {
            max_count = count;
        }
    }

    for (size_t i = 0; i < bins.size(); i++) {
        size_t height = 0;

        if (max_count > MAX_ASTERISK) {
            height = static_cast<size_t>(
                MAX_ASTERISK * static_cast<double>(bins[i]) / max_count
            );
        } else {
            height = bins[i];
        }

        if (bins[i] < 10) {
            std::cout << "  " << bins[i] << "|";
        } else if (bins[i] < 100) {
            std::cout << " " << bins[i] << "|";
        } else {
            std::cout << bins[i] << "|";
        }

        for (size_t j = 0; j < height; j++) {
            std::cout << "*";
        }
        std::cout << std::endl;
    }
}
