#include "uv.h"
#include "timer.h"
#include "pthread.h"
#include "platmacro.h"

static void* __test_timer_produce_thread(void* arg)
{
    return NULL;
}

static int __test_timer_consume_thread(void* arg)
{
    int num = 0;
    return num;
}

static int trig_times = 0;
void timer_once_callback(void* para)
{
    trig_times++;
    printf("once trig times %d \n", trig_times);
}

static int trig_loop_times = 0;
void timer_loop_callback(void* para)
{
    trig_loop_times++;
    printf("loop trig times %d \n", trig_loop_times);
}


void once_timer_test(void)
{
    bs_timer_t bs_timer = NULL;
    bs_timer_t bs_timer_loop = NULL;

    printf("once timer start \n");
    bs_timer = timer_single_init(timer_once_callback, 200, 0, NULL);
    if (bs_timer == NULL) {
        printf("once timeer create error\n");
    }
    bs_timer_loop = timer_single_init(timer_loop_callback, 200, 1, NULL);
    if (bs_timer_loop == NULL) {
        printf("loop timeer create error\n");
    }
    if (0 != timer_single_start(bs_timer_loop)) {
        printf("loop timeer start error\n");
    }
    if (0 != timer_single_start(bs_timer)) {
        printf("once timeer start error\n");
    }
    uv_sleep(5000);
    if (0 != timer_single_stop(bs_timer)) {
        printf("once timeer stop error\n");
    }
    if (0 != timer_single_stop(bs_timer_loop)) {
        printf("loop timeer stop error\n");
    }
    if (0 != timer_single_start(bs_timer)) {
        printf("once timeer restart error\n");
    }
    if (0 != timer_single_start(bs_timer_loop)) {
        printf("loop timeer restart error\n");
    }
    uv_sleep(5000);
    if (0 != timer_single_close(bs_timer)) {
        printf("once timeer remove error\n");
    }
    if (0 != timer_single_close(bs_timer_loop)) {
        printf("loop timeer remove error\n");
    }
}

int trig[200] = {0};

static void loop_timer_handler(void *arg)
{
    int *p = arg;
    unsigned int timer = *(int*)p;
    if (timer < 50)
        printf("list timer %d trig %d \n", timer, ++trig[timer]);
    else {
        printf("list error %d \n",timer);
    }
    return;
}

int timer_id[20] = { 0 };
void list_timer(void)
{ 
    timer_list_t list_timer = timer_list_create( 20 );
    if(list_timer == NULL){
        printf("timer error \n");
    }
    for(int i = 0; i < 20; i++){
        int ret =  timer_list_add_one(list_timer,  loop_timer_handler, i *200,  1, &timer_id[i]);
        if(ret > 0){
            timer_id[i] = ret;
            printf("timer create ok %d \n", i);
        }
        else{
            printf("timer create error %d \n",i);
        }
    }

    uv_sleep(20*1000);
    for (int i = 0; i < 20; i++) {
        int ret = timer_list_stop_one(list_timer, timer_id[i]);
        if (ret == 0) {
            timer_id[i] = ret;
            printf("timer stop ok %d \n", i);
        }
        else {
            printf("timer stop error %d \n", i);
        }
    }
    uv_sleep(20 * 1000);

    for (int i = 0; i < 20; i++) {
        int ret = timer_list_start_one(list_timer, timer_id[i]);
        if (ret == 0) {
            timer_id[i] = ret;
            printf("timer start ok %d \n", i);
        }
        else {
            printf("timer start error %d \n", i);
        }
    }
    uv_sleep(50 * 1000);
    for (int i = 0; i < 20; i++) {
        int ret = timer_list_remove_one(list_timer, timer_id[i]);
        if (ret == 0) {
            timer_id[i] = ret;
            printf("timer remove ok %d \n", i);
        }
        else {
            printf("timer remove error %d \n", i);
        }
    }
    uv_sleep(10 * 1000);    
    timer_list_close(list_timer);
    uv_sleep(10 * 1000);
    printf("list timer end \n");

}

void test_timer(void)
{    
    once_timer_test();

    list_timer();

    while(1){
        uv_sleep(1000);
    }

    pthread_t thread_id, thread_id2;
    if(0!=pthread_create(&thread_id, NULL,(void*)__test_timer_produce_thread , NULL)){
        printf("fofo thread error\r\n");
    }

    if(0!=pthread_create(&thread_id2, NULL,(void*)__test_timer_consume_thread , NULL)){
        printf("fofo thread error\r\n");
    }    

    pthread_join(thread_id, NULL);
    pthread_join(thread_id2, NULL);

}



