#include "comn.h"

int main(int argc, char *argv[]) {
	if (argc == 4) {

		char file_path[20];
		memset(file_path, 0, sizeof(file_path));
		sprintf(file_path, "./json/%s.json", argv[3]);
		if (-1 != access(file_path, 0)) {
			void *context = zmq_ctx_new();
			assert(context != NULL);
			void *requester = zmq_socket(context, ZMQ_REQ);
			assert(requester != NULL);

			char path[20];
			char line[1000];
			FILE *file;

			char msg_buf[1000] = { 0, };
			char recv_buf[1000] = { 0, };

			json_t *jstr;
			json_error_t *error;

			columnStc col_stc = { 0, };
			dataStc data_stc = { 0, };
			columnStc process_list[10] = { 0, };
			columnStc condition_list[10] = { 0, };

			memset(path, 0, sizeof(path));
			sprintf(path, "tcp://%s:%s", argv[1], argv[2]);
			printf("path :: %s\n", path);

			printf("zmq connect\n");
			if (0 != zmq_connect(requester, path)) {
				printf("연결 실패\n");
			}
			printf("zmq send\n");
			strcpy(msg_buf, "message");
			printf("msg_buf :: %s\n", msg_buf);

			file = fopen(file_path, "r");

			int i = 1;
			while (!feof(file)) {
				if (fgets(line, sizeof(line), file) == 0)
					break;

				jstr = json_loads(line, 0, &error);

				json_t *data, *obj, *arr;
				const char *key;
				char *json_str;
				int idx;
				int pr_cnt = 0, con_cnt = 0;

				json_object_foreach(jstr, key, obj)
				{
					data = json_object_get(jstr, key);

					if (!json_is_array(data)) {
						json_str = json_string_value(obj);

						if (0 == strcmp(key, "schema")) {
							data_stc.schema = json_str;
						} else if (0 == strcmp(key, "table")) {
							data_stc.table = json_str;
						} else {
							data_stc.dml_type = json_str;
						}
					}

					json_array_foreach(obj, idx, arr)
					{
						char *list_type;
						pr_cnt = 0, con_cnt = 0;
						list_type = key;
						json_object_foreach(arr, key, obj)
						{
							if (NULL != json_string_value(obj)) {
								json_str = json_string_value(obj);

								if (0 == strcmp(key, "column_name")) {
									col_stc.col_name = json_str;
								} else if (0 == strcmp(key, "value")) {
									col_stc.col_value = json_str;
								} else if (0 == strcmp(key, "column_type")) {
									col_stc.col_type = json_str;
								} else if (0 == strcmp(key, "column_size")) {
									col_stc.col_size = json_str;
								}
							}
						}

						if (0 == strcmp(list_type, "process_column_list")) {
							process_list[pr_cnt] = col_stc;
							pr_cnt++;
						} else if (0 == strcmp(list_type, "condition_list")) {
							condition_list[con_cnt] = col_stc;
							con_cnt++;
						}
					}
					data_stc.process_column_list = process_list;
					data_stc.condition_list = condition_list;
				}

				if (i < 5) {
					strcpy(msg_buf, data_stc.schema);
					zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
					strcpy(msg_buf, data_stc.table);
                                        zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
					strcpy(msg_buf, data_stc.dml_type);
                                        zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);

					// process_column_list
					printf("column_list cnt : %d\n", pr_cnt);
					printf("condition_list cnt : %d\n", con_cnt);
					printf("type :: %s\n", data_stc.dml_type);
					int j;
					for(j = 0; j < pr_cnt; j++) {
						printf("prlist\n");
						strcpy(msg_buf, "process_column_list");
                                	        zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
						strcpy(msg_buf, col_stc.col_name);
                	                        zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
						strcpy(msg_buf, col_stc.col_value);
	                                        zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
						strcpy(msg_buf, col_stc.col_type);
                       		                zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
						strcpy(msg_buf, col_stc.col_size);
	                                        zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
					}
					
					for(j = 0; j < con_cnt; j++) {
						printf("conlist\n");
						strcpy(msg_buf, "condition_list");
                                                zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
                                                strcpy(msg_buf, col_stc.col_name);
                                                zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
                                                strcpy(msg_buf, col_stc.col_value);
                                                zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
                                                strcpy(msg_buf, col_stc.col_type);
                                                zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
                                                strcpy(msg_buf, col_stc.col_size);
                                                zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
					}
				} else {
					zmq_send(requester, msg_buf, sizeof(msg_buf), 0);
					zmq_recv(requester, recv_buf, sizeof(recv_buf), 0);
					printf("recv_buf :: %s\n", recv_buf);
					i = 0;
				}
				i++;
				json_decref(jstr);
				sleep(1);
			}
			strcpy(msg_buf, "file end");
			zmq_send(requester, msg_buf, sizeof(msg_buf), 0);
			zmq_recv(requester, recv_buf, sizeof(recv_buf), 0);
			printf("end recv_buf :: %s\n", recv_buf);
		} else {
			printf("### not exist file path ###\n");
		}
	} else {
		printf("### no enough arguments  ###\n");
	}

	return 0;
}

