#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#undef DEBUG
#include "genUtil.h"
#include "../inc/ltlgData.h"

void
ltlgDataWriteFile(FILE* fpt, ltlgData* ltlg, ltlgDataParaTypeInteger mode, ltlgDataParaTypeInteger* status)
{   
    char  tmp[LTLG_FTN_RECORD];
    char* current;
    char* current1;
    char* current2;
	char* prd;
	char* prMax;
    ltlgDataParaTypeInteger i, j, tmpj, k, tmpk, nllRec, nrRec;

    if(fpt==NULL) {
        fprintf(stderr, "File Descripter in NULL\n");
        *status = EXIT_FAILURE;
        return;
    }
    fseek(fpt, 0L, 0);
/* The First Record */
    current = tmp;
    strncpy(current, ltlg->title1, LTLG_TITLE_LENGTH);
    current += LTLG_TITLE_LENGTH;
    strncpy(current, ltlg->title2, LTLG_TITLE_LENGTH);
    current += LTLG_TITLE_LENGTH;
    strncpy(current, ltlg->side, LTLG_SIDE_LENGTH);
    current += LTLG_SIDE_LENGTH;
    *((ltlgDataParaTypeReal*)   current) = ltlg->R.d;
    current += sizeof(ltlgDataParaTypeReal);
    *((ltlgDataParaTypeInteger*)current) = ltlg->ll.Max;
	DEBUGPRINT1("llMax: %f\n", ltlg->ll.Max);
    current += sizeof(ltlgDataParaTypeInteger);
	/*
    *((ltlgDataParaTypeReal*)   current) = ltlg->r.d;
	*/
	prd = (char*)(current - tmp);
    current += sizeof(ltlgDataParaTypeReal);
	/*
    *((ltlgDataParaTypeReal*)   current) = ltlg->r.Max;
	*/
	prMax = (char*)(current - tmp);
    current += sizeof(ltlgDataParaTypeReal);
    *((ltlgDataParaTypeInteger*)current) = ltlg->r.n;
    tmp[LTLG_TITLE_LENGTH-1] = tmp[2*LTLG_TITLE_LENGTH-1] = tmp[2*LTLG_TITLE_LENGTH+LTLG_SIDE_LENGTH-1] = ' ';
    fwrite(tmp, sizeof(char), LTLG_FTN_RECORD, fpt);
	fseek(fpt, (size_t)prd, SEEK_SET);
	fwrite(&(ltlg->r.d), sizeof(ltlgDataParaTypeReal), 1, fpt);
	fseek(fpt, (size_t)prMax, SEEK_SET);
	fwrite(&(ltlg->r.Max), sizeof(ltlgDataParaTypeReal), 1, fpt);
	fseek(fpt, LTLG_FTN_RECORD, SEEK_SET);
/* The Second Record */
    nllRec = LTLG_FTN_RECORD/sizeof(ltlgDataLLDataInfo);
	DEBUGPRINT2("llMax: %f , nRec: %d\n", ltlg->ll.Max, nllRec);
    current  = tmp;
    current1 = current  + sizeof(ltlgDataParaTypeInteger);
    current2 = current1 + sizeof(ltlgDataParaTypeInteger);
    for(i=0; i<ltlg->ll.Max; i+=nllRec) {
        for(j=i; j<i+nllRec-1 && j<ltlg->ll.Max; j++) {
			DEBUGPRINT1("%d\n", j);
            tmpj = j-i;
            *((ltlgDataParaTypeInteger*)(current +tmpj*sizeof(ltlgDataLLDataInfo))) = ltlg->llInfo[j].NN;
            *((ltlgDataParaTypeInteger*)(current1+tmpj*sizeof(ltlgDataLLDataInfo))) = ltlg->llInfo[j].NL;
            *((ltlgDataParaTypeReal*)   (current2+tmpj*sizeof(ltlgDataLLDataInfo))) = ltlg->llInfo[j].WT;
        }
        fwrite(tmp, sizeof(char), LTLG_FTN_RECORD, fpt);
    }
/* ltlgDataLLData Records */
    nrRec = LTLG_FTN_RECORD/(sizeof(ltlgDataParaTypeReal)*2);
    current  = tmp;
    current1 = current+sizeof(ltlgDataParaTypeReal);
    for(i=0; i<ltlg->ll.Max; i++) {
        for(j=0; j<ltlg->r.n; j+=nrRec) {
            for(k=j; k<j+nrRec && k<ltlg->r.n; k++) {
                tmpk = k-j;
                *((ltlgDataParaTypeReal*)(current +tmpk*sizeof(ltlgDataParaTypeReal)*2)) = ltlg->ltlg[i][k].Re;
                *((ltlgDataParaTypeReal*)(current1+tmpk*sizeof(ltlgDataParaTypeReal)*2)) = ltlg->ltlg[i][k].Im;
            }
        	fwrite(tmp, sizeof(char), LTLG_FTN_RECORD, fpt);
        }
    }
    *status = EXIT_SUCCESS;
}
