#include "common.h"
#include "rest_proxy.h"

#define DEBUG_PORT    4865

static int sockfd = 0;
static int client_fd = -1;

extern int init_socket(unsigned int local_addr, short local_port, char stype, int noblock_flag);
extern int setnodelay(int fd);
extern int printf_stat(FILE *fd);
extern int reset_data_sn();

extern void rest_proxy_print_stat();
extern int dba_verify_vc_with_pwd(const char *vc_pwd, char card_info[][1024], char *enc_password);
extern int dba_db_sql_query_only_execute_real(const char *sql, char result[][1024], int MAX_ROWS, int MAX_FIELDS );
extern void crm_set_notificaton_flag(int flag);
extern void crm_set_test_mode_flag(int flag);

char cli_cmd[128];
int	cli_cmd_len = 0;
int cli_read_cmd(char *buf)
{
	int len = cli_cmd_len;

	if(len>0)
		strcpy(buf, cli_cmd);
	cli_cmd_len =  0;
	
	return len;
}

int cli_send_debug(char *buf)
{
	if(buf != NULL)
		send(client_fd,buf,strlen(buf),MSG_NOSIGNAL);                

	return 1;
}

int save_cli_cmd(char *cmd)
{
	strcpy(cli_cmd, cmd);
	cli_cmd_len = strlen(cmd);
	
	return 1;
}

#ifdef TEST_RESTPROXY
extern int g_pps_sendquery_to_restproxy_num;
extern int g_pps_sendtopup_to_restproxy_num;
extern int g_pps_sendtrans_to_restproxy_num;
#endif
int recv_command()
{
	int i = 0;
	socklen_t server_addr_len = 0;
	int    nbytes/*, len*/;
	struct sockaddr_in  sin;  
	char   buf[128],cmd;
	FILE   *fp = NULL;

	// len = sizeof(struct sockaddr);

	nbytes = recvfrom(client_fd, buf, 128, 0,(struct sockaddr *) &sin, &server_addr_len);
	if(nbytes == 0)
	{
	    client_fd = -1;
	 	return 0;
	}
    if(nbytes < 0)
		return 0;
	
	if(toupper(buf[0]) == 'Q')
	{
	    close(client_fd);
	    client_fd = -1;
	 	return 0;
	}	
	save_cli_cmd(buf);
	// send(client_fd,">",1,MSG_NOSIGNAL);
	// return 1;
	for(i = 0; i < nbytes; ++i)
	{
		if(buf[i] == '\r' && buf[i+1] == '\n')
		{
			buf[i] = '\0';
		}
	}
	if(strncasecmp(buf, "test query", 10) == 0)
	{
		send(client_fd, ">test query OK!\n", 16, MSG_NOSIGNAL);

#ifdef TEST_RESTPROXY
		if(!g_pps_sendquery_to_restproxy_num)
			g_pps_sendquery_to_restproxy_num = atoi(&buf[11]);
#endif
		goto END;
	}
	if(strncasecmp(buf, "test topup", 10) == 0)
	{
		send(client_fd, ">test topup OK!\n", 16, MSG_NOSIGNAL);
#ifdef TEST_RESTPROXY
		if(!g_pps_sendtopup_to_restproxy_num)
			g_pps_sendtopup_to_restproxy_num = atoi(&buf[11]);
#endif
		goto END;
	}
	if(strncasecmp(buf, "test transfer", 13) == 0)
	{
		send(client_fd, ">test transfer OK!\n", 19, MSG_NOSIGNAL);
#ifdef TEST_RESTPROXY
		if(!g_pps_sendtrans_to_restproxy_num)
			g_pps_sendtrans_to_restproxy_num = atoi(&buf[14]);
#endif
		goto END;
	}	
	if(strncasecmp(buf, "emu enable=", 11) == 0)
	{
		int ivalue = atoi(&buf[11]);
		if(ivalue == 0)
			send(client_fd, ">Disable Emulator, OK!\n", 23, MSG_NOSIGNAL);
		else
			send(client_fd, ">Enable Emulator, OK!\n", 22, MSG_NOSIGNAL);
		rest_conf_set_emulator_flag(ivalue);
		goto END;
	}	
	if(strncasecmp(buf, "get stat", 8) == 0)
	{
		rest_proxy_print_stat();
		
		send(client_fd, ">get stat OK!\n", 15, MSG_NOSIGNAL);
		goto END;
	}
	if(strncasecmp(buf, "test card=", 10) == 0)
	{
		dba_verify_vc_with_pwd(&buf[10], NULL, NULL);
		
		send(client_fd, ">get stat OK!\n", 15, MSG_NOSIGNAL);
		goto END;
	}
	if(strncasecmp(buf, "test query", 10) == 0)
	{
		char sql[4096]="select * from tb_sms_info limt 1";
		char result[30][1024];
		dba_db_sql_query_only_execute_real(sql, result, 1, 30);
		
		send(client_fd, ">test tariff query OK!\n", 15, MSG_NOSIGNAL);
		
		goto END;
	}
	if(strncasecmp(buf, "enable notification", 19) == 0)
	{
		crm_set_notificaton_flag(1);		
		goto END;
	}
	if(strncasecmp(buf, "disable notification", 20) == 0)
	{
		crm_set_notificaton_flag(0);		
		goto END;
	}
	if(strncasecmp(buf, "test mode=", 10) == 0)
	{
		crm_set_test_mode_flag((int)(buf[10]));
		
		send(client_fd, ">set test mode OK!\n", 15, MSG_NOSIGNAL);
		goto END;
	}
	
	if(nbytes>0)
	{
    	cmd = toupper(buf[0]);
    	switch(cmd)
    	{
    	    case 'P':
				fp = fopen("stat.txt","w");
				// printf_stat(fp);                
				send(client_fd,">OK!\n",4,MSG_NOSIGNAL);                
				fclose(fp);                
				break;
            case 'S':
				send(client_fd,">OK!\n",4,MSG_NOSIGNAL); 
				// reset_data_sn();
				break;
            case 'Q': 
				close(client_fd);
				client_fd = -1;
            	break;
            /*
            default:
                send(client_fd,">UNK CMD!\n",15,MSG_NOSIGNAL);
                send(client_fd,">",6,MSG_NOSIGNAL);
            break;
            */
        }
        send(client_fd,">",1,MSG_NOSIGNAL);
    }
END:
    return nbytes;
}

int debug_init()
{	
    sockfd = init_socket(0, DEBUG_PORT,1,1);
    listen(sockfd, 2);
    return 1;
}
void debug_monitor(void *arg)
{
	prctl(PR_SET_NAME, "debug_monitor");

	socklen_t clilen;
	struct sockaddr_in cliaddr;

	debug_init();

	while(1)
	{
	    	if(client_fd<0)
	    	{
				client_fd = accept(sockfd,(struct sockaddr *)&cliaddr,&clilen);
				if(client_fd>0)
				{
					setnodelay(client_fd);
					send(client_fd, ">", 1, MSG_NOSIGNAL);
				}
		}
		else
		{
	    		 recv_command();
		}
		usleep(10000);        
	}
}
