/*
# homology2PG : $Revision$  
# $Date$ 
# Created by $Author$
# Usage : homology2PG
# Attention
#   $Loccker$
#  	$State$ 
#
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>                  
#define GLOBAL_DECLARATION
#include "../inc/config.h"

#define DEBUG
#include "genUtil.h"
#include "DataBase.hh"
#include <pgsql/libpq-fe.h>
#include "Memory.h"

#define ON 1
#define OFF 0

void AddColumn(DataBase* dataBase, char* TableName,int rNumber,int j,int* ResNumName, char* tmp, int skip);
void InsertValues(DataBase* dataaBase,char* TableName, int LineNumber, char* code);
void SetData(DataBase* dataBase, char* TableName,int rNumber, int j, int* ResNumName,char* tmp,int LineNumber);

/*
Example:
typedef struct lhomology2PGInfo {
	float a;
	int   b;
} lhomology2PGInfo;

typedef enum lhomology2PGMode {
	a=0,
	b=1
} lhomology2PGMode;
*/

int
main(int argc, char* argv[]) 
{
  homology2PGInfo info;
  
  init0(&info);
  argCheck(&info, argc, argv);
  init1(&info);
  
  DEBUGPRINT("Program Start\n");
  
  int start = 1;
  char* code;
   char sql[256]; 
  int i = 0;
  int rNumber = 1;
  int LineNumber = 1;
  int Insert = 1;
  char Notice[20];
  char test[20];
  int NoticeNumber = 0;
  strcpy(Notice,"actin_rabb");

  int TopNumber=0;
  int LastNumber = 0;

  char tmp[256];

//  char* tmp0 = strdup("pc00");
//  char* tmp1 = strdup("7777");
//  char* tmp2 = strdup("test");

  DataBase dataBase;

  dataBase.SetDataBaseInfo(info.Host,info.Port,info.database);
  
  char TableName[20] = "homotest";

  sprintf(sql, "CREATE TABLE %s (no INT,pdbcode TEXT)",TableName);
  dataBase.Exec(sql);

  long StartPosition;  
  int Pass;
  int FirstAction;

  int ResNumName[61];
  ResNumName[0] = 0; 
  Pass = ON;
  FirstAction = ON;
  
  int j;
  //j  = 16 + info.Skip;
  j = 16;
  int skip;
  skip  = info.Skip;


  StartPosition = ftell(info.fptIn);

  while(1){
    if(start > 0){
      //printf("\n");
      printf("while++");
      
      if(fgets(tmp, 256, info.fptIn) == NULL)
	break;
      
      //Space,NoData,\0etc....
      if(tmp[0] == '\0' || tmp[0] == '\n'){
	StartPosition = ftell(info.fptIn);
	printf("nodata\n");
      } else if(tmp[0] == ' '){
	
	printf("space\n");
	LineNumber = 1;
	Insert = 0;
	printf("Last  %d---TOP   %d",LastNumber,TopNumber);
	TopNumber = LastNumber;
	//TopNumber = LastNumber;
	FirstAction = OFF;
	Pass = ON;
	j = 16;
	skip = 0;
      }else{
	
	//Mokuteki no Mono wo Search
	code = strdup(tmp);
	code[strcspn(tmp, " ")] = 0;
	//	printf("LineNumber---%d",LineNumber);
	if( strcmp(code,Notice)==0 ){
	  if(Pass == ON){
	    if(FirstAction == ON)
	      InsertValues(&dataBase,TableName,LineNumber,code);
	 
	    printf("Create\n");  	    
	    rNumber = TopNumber;
	    AddColumn(&dataBase,TableName,LastNumber,j,ResNumName,tmp,skip);
	    SetData(&dataBase,TableName,LastNumber,j,ResNumName,tmp,LineNumber);
	    LastNumber = ResNumName[60]/1000;
	    TopNumber = ResNumName[1]/1000;
	    printf("LastNumber =%d=",LastNumber);
	    Pass = OFF;
	    fseek(info.fptIn,StartPosition,SEEK_SET);
	  
	  }else if( Pass == OFF){
	    printf("PassOff\n");
	  }
	  
	}else if( Pass == ON && strcmp(code,Notice)!=0){
	  printf("PassOn\n");
	}else if( Pass == OFF &&strcmp(code,Notice)!=0){
	  LineNumber = LineNumber + 1;
	  if(FirstAction == ON)
	    InsertValues(&dataBase,TableName,LineNumber,code);
	  
	  printf("ADD++");
	  SetData(&dataBase,TableName,TopNumber,j,ResNumName,tmp,LineNumber); 
	  
	}
      }
      //LineNumber = LineNumber + 1;
    }
    start = start + 1; 
  }
  exit(EXIT_SUCCESS);
  
}

void
additionalUsage()
{
  fprintf(stderr, "----- Additional Usage -----\n");
}


void AddColumn(DataBase* dataBase,char* TableName,int rNumber,int j,int* ResNumName, char* tmp, int skip)
{
  char ResNum[10];
  int i;
  char sql[256];
  
  if(ResNumName[0]== 0){
    printf("first");   
    ResNumName[0] = 0;
  }else{
    printf("not");
    ResNumName[0] = ResNumName[60];
  }
  
  printf("rNumber = %d : %d\n",rNumber,1/1000);
  
  for(i = 1 ; i <= 60; i++){
    j = j + 1 + skip;

    if(tmp[j] == '\n')
      {break;}
    rNumber;
    
    if(i < skip){
      printf("...skip...");
      ResNumName[i] = ResNumName[i-1] + 1;
      sprintf(ResNum,"r%d_%03d",ResNumName[i]/1000,ResNumName[i]%1000); 
      
    }else if(tmp[j]=='-'){
      printf("...-...");
      ResNumName[i] = ResNumName[i-1] + 1;
      sprintf(ResNum,"r%d_%03d",ResNumName[i]/1000,ResNumName[i]%1000); 

    }else{
      ResNumName[i] = ResNumName[i-1] + 1000;
      ResNumName[i] = ResNumName[i]/1000;
      ResNumName[i] = ResNumName[i]*1000;
      
      printf("...Nomal...i-1:%d..i:%d..%d",ResNumName[i-1],ResNumName[i],ResNumName[i-1]/1000);
      sprintf(ResNum,"r%d",ResNumName[i]/1000);
    }

    printf("Create-ResNum = %s  :  %d\n",ResNum,ResNumName[i]);

    sprintf(sql, "ALTER TABLE %s ADD COLUMN %s CHAR",TableName,ResNum);
    dataBase->Exec(sql);
    dataBase->ResClear();
  }
  ResNumName[0] = ResNumName[60];
}

void InsertValues(DataBase* dataBase,char* TableName, int LineNumber, char* code)
{
  char sql[256]; 
  sprintf(sql, "INSERT INTO %s VALUES(%d,'%s')",TableName,LineNumber,code);
  dataBase->Exec(sql);
  dataBase->ResClear();
}


void SetData(DataBase* dataBase,char* TableName,int rNumber, int j, int* ResNumName,char* tmp,int LineNumber)
{
  int i;
  char ResNum[10];
  char sql[256]; 
  printf("LineNumber---%d\n",LineNumber);
  for(i = 1 ; i <= 60; i++){
      j = j + 1;  
     if(tmp[j] == '\n')
       {break;}
     if(ResNumName[i]%1000!=0){
      sprintf(ResNum,"r%d_%03d",ResNumName[i]/1000,ResNumName[i]%1000);
    }else{
      sprintf(ResNum,"r%d",ResNumName[i]/1000);
    }
    
    printf( "%s-%c ",ResNum,tmp[j]);
    sprintf(sql, "UPDATE %s SET %s = '%c' WHERE no = %d",TableName,ResNum,tmp[j],LineNumber);
    
    dataBase->Exec(sql);
    dataBase->ResClear();
  }

}
