// LICENSE: This code is freeware.  You may use this code for any purpose you
// like so long as this license is attached.  If this code is used in a
// commercial application, you must send me an email so I feel uber-geeky.  No
// other compensation is required.  Email me at jechols@nerdbucket.com

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <gmpxx.h>
#include "HGProbabilityCalc.h"
#include "jestatistics.h"

using namespace std;

// Our special little callback function for displaying the odds.  We show the
// odds in a nice tabular way, and calculate odds against to the nearest tenth.
class ProbCall : public ProbabilityOddsCallback {
  private:
  string m_format;

  public:
  void set_format(const string fmt) {m_format = fmt;}

  void operator() (int matches, mpf_class odds) {
    if (odds == 0.0) {
      printf(m_format.c_str(), matches, 0.0, 0.0);
    }
    else {
      mpz_class odds_against_int_10 = (((1.0 / odds) + 0.09) * 10);
      mpf_class odds_against = odds_against_int_10;
      odds_against /= 10.0;
      mpf_class odds_percent = odds * 100;
      gmp_printf(m_format.c_str(), matches, odds_against.get_d(), odds_percent.get_d() );
    }
  }
};

// Gives the user more advanced information about hypergeometry
void print_advanced_usage(char *program_name) {
  printf(
    "To get Keno odds for a 10-spot game where range is 80 and computer picks 20:\n"
    "  %s 80 20 10\n\n"

    "To get state lottery-style odds where range is 49 and you pick 6:\n"
    "  %s 49 6 6\n\n"

    "To get hold \'em odds where you have a pocket pair and are looking for trips\n"
    "or quads on the flop (range is 50 cards left, deal is 3, player needs\n"
    "1 for trips, 2 for quads):\n"
    "  %s 50 3 2\n\n"

    "Algorithm is based on the Urn Problem - there are black and white marbles in\n"
    "an urn.  White marbles represent a \"win\".  Some number are chosen and set\n"
    "aside.  Given a marble count, a number of picks, and the number of white\n"
    "marbles, we compute the odds for getting all possibilities of white/black\n"
    "combinations.\n",
    program_name, program_name, program_name
  );
}

// Shows the user basic usage
void print_basic_usage(char *program_name) {
  printf("Usage: %s [marbles] [picks] [white marbles] (-h) (-hh) (--csv)\n\n", program_name);
  printf(
    "Calculate permutations for hypergeometric distributions, such as keno, where\n"
    "you can't pick the same number twice and order of numbers isn't important\n"
    "(i.e., 123 is exactly the same as 213 or 321, and 113 isn\'t valid because\n"
    "it has '1' twice), and the computer picks more items than the player.\n"
    "\n"
    "Use -h for this help, -hh for advanced help, and --csv for CSV output.\n"
  );
}

int main(int argc, char *argv[]) {
  bool verbose_help = false, help = false, csv = false;
  ProbCall pc;

  if (argc < 4) {
    help = true;
  }

  // Default format
  pc.set_format("%-28d%-20.1F%0.12F\n");

  // DON'T FORGET: start at 1 - argv[0] is the program name.  Won't hurt us
  // most likely, but why risk it?
  for (int c = 1; c < argc; ++c) {
    // !strcmp is more clever, but this is way more readable to me
    if (0 == strcmp("-h", argv[c])) {help = true;}
    if (0 == strcmp("-hh", argv[c])) {verbose_help = true;}
    // setup format for csv if user wants that
    if (0 == strcmp("--csv", argv[c])) {csv = true; pc.set_format("%d,%0.1F,%0.18F\n");}
  }

  if (help || verbose_help) {
    print_basic_usage(argv[0]);
    if (verbose_help) {print_advanced_usage(argv[0]);}

    return 1;
  }

  // All is well - do them mathy stuffs!
  HGProbabilityCalc hgpc(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));

  // If we wanted normal output, do it here
  if (!csv) {
    printf("You have %d marbles\n", hgpc.total_marbles() );

    // Verify that odds_against is working (technically get_all_odds will hit
    // that function for us, but it theoretically doesn't have to, so test both)
    gmp_printf("Odds of hitting 0 white marbles: %F\n", hgpc.odds_against(0).get_d() );
    printf("\n\n");

    printf("Number matched              Odds against        Probability (%%)\n");
  }

  // Callbacks are fun.  This situation may be odd to a somebody unfamiliar
  // with iterator callbacks.  Ruby does them a lot, so I like them.  In this
  // situation, to account for formatting variables, I use a "functor".
  // Thanks, Jesse!
  hgpc.get_all_odds(pc);

  // No reason to report anything but success
  return 0;
}
