#include <stdio.h>
#include <stdlib.h>
#include "ShimLib.h"
typedef  void(*error_cb)(const char* msg);

int statusCountFlag = 0;
error_cb innerCallback = NULL;


int InitLib(error_cb cb) {
	innerCallback = cb;
	innerCallback("11111");
	return 0;
}

int ScanControl(ScanAction actionType) {
	switch (actionType) {
	case SCAN:
		printf("Do Scan!\r\n");
		statusCountFlag = 2;
		break;
	case PREVIEW_SCAN:
		statusCountFlag = 1;
		printf("Do preview!\r\n");
		break;
	case STOP:
		statusCountFlag = 0;
		printf("Stop everything!\r\n");
		break;
	}
	return 0;
}

StatusInfo GetStatus() {
	StatusInfo inf;
	switch (statusCountFlag)
	{
	case 0:
		inf.status = READY;
		break;
	case 1:
		inf.status = SCANNING;
		inf.progress = 0.0f;
		break;
	case 2:
		inf.status = SCANNING;
		inf.progress = 0.3f;
		statusCountFlag++;
		break;
	case 3:
		inf.status = SCANNING;
		inf.progress = 0.6f;
		statusCountFlag++;
		break;
	case 4:
		inf.status = SCANNING;
		inf.progress = 0.9f;
		statusCountFlag++;
		break;
	case 5:
		inf.status = SCANNING;
		inf.progress = 1.0f;
		statusCountFlag = 0;
		break;
	case 6:
		//        case 7:
		//            inf.status = BUSY;
		//            statusCountFlag++;
		//            break;
		//        case 8:
		inf.status = BUSY;
		statusCountFlag = 0;
		break;
	default:
		inf.status = BUSY;
		statusCountFlag = 0;
	}
	return inf;
}

//result, 0 success, other false
int SetScanInfo(const char* jsonString, int empty) {
	return 0;
}

int preivew_change_flag = 0;

const size_t Row = 140;
const size_t Col = 140;

#define BUFFER_SIZE Row * Col

const char* FRAME_FILE_PATH_1 = "./img1_v2.bin";
const char* FRAME_FILE_PATH_2 = "./pre_image.bin";
const char* GetPreviewData() {
	FILE* file;
	preivew_change_flag++;
	preivew_change_flag = preivew_change_flag % 2;
	//    _sleep(2000);
	if (file = fopen(preivew_change_flag ? FRAME_FILE_PATH_1 : FRAME_FILE_PATH_2, "rb")) {
		unsigned char* buffer = malloc(sizeof(unsigned char) * BUFFER_SIZE);
		fread(buffer, sizeof(unsigned char), BUFFER_SIZE, file);
		fclose(file);
		return buffer;
	}

	return NULL;
}

const char* GetDeviceInfo(DeviceInfo infoType) {
	switch (infoType) {
	case  MEAN_TEMPERATURE:
		return "28";
	}
	return "";
}
