#include<graphics.h>
#include<dos.h>
#include<io.h>
#include<conio.h>
#include<stdio.h>
// #include<string.h>
#include<ctype.h>
#include<alloc.h>
#include<stdlib.h>
#include "sv_util.h"
#include "..\libs\make_bot\make_bot.h"


#define Letra_Boton    setcolor(1);
#define Azul           1
#define Texto_Normal   setcolor(63);
#define Texto_Negrita  setcolor(0);

extern int x_raton, y_raton, raton;
extern int boton_izq, boton_der;

extern int InputCadenaG(char *s, int numalp, int lmax, int cc, int cf, struct boton Campo);
extern void  desactiva_raton(void);
extern void  activa_raton(void);
// extern void  Anclar_Raton(void);
extern void  inicializa_raton_grafico(int x1, int y1, int x2, int y2, int px, int py);
extern int Espera_Tecla_o_Raton(void);
extern void BEEP(void);
extern void textxy(int x, int y, char *text);


#define NEGRO     0
#define AZUL      1
#define ROJO      4
#define VERDE     2
#define BLANCO   63
#define GRIS_O   56
#define GRIS_C    7
#define AMARILLO 62

extern void textxy(int x, int y, char *text);

int Comprueba_Impresora(void);
int Confirma(char *T1, char *T2, char *T3);
int Desde_Hasta(int *Desde, int *Hasta);
int Pide_Fecha_Actual(void);
int file_exists(char *filename);

int Confirma(char *T1, char *T2, char *T3){

void far *ptr;
unsigned size;
struct boton pregunta[2];
int fins_print=0, key_print, bpush, ok = 0;
int TFuente;
struct textsettingstype textinfo;
gettextsettings(&textinfo);
TFuente = textinfo.charsize;

size = imagesize(198, 138, 372, 257);

/* get byte size of image */
Fuente(SMALL_FONT, 5);

if ( (ptr = farmalloc(size)) == NULL) { Fuente(SMALL_FONT, TFuente); return 0; }

getimage(198, 138, 372, 257, ptr);

pregunta[0].up  = 140;	pregunta[0].down = 255;
pregunta[0].left= 200;  pregunta[0].right= 370;
ImprimeBoton (1, GRIS_C, GRIS_O, BLANCO, pregunta[0]);

pregunta[0].up  = 235;	pregunta[0].down = 250;
pregunta[0].left= 210;    pregunta[0].right= 270;
ImprimeBoton (1, GRIS_C, BLANCO, GRIS_O, pregunta[0]);

pregunta[1].up  = 235;	pregunta[1].down = 250;
pregunta[1].left= 300;  pregunta[1].right= 360;
ImprimeBoton (1, GRIS_C, BLANCO, GRIS_O, pregunta[1]);

textxy(212, 237, "   Si");
 textxy(212, 239, "   _");
textxy(302, 237, "   No");
 textxy(302, 239, "   _");

textxy(210+2, 150+2, T1);
textxy(210+2, 165+2, T2);
textxy(210+2, 180+2, T3);


//��������������������������������������������������������������������������������
do{

 if(raton!=0) activa_raton();
  Espera_Tecla_o_Raton();
 if(raton!=0) desactiva_raton();

 if(kbhit()){
   key_print = getch();
   switch( toupper(key_print) ){
      // S  Si
      case  83:
	ok = 1;
	fins_print = 1;
	break;
      // N  No
      case  78:
	ok = 0;
	fins_print = 1;
	break;
      default:
	break;
     }
  } else
   if(raton!=0 && boton_izq==1 || boton_der==1) {
     if( (bpush = CheckButon( 2, pregunta))>0 ) {

       switch(bpush){
	 case 1:
		ok = 1;
		fins_print = 1;
		break;
	 case 2:
		ok = 0;
		fins_print = 1;
		break;
	 default:
                break;
       }
     }
   }

}while(!fins_print);




putimage(198, 138, ptr, COPY_PUT);
farfree(ptr);
Fuente(SMALL_FONT, TFuente);

return ok;

}



int Pide_Fecha_Actual(void){

    int Fecha;

//������     void far *ptr;
//������     unsigned size;
    char mes[][11] = { " Enero",   "Febrero",  " Marzo",
		       " Abril",   " Mayo",    " Junio",
		       " Julio",   " Agosto",  "Septiemb",
		       "Octubre",  "Noviemb",  "Diciemb"
    };

    struct boton pregunta[3];
    struct boton meses[12];

    int key_print, bpush, ok = 0;
    int TFuente;

    for( Fecha = 0; Fecha < 4; Fecha++ ){
     meses[2+3*Fecha].left  = 405;   meses[2+3*Fecha].right  = 455;
     meses[1+3*Fecha].left  = 350;   meses[1+3*Fecha].right  = 400;
     meses[0+3*Fecha].left  = 295;   meses[0+3*Fecha].right  = 345;
    }

    for( Fecha = 0; Fecha < 3; Fecha++ ) {
     meses[Fecha].up      = 80+140;   meses[Fecha].down     = 80+160;
     meses[Fecha+3].up    = 80+165;   meses[Fecha+3].down   = 80+185;
     meses[Fecha+6].up    = 80+190;   meses[Fecha+6].down   = 80+210;
     meses[Fecha+9].up    = 80+215;   meses[Fecha+9].down   = 80+235;
    }


    struct textsettingstype textinfo;
    gettextsettings(&textinfo);
    TFuente = textinfo.charsize;
				      struct date d;
				      getdate(&d);
				      Fecha = d.da_mon;

				      /* get byte size of image */
//������			      size = imagesize(198, 138, 372, 257);



//������ if ( (ptr = farmalloc(size)) == NULL) {
//������ 				      Fuente(SMALL_FONT, TFuente);
//������ 				      return Fecha;
//������ 				      }

//������ 				      getimage(198, 138, 372, 257, ptr);


pregunta[0].up  = /*140*/130;	pregunta[0].down = /*255*/400;
pregunta[0].left= /*200*/150;  pregunta[0].right= /*370*/465;
ImprimeBoton (1, GRIS_C, GRIS_O, BLANCO, pregunta[0]);

pregunta[0].up  = /*143*/135;	pregunta[0].down = /*252*/395;
pregunta[0].left= /*203*/155;  pregunta[0].right= /*367*/460;
ImprimeBoton (0, GRIS_C, BLANCO, GRIS_O, pregunta[0]);


pregunta[0].up  = /*235*/370;	pregunta[0].down = /*250*/390;
pregunta[0].left= /*210*/160;    pregunta[0].right= /*270*/230;
ImprimeBoton (1, GRIS_C, BLANCO, GRIS_O, pregunta[0]);

pregunta[1].up  = /*235*/370;	pregunta[1].down = /*250*/390;
pregunta[1].left= /*300*/385;  pregunta[1].right= /*360*/455;
ImprimeBoton (1, GRIS_C, BLANCO, GRIS_O, pregunta[1]);

pregunta[2].up  = /*165*/175;	pregunta[2].down = /*185*/195;
pregunta[2].left= 212;  pregunta[2].right= /*232*/245;
ImprimeBoton (1, GRIS_C, BLANCO, GRIS_O, pregunta[2]);

Fuente(SMALL_FONT, 4);
for( int i = 0; i < 12; i++ ) {
	ImprimeBoton (1, GRIS_C, GRIS_O, BLANCO, meses[i]);
	textxy(meses[i].left+4, meses[i].up+3, mes[i]);
}
ImprimeBoton (1, ROJO, GRIS_O, BLANCO, meses[Fecha-1]);
textxy(meses[Fecha-1].left+4, meses[Fecha-1].up+3, mes[Fecha-1]);

Fuente(SMALL_FONT, 5);

textxy(162, 372, " Aceptar");
 textxy(162, 373, " _");
textxy(387, 372, " Cancelar");
 textxy(387, 373, " _");

textxy(160+2, 145+2, "Introduzca mes a gestionar:");

// textxy(210+2, 165+2, T2);
// textxy(210+2, 180+2, T3);
char buffer[80];
int Mes=Fecha;

sprintf( buffer, "% 2d", Fecha);
textxy(pregunta[2].left + 4, pregunta[2].up + 2, buffer);

//��������������������������������������������������������������������������������
do{

 if(raton!=0) activa_raton();
  Espera_Tecla_o_Raton();
 if(raton!=0) desactiva_raton();

 if(kbhit()){
   key_print = getch();
   if( key_print == 13 ) {Fecha = Mes; ok = 1; } else
   if( isdigit(key_print) ) {
   // Edita el mes actual
			 sprintf( buffer, "%d", Fecha);
			 do{
			 InputCadenaG( buffer, 1, 4, 63, 57, pregunta[2]);
			 Fecha = atoi(buffer);
			 }while(Fecha<1 || Fecha>12);
   } else
   if(key_print==0)
    switch( getch() ){
      // Flecha Izquierda
      case  75:
      case  15:
	ImprimeBoton (0, GRIS_C, GRIS_O, BLANCO, meses[Mes-1]);
	Mes--;
	if ( (Mes + 1)== 1 ) Mes = 3; else
	if ( (Mes + 1)== 4 ) Mes = 6; else
	if ( (Mes + 1)== 7 ) Mes = 9; else
	if ( (Mes + 1)==10 ) Mes = 12;
	ImprimeBoton (0, GRIS_C, AZUL, AZUL, meses[Mes-1]);
	break;
      // Flecha Derecha
      case  77:
      case   9:
	ImprimeBoton (0, GRIS_C, GRIS_O, BLANCO, meses[Mes-1]);
	Mes++;
	if ( (Mes - 1)== 3 ) Mes = 1;  else
	if ( (Mes - 1)== 6 ) Mes = 4;  else
	if ( (Mes - 1)== 9 ) Mes = 7;  else
	if ( (Mes - 1)==12 ) Mes = 10;
	ImprimeBoton (0, GRIS_C, AZUL, AZUL, meses[Mes-1]);
	break;
      // Flecha Abajo
      case  80:
	ImprimeBoton (0, GRIS_C, GRIS_O, BLANCO, meses[Mes-1]);
	Mes += 3; if ( Mes > 12) Mes = Mes - 12;
	ImprimeBoton (0, GRIS_C, AZUL, AZUL, meses[Mes-1]);
	break;
      // Flecha Arriba
      case  72:
	ImprimeBoton (0, GRIS_C, GRIS_O, BLANCO, meses[Mes-1]);
	Mes -= 3; if ( Mes < 1) Mes = 12 + Mes;
	ImprimeBoton (0, GRIS_C, AZUL, AZUL, meses[Mes-1]);
	break;
    }
   else
   switch( toupper(key_print) ){
      // A  Aceptar
      case  65:
	ok = 1;
	break;
      // C  Cancelar
      case  67:
	ok = 1;
	Fecha = -1;
	break;
      default:
	break;
     }
  } else
   if(raton!=0 && boton_izq==1 || boton_der==1) {
     if( (bpush = CheckButon( 2, pregunta))>0 ) {

       switch(bpush){
	 case 1:
		ok = 1;
		break;
	 case 2:
		ok = 1;
		Fecha = -1;
		break;
	 default:
		break;
       }
     }
   }

}while(!ok);


//������ putimage(198, 138, ptr, COPY_PUT);
//������ farfree(ptr);


Fuente(SMALL_FONT, TFuente);

return Fecha;

}


int file_exists(char *filename)
{
  return (access(filename, 0) == 0);
}


int Comprueba_Impresora(void){

 unsigned int tmp;
 union REGS Impresora_in, Impresora_out;
 struct byte {
  int a : 1;
  int b : 1;
  int c : 1;
  int d : 1;
  int e : 1;
  int f : 1;
  int g : 1;
  int h : 1;
 };
 union bits {
  char ch;
  struct byte bit;
 } Imp_out;

 Impresora_in.h.ah = 0x02;
 Impresora_in.x.dx = 0x0;

 int86(0x17, &Impresora_in, &Impresora_out);

 return ( Impresora_out.h.ah ? 1 : 0 );

}

int Desde_Hasta(int *Desde, int *Hasta){

int fins_print= 0, key_print, bpush;
char buffer[80], devuelve = 0;
void far *ptr;
unsigned size;
struct boton pregunta[4];

struct date Fecha;
getdate(&Fecha);

*Desde = *Hasta = Fecha.da_day;

size = imagesize(198, 138, 372, 257);

/* get byte size of image */

if ( (ptr = farmalloc(size)) == NULL) return -1;
	getimage(198, 138, 372, 257, ptr);

	pregunta[0].up  = 140;	pregunta[0].down = 255;
	pregunta[0].left= 200;  pregunta[0].right= 370;
	ImprimeBoton (1, ROJO, GRIS_C, BLANCO, pregunta[0]);
	pregunta[0].up  = 150;	pregunta[0].down = /*175*/165;
	pregunta[0].left= /*210*/250;    pregunta[0].right= 360;
	ImprimeBoton (1, NEGRO, GRIS_O, BLANCO, pregunta[0]);
	pregunta[1].up  = 185;	pregunta[1].down = /*210*/200;
	pregunta[1].left= /*210*/250;  pregunta[1].right= 360;
	ImprimeBoton (1, NEGRO, GRIS_O, BLANCO, pregunta[1]);
	pregunta[2].up  = 220;	pregunta[2].down = 245;
	pregunta[2].left= 210;  pregunta[2].right= 280;
	ImprimeBoton (1, GRIS_C, BLANCO, GRIS_O, pregunta[2]);
	pregunta[3].up  = 220;	pregunta[3].down = 245;
	pregunta[3].left= 290;  pregunta[3].right= 360;
	ImprimeBoton (1, GRIS_C, BLANCO, GRIS_O, pregunta[3]);

	Fuente(SMALL_FONT, 4);

Letra_Boton
outtextxy(pregunta[0].left+2 - 40, pregunta[0].up+2, "DESDE");
outtextxy(pregunta[1].left+2 - 40, pregunta[1].up+2, "HASTA");
outtextxy(pregunta[2].left+2, pregunta[2].up+2, "CANCELAR");
 outtextxy(pregunta[2].left+2, pregunta[2].up+4, "_");
outtextxy(pregunta[3].left+2, pregunta[3].up+2, "ACEPTAR");
 outtextxy(pregunta[3].left+2, pregunta[3].up+4, "_");

//��������������������������������������������������������������������������������
do{

 if(raton!=0) activa_raton();
  Espera_Tecla_o_Raton();
 if(raton!=0) desactiva_raton();

 if(kbhit()){
   key_print = getch();
   if(key_print == 27 ) {   fins_print = 1; 	devuelve = -1; } else
   if(key_print == 'A' || key_print == 'a') {   fins_print = 1;    } else
   if(key_print == 'C' || key_print == 'c') {   fins_print = 1; 	devuelve = -1; } else
   if( isprint(key_print) ) {
			sprintf( buffer, "%d", *Desde);
		       do{
			InputCadenaG( buffer,      1,    2, 63, 57, pregunta[0]);
			*Desde = atol(buffer);
		       }while(*Desde<1 || *Desde>12);

			sprintf( buffer, "%d", *Hasta);
		       do{
			InputCadenaG( buffer,      1,    2, 63, 57, pregunta[1]);
			*Hasta = atol(buffer);
		       }while(*Hasta<1 || *Hasta>12);
   } else
   if( key_print == 0 ) {
     switch( (key_print=getch()) ){
      // F1 Cancelar
      case  59:
	fins_print = 1;
	devuelve = -1;
	break;
      // F3 Imprimir Ficha
      default:
	break;
     }
   }
 } else
   if(raton!=0 && boton_izq==1 || boton_der==1) {
     if( (bpush = CheckButon( 3, pregunta))>0 ) {
/*
	pregunta[0].up  = 140;	pregunta[0].down = 255;
	pregunta[0].left= 200;  pregunta[0].right= 370;
	ImprimeBoton (1, GRIS_O, GRIS_C, BLANCO, pregunta[0]);
*/
       switch(bpush){
	 // Cancelar
	 case 3:
		fins_print=1;
		devuelve = -1;
		break;
	 case 4:
		fins_print=1;
		break;
       }
     }
   }


}while(!fins_print);

//��������������������������������������������������������������������������������


      putimage(198, 138, ptr, COPY_PUT);
      farfree(ptr);

      Fuente(SMALL_FONT, 5);

return devuelve;
}