#include <stdio.h>
#include <windows.h>
#include "../../src/shell.h"
#define minnum(x,y) (((x)<(y))?(x):(y))
static void ResizeConBufAndWindow(
	HANDLE hConsole,
	int xSize, int ySize)
{
    SMALL_RECT      srWindowRect;       /* hold the new console size */
    COORD           coordScreen,maxscreen;
DWORD			dwErr = 0;

    /* get the largest size we can size the console window to */
    coordScreen = GetLargestConsoleWindowSize(hConsole);

    if(xSize<0)
	    xSize= coordScreen.X ;
    if(ySize<0)
	  ySize=coordScreen.Y ;

    /* define the new console window size and scroll position */
    srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
    srWindowRect.Right =  (SHORT) (minnum(xSize, coordScreen.X) - 1);
    srWindowRect.Bottom = (SHORT) (minnum(ySize, coordScreen.Y) - 1);

    /* define the new console buffer size */
	 coordScreen.X = xSize;
    coordScreen.Y = ySize;
/*
	if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
	{
		fprintf(stderr,"second SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect) failed\n");

	}
*/
	if (!SetConsoleScreenBufferSize(hConsole, coordScreen))
	{
		fprintf(stderr,"SetConsoleScreenBufferSize(hConsole, coordScreen failed\n");
	}
	if (!SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect))
	{
		fprintf(stderr,"second SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect) failed\n");

	}
}
void printcoor(COORD   co) {
	printf("X=%hd, Y=%hd",co.X,co.Y);
	}

void printrect(SMALL_RECT rec) {
    printf("Left= %hd ",rec.Left);      
    printf("Top= %hd ",rec.Top);       
    printf("Right= %hd ",rec.Right);     
    printf("Bottom= %hd ",rec.Bottom);    
	}
int main(int argc,char **argv) {
int xnr=-1,ynr=-1;
HANDLE conhan;
HANDLE output=GetStdHandle( STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
if(!GetConsoleScreenBufferInfo(
		output,
    &csbi 	// address of screen info. buffer 
   )) {
	fprintf( stderr,"GetConsoleScreenBufferInfo failed\n");exit(5);
	}
printf("dwSize: ");printcoor(csbi.dwSize);puts("");
printf("dwCursorPosition: ");printcoor(csbi.dwCursorPosition);puts("");
printf("wAttributes=%hd\n",csbi.wAttributes);
    printf("SMALL_RECT: "); printrect(csbi.srWindow);
printf("dwMaximumWindowSize: ");printcoor(csbi.dwMaximumWindowSize);puts("");

getchar();
if(argc==3) {
	xnr=atoi(argv[1]);
	ynr=atoi(argv[2]);
	}

ResizeConBufAndWindow(output,xnr,ynr);
printf("resized:\n");

getchar();
puts("resized again new buffer");
getchar();
//ResizeConBufAndWindow(conhan,-1,-1);
puts("end");
if(!SetConsoleActiveScreenBuffer(output)) {
	fprintf(stderr,"SetConsoleActiveScreenBuffer(conhan))\n");
	exit(5);
	}
getchar();
process("command.com",NULL);
getchar();
conhan=CreateConsoleScreenBuffer(
		GENERIC_READ|GENERIC_WRITE,	
		FILE_SHARE_READ|FILE_SHARE_WRITE,	
    NULL,	// address of security attributes  
	  CONSOLE_TEXTMODE_BUFFER, 
		NULL
   );
if(conhan==INVALID_HANDLE_VALUE) {
		fprintf(stderr,"CreateConsoleScreenBuffer failed\n");
		exit(4);
		}
puts("set New buffer");
getchar();
if(!SetConsoleActiveScreenBuffer(conhan)) {
	fprintf(stderr,"SetConsoleActiveScreenBuffer(conhan))\n");
	exit(5);
	}
SetStdHandle(STD_OUTPUT_HANDLE,conhan);
SetStdHandle(STD_INPUT_HANDLE,conhan);
SetStdHandle(STD_ERROR_HANDLE,conhan);
return 0;
}
