#include "tank_common.h"
#include "devcontrol_common.h"
#include "gpio_init.h"
#include "pthrpoll.h"

_Thread_local const tank_common_back *g_tank_common_config_t = NULL;

long long shot_device_time_start =0 ;
long long shot_device_time_end = 0;

int tank_shot_back_stop(int pin,int val);


void tank_shot_back(unsigned char gval) {
	if (gval < 50) {
		pwmWrite(PWM_PIN_CHANGE, 75);
	} else if (gval <= 55) {
		pwmWrite(PWM_PIN_CHANGE, 71);
	} else if (gval <= 60) {
		pwmWrite(PWM_PIN_CHANGE, 70);
	} else if (gval <= 65) {
		pwmWrite(PWM_PIN_CHANGE, 70);
	} else if (gval <= 70) {
		pwmWrite(PWM_PIN_CHANGE, 70);
	} else if (gval <= 75) {
		pwmWrite(PWM_PIN_CHANGE, 69);
	} else if (gval <= 90) {
		pwmWrite(PWM_PIN_CHANGE, 68);
	} else if (gval <= 100) {
		pwmWrite(PWM_PIN_CHANGE, 68);
	} else if (gval <= 110) {
		pwmWrite(PWM_PIN_CHANGE, 66);
	} else if (gval <= 120) {
		pwmWrite(PWM_PIN_CHANGE, 65);
	} else if (gval <= 130) {
		pwmWrite(PWM_PIN_CHANGE, 64);
	} else if (gval <= 140) {
		pwmWrite(PWM_PIN_CHANGE, 62);
	} else if (gval <= 150) {
		pwmWrite(PWM_PIN_CHANGE, 60);
	} else if (gval <= 160) {
		pwmWrite(PWM_PIN_CHANGE, 59);
	} else if (gval <= 170) {
		pwmWrite(PWM_PIN_CHANGE, 58);
	} else if (gval <= 180) {
		pwmWrite(PWM_PIN_CHANGE, 57);
	} else if (gval <= 190) {
		pwmWrite(PWM_PIN_CHANGE, 56);
	} else if (gval <= 200) {
		pwmWrite(PWM_PIN_CHANGE, 55);
	}    
}
 
const tank_common_back tank_common_config_t[]={
    {
        .device_id = DEVICE_TANK0202,
        .back_time=15,//后退多少ms
        .back_interval_back=200, //间隔多少ms才能再次使用
        .shot_back_speed=80,
        .shot_back=tank_shot_back_stop
        
    },
    {
        .device_id = DEVICE_TANK0203,
        .back_time=15,//后退多少ms
        .back_interval_back=200, //间隔多少ms才能再次使用
        .shot_back_speed=80,
        .shot_back=tank_shot_back_stop
    },

    { .device_id = -1 }
};

void tank_shot_back_stop_task_function(void *arg) {//多线程处理坦克发射后退线程池
	while(1){
		long long interval=shot_device_time_start-shot_device_time_end;
		if(g_device_delay_count > g_tank_common_config_t->back_time&&g_device_delay_count < (g_tank_common_config_t->back_time+30)) 
                tank_shot_back(0);

		if(interval > g_tank_common_config_t->back_interval_back){
			if(g_device_delay_count <= g_tank_common_config_t->back_time) {
				tank_shot_back(g_tank_common_config_t->shot_back_speed);
				my_zlog_debug("操作耗时: %lld 毫秒", interval);
			}
		}

	}
	free(arg);
}

ThreadPool_t *pool_tank_t;

void tank_shot_pthrpoll_task_init(){
	int *arg = malloc(sizeof(int));
	*arg = 1;
	pool_tank_t=thread_pool_init(1,1);
	thread_pool_add_task(pool_tank_t, tank_shot_back_stop_task_function, &arg);
	my_zlog_debug("线程池打开");
}

int tank_shot_back_stop(int pin,int val){
	static int shot_count=0;
	shot_device_time_start=get_current_time_millis();
	if(pin !=27){
		my_zlog_debug("非27引脚");
		return 1;
	}	
	
	if(val == 0) {
		tank_shot_back(0);
		shot_count=2;
	}
	if(val != 0) {
		if(shot_count == 0){
			tank_shot_pthrpoll_task_init();
			shot_count=1;
		}
		shot_count=1;
		if(shot_count == 1){
			g_device_delay_count=0;
		}
	}

	shot_device_time_end=get_current_time_millis();
}

/*销毁坦克使用的线程池，让其正常销毁，只有在tank0202设备号下才有用*/
void tank_thread_close(){
	thread_pool_destroy(pool_tank_t);
}

void tank_shot_stop_control(int device_id,int pin,int val) {
    if (!g_tank_common_config_t || g_tank_common_config_t->device_id != device_id) {
        // 查找设备配置
        for(int i = 0; tank_common_config_t[i].device_id != -1; i++) {
            if(tank_common_config_t[i].device_id == device_id) {
                g_tank_common_config_t = &tank_common_config_t[i];
                break;
            }
        }
    }
    
    if(!g_tank_common_config_t) {
        my_zlog_error("Error: Device stop ID %d not found!\n", device_id);
        return;
    }
    
    g_tank_common_config_t->shot_back(pin,val);
             

}