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

void show_histogram(std::vector<size_t> bins) {
    size_t mx_count = 0;
    bool need_scale = false;
    for (size_t x : bins) {
        if (x > mx_count) {
            mx_count = x;
        }
        if (x > 76) {
            need_scale = true;
        }
    }
    size_t max_bar = need_scale ? 76 : mx_count;
    size_t digits = 1;
    for (size_t t = mx_count; t >= 10; t /= 10) {
        ++digits;
    }

    for (size_t count : bins) {
        size_t bar_len = 0;
        if (need_scale) {
            if (count == mx_count) {
                bar_len = max_bar;
            } else {
                bar_len = static_cast<size_t>(max_bar * static_cast<double>(count) / mx_count);
            }
        } else {
            bar_len = count;
        }
        size_t spaces = max_bar - bar_len;
        for (size_t i = 0; i < spaces; ++i) {
            std::cout << ' ';
        }
        for (size_t i = 0; i < bar_len; ++i) {
            std::cout << '*';
        }
        std::cout << "| ";
        std::string s = std::to_string(count);
        for (size_t i = s.size(); i < digits; ++i) {
            std::cout << ' ';
        }
        std::cout << s << "\n";
    }
}
