/* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

//#include <db_config.h>
//#include <db_int.h>
#ifdef DB_UNKNOWN
#undef DB_UNKNOWN
#endif

// #include "db.h"
#include "sqlite3.h"

#define ATTRIBUTENUM                            36
#define MAX_NUMBER                              50

#define DICOM_HOME                              "dicom"

#define DB_FILE_NAME                            "dicom.db"
#define DICOM_DB_NAME                           "dicom"

extern char atts_name[ATTRIBUTENUM][40];

enum ATT_INDEX {
    ACCESSIONNUMBER, 
    ACQUISITIONDATE, 
    ACQUISITIONTIME, 
    BODYPARTEXAMINED, 
    DATACOLLECTIONDIAMETER,
    DEVICESERIALNUMBER,
    DISTANCESOURCETODETECTOR,
    DISTANCESOURCETOPATIENT,
    IMAGECOMMENTS, 
    INSTITUTIONADDRESS,
    INSTITUTIONNAME, 
    INSTITUTIONALDEPARTMENTNAME, 
    KVP, 
    MANUFACTURER, 
    MANUFACTURERMODELNAME, 
    MODALITY, 
    NAMEOFPHYSICIANREADINGSTUDY, 
    OPERATORNAME,
    PATIENTAGE,
    PATIENTBIRTHDATE,
    PATIENTID,
    PATIENTNAME,
    PATIENTSEX,
    PERFORMINGPHYSICIANNAME,
    REFERRINGPHYSICIANNAME,
    SERIESDATE,
    SERIESINSTANCEUID,
    SLICETHICKNESS,
    SOFTWAREVERSION,
    SOPCLASSUID,
    SOPINSTANCEUID,
    SOURCETYPE,
    SPATIALRESOLUTION,
    STATIONNAME,
    STUDYDATE,
    STUDYDESCRIPTION
 };

typedef struct _dicom_dbs{
	sqlite3 *db;
	const char *dbname;
}DICOM_DBS;

typedef unsigned int u_int32_t;

typedef struct _dicom_image{
	u_int32_t      id;
	char		empty[1];

	u_int32_t       accession_number_length;    
	u_int32_t       acquisition_date_length;    
	u_int32_t       acquisition_time_length;    
	u_int32_t       body_part_examined_length;   
	u_int32_t       data_collection_diameter_length;   
	u_int32_t       device_serial_number_length;   
	u_int32_t       distance_source_to_detector_length;   
	u_int32_t       distance_source_to_patient_length; 
	u_int32_t       image_comments_length;    
	u_int32_t       institution_address_length;    
	u_int32_t       institution_name_length;    
	u_int32_t       institutional_department_name_length;    
	u_int32_t       kvp_length;    
	u_int32_t       manufacturer_length;    
	u_int32_t       manufacturer_model_name_length;    
	u_int32_t       modality_length;    
	u_int32_t       name_of_physician_reading_study_length;    
	u_int32_t       operator_name_length;   
	u_int32_t       patient_age_length;
	u_int32_t       patient_birth_date_length;   
	u_int32_t       patient_id_length;
	u_int32_t       patient_name_length;   
	u_int32_t       patient_sex_length;    
	u_int32_t       performing_physician_name_length;   
	u_int32_t       referring_physician_name_length;     
	u_int32_t       series_date_length;   
	u_int32_t       series_instance_uid_length;   
	u_int32_t       slice_thickness_length;
	u_int32_t       software_version_length; 
	u_int32_t       sop_class_uid_length;   
	u_int32_t       sop_instance_uid_length;   
	u_int32_t       source_type_length;   
	u_int32_t       spatial_resolution_length;   
	u_int32_t       station_name_length;   
	u_int32_t       study_date_length;  
	u_int32_t       study_description_length;   
        u_int32_t       thumbnail_format_length; /* Byte size of format */
        u_int32_t       thumbnail_content_length;
						/* Byte size of content */
	
	char            *accession_number;    
	char            *acquisition_date;    
	char            *acquisition_time;    
	char            *body_part_examined;   
	char            *data_collection_diameter;   
	char            *device_serial_number;   
	char            *distance_source_to_detector;   
	char            *distance_source_to_patient; 
	char            *image_comments;    
	char            *institution_address;    
	char            *institution_name;    
	char            *institutional_department_name;    
	char            *kvp;    
	char            *manufacturer;    
	char            *manufacturer_model_name;    
	char            *modality;    
	char            *name_of_physician_reading_study;    
	char            *operator_name;   
	char            *patient_age;
	char            *patient_birth_date;   
	char            *patient_id;
	char            *patient_name;   
	char            *patient_sex;    
	char            *performing_physician_name;   
	char            *referring_physician_name;     
	char            *series_date;   
	char            *series_instance_uid;   
	char            *slice_thickness;
	char            *software_version; 
	char            *sop_class_uid;   
	char            *sop_instance_uid;   
	char            *source_type;   
	char            *spatial_resolution;   
	char            *station_name;   
	char            *study_date;  
	char            *study_description;   
        char            *thumbnail_format;          /* thumbnail format */
        char            *thumbnail_content;        /* thumbnail content */		
}DICOM_IMAGE;

/* Function prototypes */
int close_dbs(DICOM_DBS *dm_dbs);
void copy_dicom_image(DICOM_IMAGE *src_dicom_image, 
    DICOM_IMAGE *des_dicom_image);
void copy_memory(void *src_date, void *des_data, int length, int is_str);
int convert_bytes_to_int(char *bytes);
int convert_int_to_bytes(int number, char *bytes);
WCHAR *convertCharToWchar(char *chars);
char *convertWcharToChar(WCHAR *wchars);
int delete_all_dicom(DICOM_DBS *dbs);
int delete_dicom_with_id(DICOM_DBS *dbs, int id);
int find_attributes_index(char *name);
void free_dicom_image(DICOM_IMAGE *dicom_image);
void free_pointer(void *p);
void generate_file_name(int index, char *file_name, char *prefix, 
    char *suffix);
void get_dicom_image_from_row(sqlite3_stmt *stmt, DICOM_IMAGE *dicom_image);
void initialize_dbs(DICOM_DBS *dm_dbs);
int open_dbs(DICOM_DBS *dm_dbs);
char *position_dicom_field(int att_index, DICOM_IMAGE *image, char **field, 
    int *length);
int retrieve_dicom_with_oneattr(DICOM_DBS *dbs, int attindex, char *attvalue,
    DICOM_IMAGE *dicom_list, int *number);
int retrieve_dicom_with_attrs(DICOM_DBS *, char **attrs,
    DICOM_IMAGE *dicom_list, int *number);
int retrieve_attrs(DICOM_DBS *dbs, int at_index, char **key, int *number);
int update_field(DICOM_DBS *dm_dbs, DICOM_IMAGE *dicom, char *field_name, 
    char *valp, int valen);
int retrieve_all_dicom(DICOM_DBS *dbs, DICOM_IMAGE *dicom_image_list, int *number, int all);
int retrieve_keys(DICOM_DBS *dbs, int *keys, int *number);
/*
int retrieve_dicom_with_multiple_attributes(DB_ENV *env, DB *dbp, 
    DBC **cursorpp, BOOL *isQueried, char **value, DICOM_IMAGE *list, 
    int *number);
*/
int retrieve_dicom_with_id(DICOM_DBS *dbs, int id, DICOM_IMAGE *dicom);
int get_dicom_image_from_bytes(const char *bdb_data, DICOM_IMAGE *dicom_image);
int write_stream_into_bdb(char *stream, int length, DICOM_DBS *dbs);

/*
 * int partial_write(int id, char *stream, int doff, int oringinal_length,
    int new_length, DB *dbp, DB_TXN *txn);
  */
