#include "common.h"

static const char *filePrefix = NULL;
static int fileNum = 0;
static const char *target_ip = NULL;
static dataType_e dataType = TEXT_LOG;
static int target_port = 0;

void* thread_function(void* arg) {
    // 初始化网络
    struct sockaddr_in dest_addr;
    int sockfd = init_udp_network(target_ip, target_port, &dest_addr);
    if (sockfd < 0) {
        printf("网络初始化失败\n");
        return NULL;
    }

    char* packBuffer = malloc(MAX_PACK_BUFFER_LEN);
    if (!packBuffer)
    {
        printf("内存分配失败\n");
        return NULL;
    }

    uint32_t cnt = 1;
    char filename[MAX_FILENAME_LENGTH] = {0};
    while (1)
    {
        // 初始化文件路径
        memset(filename, 0, MAX_FILENAME_LENGTH);
        snprintf(filename, MAX_FILENAME_LENGTH, "%s%04d.jpg", filePrefix, cnt++);

        // 打开JPEG文件
        FILE *file = fopen(filename, "rb");
        if (!file) {
            printf("打开文件失败:%s\n", filename);
            close(sockfd);
            break;
        }

        // 初始化packBuffer
        memset(packBuffer, 0, MAX_PACK_BUFFER_LEN);

        // 读取JPEG文件(如果发送JPEG数据则直接读取进内存，如果要发送RGB/GRAY数据，则转换为对应格式数据后再放进内存)
        uint8_t *rgb_data = packBuffer + sizeof(packHead_s);
        int width = 0, height = 0, filesize = 0;
        if (read_jpeg_file(file, dataType, &rgb_data, &width, &height, &filesize)) {
            printf("读取JPEG文件失败\n");
            fclose(file);
            close(sockfd);
            break;
        }

        // 提取帧大小(数据段大小)
        int frame_size = parse_frame_size(dataType, width, height, filesize);
        if (!frame_size)
        {
            printf("不支持的帧类型\n");
            fclose(file);
            close(sockfd);
        }

        // 填充数据包
        int32_t realPackBuffLen = 0;
        switch (dataType)
        {
        case GRAPH_JPG:
            realPackBuffLen = fill_pack_for_jpg(packBuffer, MAX_PACK_BUFFER_LEN, rgb_data, frame_size, dataType);
            break;
        case GRAPH_RGB555:
        case GRAPH_RGB565:
        case GRAPH_GRAY8:
            realPackBuffLen = fill_pack_for_pixel_image(packBuffer, MAX_PACK_BUFFER_LEN, rgb_data, frame_size, dataType, width, height);
            break;
        default:
            break;
        }

        ssize_t total_sent = send_buffer(sockfd, &dest_addr, packBuffer, realPackBuffLen);
        if (total_sent > 0)
            printf("发送数据：%s，共发送%dB，帧类型：%d 帧大小：%dB，分辨率：%dx%d\n",
                filename, total_sent, dataType, frame_size, width, height);
        else
            printf("数据发送失败\n");

        // 释放内存
        fclose(file);

        if (cnt > fileNum)
            cnt = 1;

        if (fileNum == 1)
        {
            printf("只有一个文件，默认不循环发送\n");
            break;
        }
        usleep(1000*20);
    }

    free(packBuffer);
    close(sockfd);
    return NULL;
}

int main(int argc, char *argv[]) {
    if (argc != 6) {
        fprintf(stderr, "用法: %s <目标IP> <目标端口> <GRAPH_JPG/GRAPH_RGB565/GRAPH_RGB555/GRAPH_GRAY8> <jpeg文件前缀> <jpeg文件数量> \n", argv[0]);
        return 1;
    }

    // 解析参数
    target_ip   = argv[1];
    target_port = atoi(argv[2]);
    if (strcmp(argv[3], "GRAPH_JPG") == 0) {
        dataType = GRAPH_JPG;
    } else if (strcmp(argv[3], "GRAPH_RGB565") == 0) {
        dataType = GRAPH_RGB565;
    } else if (strcmp(argv[3], "GRAPH_RGB555") == 0) {
        dataType = GRAPH_RGB555;
    } else if (strcmp(argv[3], "GRAPH_GRAY8") == 0) {
        dataType = GRAPH_GRAY8;
    } else {
        printf("无效的帧类型\n");
        return 1;
    }
    filePrefix  = argv[4];
    fileNum     = atoi(argv[5]);

    pthread_t thread;

    // 创建线程
    if (pthread_create(&thread, NULL, thread_function, NULL) != 0) {
        printf("线程创建失败\n");
        return 1;
    }

    printf("主线程继续执行...\n");

    // 等待线程结束并获取返回值
    if (pthread_join(thread, NULL) != 0) {
        printf("线程等待失败\n");
        return 1;
    }

    printf("线程返回\n");
    return 0;
}
