#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
#include <sstream>
#include <string>
#include <curl/curl.h>

using namespace std;

struct Input {
    vector<double> numbers;
    size_t bin_count{};
};

Input input_data(istream& stream, bool prompt) {
  Input in;
  size_t number_count;
  if (prompt) {
    cerr << "Enter number count: ";
  }
  stream >> number_count;
  in.numbers.resize(number_count);
  for (size_t i = 0; i < number_count; i++) {
    if (prompt) {
      cerr << "Enter number " << i+1 << ": ";
    }
    stream >> in.numbers[i];
  }
  if (prompt) {
    cerr << "Enter bin count: ";
  }
  stream >> in.bin_count;
  return in;
}
Input
download(const string& address) {
    stringstream buffer;
    CURL *curl = curl_easy_init();
    if(curl) {
        CURLcode res;
        curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
        res = curl_easy_perform(curl);
        if (res!=CURLE_OK){
            cerr << curl_easy_strerror(res);
            exit(1);
        }
        curl_easy_cleanup(curl);
    }
    return input_data(buffer, false);
}

int main(int argc, char* argv[]) {
    Input input;
     if (argc > 1) {
        input = download(argv[1]);
    } else {
        input = input_data(cin, true);
    }
    const auto bins = make_histogram(input.numbers, input.bin_count);
    show_histogram_svg(bins);
}