/********************************************************************************************
ezwUtils.cpp
	Jim Millard
	for CO311

    EZwindows sort visualization utilities

********************************************************************************************/
#include "ezwUtils.h"

#include <sys/timeb.h>
#include <ezwin.h>
#include <ray.h>
#include <label.h>

#include "ezwSort.h"

extern float baseline; //Bottom edge of all bars
extern float tempX; //horizontal position used when swapped to a "temp" location
extern float clientX, clientY; //display area for sorting

/*********************************************************************************************/

int minValue(const int A[], const int size)
    {
    int smallest = A[0];
    for (int i = 1; i < size; i++)
        if (A[i] < smallest)
            smallest = A[i];
    return smallest;
    }

/*********************************************************************************************/

int maxValue(const int A[], const int size)
    {
    int largest = A[0];
    for (int i = 1; i < size; i++)
        if (A[i] > largest)
            largest = A[i];
    return largest;
    }

/*********************************************************************************************/

void RefreshDisplay(SimpleWindow& W, string Status, color StatusColor)
    {
    //divide the client area up
    RaySegment BaseLine(W, 0,clientY, winX,clientY, Black, (float)0.01);
    BaseLine.Draw();
    RaySegment SideBar(W, clientX,clientY, clientX,0, Black, (float)0.01);
    SideBar.Draw();

    //write the label for the temporary area
    Label temp(W, tempX, clientY + 0.5 ,"<temp>");
    temp.Draw();

    //write the label for the status area
    Label status(W, clientX/2, clientY + 0.5, Status.c_str(), StatusColor);
    status.Draw();
    }

/*********************************************************************************************/

void DelayMS(const short waitTime)
    {
    if (waitTime > 0)
        {        
        struct timeb tbNow;
        double Now, StopAfter;

        ftime( &tbNow );
        StopAfter = waitTime + tbNow.time*1000. + tbNow.millitm;

        do  {
            ftime( &tbNow );
            Now = tbNow.time*1000. + tbNow.millitm;
	        }
        while (Now <= StopAfter);
        }
    }

/*********************************************************************************************/

void Highlight(RaySegment& R, color C, const short waitTime)
    {
    R.SetColor(C);
    R.Draw();
    DelayMS(waitTime);
    }