#include <stdio.h>
#include <strings.h> 
#include <stdlib.h>
#include "minfo.h"

void calcHist(struct minfo *theMatrix)
{
    int curRow = 0, curCol = 0;
    char *curChar = theMatrix->matrix;

    for (curRow = 0; curRow < HISTSIZE; curRow++)
	theMatrix->histogram[curRow] = 0;

    for (curRow = 0; curRow < theMatrix->rows; curRow++)
	for (curCol = 0; curCol < theMatrix->cols; curCol++)
	{
	    theMatrix->histogram[(*curChar) - '@']++;
	    curChar++;
	}
}

int dumpHist(struct minfo *theMatrix, FILE * fp)
{
    int curVal = 0;

    for (; curVal < HISTSIZE; curVal++)
    {
	fprintf(fp, "%c: %04d - ", curVal + '@', theMatrix->histogram[curVal]);
	if (curVal % 5 == 4)
	    putc('\n', fp);
    }
    fflush (fp);
    sleep(1);
}

void calcThreshold(struct minfo *theMatrix)
{
    static int firstTime=1;
    int totalPoints = 0;
    int curRow;
    int wantPoints;
    int threshPoints = 0;

    for (curRow = 0; curRow < HISTSIZE; curRow++)
	totalPoints += theMatrix->histogram[curRow] * curRow;

    wantPoints = totalPoints / theMatrix->numContestants + 
	theMatrix->numContestants;

    if (firstTime)
    {
	theMatrix->winPoints = totalPoints / 2 + 1;
	firstTime = 0;
    }
    for (curRow = HISTSIZE - 1; curRow >= 0 && threshPoints < wantPoints;
	 curRow --)
	threshPoints += theMatrix->histogram[curRow] * curRow;

    theMatrix->threshold = curRow + '@';
}

int matrixLoc(struct minfo *theMatrix, int row, int col)
{
    return row * theMatrix->cols + col;
}

int dumpMatrix(struct minfo *theMatrix, FILE * fp)
{
    int curRow = 0, curCol = 0;
    char *curChar = theMatrix->matrix;

    fprintf (fp, "\033[2J\033[H");
    for (; curRow < theMatrix->rows; curRow++)
    {
	for (curCol = 0; curCol < theMatrix->cols; curCol++)
	{
	    putc(*curChar, fp);
	    curChar++;
	}
	putc('\n', fp);
    }
    putc('\n', fp);
    fflush (fp);
}

int parseMatrix(FILE *mfile, struct minfo *theMatrix)
{
    static char aline[200] = "no line read";
    char *workPtr;
    int size, size2;
    int conversions;
    int curRow;

    theMatrix->rows = -1;
    theMatrix->cols = -1;

    theMatrix->ourNum = -1;
    theMatrix->numContestants = -1;

    if (!fgets (aline, 190, mfile))
    {
	puts ("Can't read first line in matrix file");
	return 0;
    }
    if (!index(aline, '\n'))
    {
	puts ("First line of matrix file too long");
	return 0;
    }
    if ((conversions = sscanf (aline, "%d%d",
			       &theMatrix->ourNum,
			       &theMatrix->numContestants)) != 2)
    {
	puts ("Error reading our number and numContestants in matrix file");
	puts (aline);
	printf ("number of conversions = %d\n", conversions);
	printf ("rows = %d, cols = %d\n", theMatrix->rows, theMatrix->cols);
	return 0;
    }

    if (!fgets (aline, 190, mfile))
    {
	puts ("Can't read first line in matrix file");
	return 0;
    }
    if (!index(aline, '\n'))
    {
	puts ("First line of matrix file too long");
	return 0;
    }
    if ((conversions = sscanf (aline, "%d%d", &theMatrix->cols, &theMatrix->rows)) != 2)
    {
	puts ("Error reading cols and rows in matrix file");
	puts (aline);
	printf ("number of conversions = %d\n", conversions);
	printf ("rows = %d, cols = %d\n", theMatrix->rows, theMatrix->cols);
	return 0;
    }
    // printf ("rows = %d, cols = %d\n", theMatrix->rows, theMatrix->cols);
    size = theMatrix->cols * theMatrix->rows;
    if (size < 0 || theMatrix->rows < 1 || theMatrix->cols < 1)
    {
	puts ("Error invalid column/row specification");
	return 0;
    }
    theMatrix->matrix = (char *) malloc(size + 2);
    if (!theMatrix->matrix)
    {
	puts ("Cant allocate mem for matrix");
	return 0;
    }
    workPtr = theMatrix->matrix;
    for (curRow = 0; curRow < theMatrix->rows; curRow++)
    {
	/* add 1 for newline and 1 for null char */
	if (!fgets (workPtr, theMatrix->cols + 2, mfile))
	{
	    printf ("Error reading row %d of matrix", curRow + 1);
	    return 0;
	}
	workPtr += theMatrix->cols;
    }
    return 1;
}
