#include "UavWork.h"

void uavOperate(int choose) {
	 switch(choose){
	 	case 1: init_position(); break;
	 	case 2: set_name(); break;
	 	case 3: set_color(); break;
	 	case 4: start_engine(); break;
	 	case 5: shut_down_engine(); break;
	 	case 6: set_rotor_power(); break;
	 	case 7: open_lighter(); break;
	 	case 8: close_lighter(); break;
	 	case 9: get_current_height(); break;
	 	case 10: get_current_attitude_angle(); break;
	 	case 11: get_current_distance(); break;
	 	case 12: get_current_horizontal_speed(); break;
	 	case 13: get_current_vertical_speed(); break;
	 	case 14: open_hd(); break;
	 	case 15: close_hd(); break;
	 	case 16: fly_by_3d_direction(); break;
	 }
}

int getChoose() {
		int num;
		printf("请选择需要操作：----------------------\n");
		printf("0.退出操作\n");
        printf("1.初始化无人机坐标\n");
        printf("2.设置无人机名称\n");
        printf("3.设置无人机颜色\n");
        printf("4.启动引擎\n");
        printf("5.关闭引擎\n");
        printf("6.设置无人机各旋翼输出\n");
        printf("7.打开无人机灯光\n");
        printf("8.关闭无人机灯光\n");
        printf("9.获取无人机当前高度\n");
        printf("10.获取无人机当前姿态角\n");
        printf("11.获取无人机与返航点的水平方向距离\n");
        printf("12.获取无人机的水平方向速度\n");
        printf("13.获取无人机的垂直速度\n");
        printf("14.打开高清图传\n");
        printf("15.关闭高清图传\n");
        printf("16.以给定速度朝方向飞行多长时间\n");
//        printf("17.在规定时间内飞至指定位置\n");
//        printf("18.在固定速度的情况下飞至指定位置\n");
//        printf("19.无人机悬停\n");
//        printf("20.打开无人机飞行轨迹\n");
        printf("输入操作项:\n");
        scanf("%d", &num);
    	printf("你输入的整数是： %d\n", num);
    	createPthread(); 
    	uavOperate(num);
}

bool init() {
	if (websocket_connect(&websocket,"ws://127.0.0.1:10088/cxx") < 0) {
		printf("WebSocket连接失败");
		return false;
	}
	printf("websocket连接成功");
	createPthread(); 
	initUav();
	return true;
}	

//初始化无人机场景
void initUav() {
	  event_id = event_id + 1;
	  uav.event_id = 1;
	  strcpy(uav.type, "other");
	  strcpy(uav.commond, "init_scene");
	  char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	  sprintf(json_str, "{\"event_id\":%i,\"type\":\"%s\",\"commond\":\"%s\"}"
       , uav.event_id, uav.type, uav.commond);
	  sendMessage(json_str);	
} 

//初始化返航点
void init_position() {
	 // 为结构体成员变量赋值
     strcpy(uav.type, "wrj");
     strcpy(uav.commond, "init_position");
     number = number + 1;
     event_id = event_id + 1;
     uav.number = number;
     uav.event_id = event_id;
     char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	 sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"pos\":%s,\"commond\":\"%s\"}"
       , uav.number,uav.event_id, uav.type, "[0,0]", uav.commond);
     sendMessage(json_str);	
}; 

void set_name() {
	char name[100];
	printf("输入无人机名称:\n");
	scanf("%s", &name);
	strcpy(uav.type, "wrj");
    strcpy(uav.commond, "set_name");
    strcpy(uav.name, name);
    event_id = event_id + 1;
    uav.number = number;
    uav.event_id = event_id;
    char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"name\":\"%s\",\"type\":\"%s\",\"commond\":\"%s\"}"
       , uav.number,uav.event_id,name, uav.type, uav.commond);
    sendMessage(json_str);
}

void set_color() {
	char color_1[8];
	printf("无人机模型区域1颜色(默认 #FFFFFF)：");
	scanf("%s", &color_1);
	if(color_1 == ""){
		strcpy(color_1, "#FFFFFF");
	}
	char color_2[8];
	printf("无人机模型区域2颜色(默认 #000000)：");
	scanf("%s", &color_2);
	if(color_2 == ""){
		strcpy(color_2, "#000000");
	}
	strcpy(uav.type, "wrj");
    strcpy(uav.commond, "set_color");
    strcpy(uav.color_1, color_1);
    strcpy(uav.color_2, color_2);
    event_id = event_id + 1;
    uav.number = number;
    uav.event_id = event_id;
    char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"color_1\":\"%s\",\"color_2\":\"%s\",\"commond\":\"%s\"}"
       , uav.number,uav.event_id,uav.type,color_1,color_2,uav.commond);
    sendMessage(json_str);
}

void start_engine() {
	strcpy(uav.type, "wrj");
    strcpy(uav.commond, "start_engine");
    event_id = event_id + 1;
	uav.number = number;
    uav.event_id = event_id;
    char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"commond\":\"%s\"}"
       , uav.number,uav.event_id, uav.type, uav.commond);
	sendMessage(json_str);
}	

void shut_down_engine() {
		strcpy(uav.type, "wrj");
    	strcpy(uav.commond, "shut_down_engine");
    	event_id = event_id + 1;
		uav.number = number;
    	uav.event_id = event_id;
    	char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
		sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"commond\":\"%s\"}"
       , uav.number,uav.event_id, uav.type, uav.commond);
		sendMessage(json_str);	
}

void set_rotor_power() {
	    char rotor[30];
	    memset(rotor, 0, sizeof(rotor));
		printf("输入旋翼输出参数(四个参数以,分割 例如：1,2,3,4):");
		scanf("%s", &rotor);
		strcpy(uav.type, "wrj");
    	strcpy(uav.commond, "set_rotor_power");
    	event_id = event_id + 1;
		uav.number = number;
    	uav.event_id = event_id;
    	char *data = (char *)malloc(sizeof(char) * 100);
    	sprintf(data,"{\"power\":[%s]}",rotor);
    	char *json_str = (char *)malloc(sizeof(char) * 400); // 假设 JSON 字符串最大长
		sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"parameters\":%s,\"commond\":\"%s\"}"
        , uav.number,uav.event_id,uav.type,data,uav.commond);
		sendMessage(json_str);	
}

void open_lighter() {
		char color[20];
		printf("设置灯光颜色：");
		scanf("%s", &color);
	    strcpy(inParams.color, color);
	    inParams.intensity = 1.0;
	    inParams.halo = 1.0;
		strcpy(uav.type, "wrj");
    	strcpy(uav.commond, "open_lighter");
    	event_id = event_id + 1;
		uav.number = number;
    	uav.event_id = event_id;
    	char *data = (char *)malloc(sizeof(char) * 100);
    	sprintf(data,"{\"intensity\":%i,\"color\":\"%s\",\"halo\":%i}",inParams.intensity,inParams.color,inParams.halo);
    	char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
		sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"parameters\":%s,\"commond\":\"%s\"}"
        , uav.number,uav.event_id,uav.type,data,uav.commond);
		sendMessage(json_str);	
}

void close_lighter() {
		strcpy(uav.type, "wrj");
    	strcpy(uav.commond, "close_lighter");
    	event_id = event_id + 1;
		uav.number = number;
    	uav.event_id = event_id;
    	char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
		sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"commond\":\"%s\"}"
        , uav.number, uav.event_id,uav.type, uav.commond);
		sendMessage(json_str);	
}

void get_current_height() {
	   strcpy(uav.type, "wrj");
       strcpy(uav.commond, "get_current_height");
       event_id = event_id + 1;
	   uav.number = number;
       uav.event_id = event_id;	
       char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
		sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"commond\":\"%s\"}"
        , uav.number, uav.event_id,uav.type, uav.commond);
	   sendMessage(json_str);
}

void get_current_attitude_angle() {
	  strcpy(uav.type, "wrj");
      strcpy(uav.commond, "get_current_attitude_angle");
      event_id = event_id + 1;
	  uav.number = number;
      uav.event_id = event_id;
      char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	  sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"commond\":\"%s\"}"
      , uav.number, uav.event_id,uav.type, uav.commond);
	  sendMessage(json_str);
}

void get_current_distance() {
	  strcpy(uav.type, "wrj");
      strcpy(uav.commond, "get_current_distance");
      event_id = event_id + 1;
	  uav.number = number;
      uav.event_id = event_id;
      char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
		sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"commond\":\"%s\"}"
        , uav.number, uav.event_id,uav.type, uav.commond);
	  sendMessage(json_str);
}

void get_current_horizontal_speed() {
	  strcpy(uav.type, "wrj");
      strcpy(uav.commond, "get_current_horizontal_speed");
      event_id = event_id + 1;
	  uav.number = number;
      uav.event_id = event_id;
      char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
		sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"commond\":\"%s\"}"
        , uav.number, uav.event_id,uav.type, uav.commond);
	  sendMessage(json_str);
}

void get_current_vertical_speed() {	
	  strcpy(uav.type, "wrj");
      strcpy(uav.commond, "get_current_vertical_speed");
      event_id = event_id + 1;
	  uav.number = number;
      uav.event_id = event_id;
      char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
		sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"commond\":\"%s\"}"
        , uav.number, uav.event_id,uav.type, uav.commond);
	  sendMessage(json_str);
}

void open_hd() {
	 strcpy(uav.type, "wrj");
     strcpy(uav.commond, "open_hd");
     event_id = event_id + 1;
	 uav.number = number;
     uav.event_id = event_id;
     char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
		sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"commond\":\"%s\"}"
        , uav.number, uav.event_id,uav.type, uav.commond);
	 sendMessage(json_str);
}

void close_hd() {
	 strcpy(uav.type, "wrj");
     strcpy(uav.commond, "close_hd");
     event_id = event_id + 1;
	 uav.number = number;
     uav.event_id = event_id;
     char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
		sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"type\":\"%s\",\"commond\":\"%s\"}"
        , uav.number, uav.event_id,uav.type, uav.commond);
	 sendMessage(json_str);
}

void fly_by_3d_direction() {
	 char powers[7];
	 memset(powers, 0, sizeof(powers));
	 printf("设置飞行方向,三个参数[X:前后,Y:左右,Z:上下]以,分割(示例：1,2,3):");
	 scanf("%s", &powers);
	 if(sizeof(powers) != 7){
	 	int code;
	 	printf("输入旋翼参数异常,1:重新输入,0:退出");
	 	scanf("%c", &code);
	 	if(code == 0){
                    return;
    	}else if(code == 1){
                    setRotorPower();
                    return;
        }
	 }
	 strcpy(uav.type, "wrj");
     strcpy(uav.commond, "fly_by_3d_direction");
	 int speed;
	 printf("设置飞行速度(米/秒):");
	 scanf("%i", &speed);
	 uav.speed = speed;
	 int duration;
	 printf("飞行时间(秒):");
	 scanf("%i", &duration);
	 uav.time = duration;
     event_id = event_id + 1;
	 uav.number = number;
     uav.event_id = event_id;
     char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
		sprintf(json_str, "{\"number\":%i,\"event_id\":%i,\"time\":%i,\"type\":\"%s\",\"speed\":%i,\"commond\":\"%s\",\"direction\":[%s]}"
        , uav.number, uav.event_id,duration,uav.type,speed, uav.commond, powers);
	 sendMessage(json_str);
}
