#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "main.h"

/**
 * @brief  Clears buffer
 * @param  pointer to char array used as buffer
 * @retval None
 */
void GPS_ClearBuffs(char *rec) {
	for (int var = 0; var < sizeof(rec); ++var) {
		rec[var] = (char) 0x00;
	}
}

/**
 * @brief  Loads if there are some data in chose part of buffer
 * @param  chosen part of buffer
 * @retval bool value, if ther isn't something returns true otherwise false
 */
bool GPS_Load(char buf) {
	if (buf != (char) 0x00) {
		return false;
	} else {
		return true;
	}
}

/**
 * @brief  Finds index of character, where starts substring, we are looking for
 * in our case we are looking for example for GLL or PRM
 * @param  pointer to unsigned char array with source data in our case it is received GPS NMEA output
 * @param poiter to unsigned char array with string, we are looking for (example: "GLL", "PRM", "text")
 * @retval number with index of first letter of message, we are looking for
 */
int GPS_FindIdx(unsigned char *input, const char *target) {
	unsigned char *result = strstr(input, target);
	int position = result - input;
	return position;
}

//		    10	      20
//0123456789012345678901234567
//GLL,4947.8943,N,01728.0003,E
//			10		  20	    30		  40
//01234567890123456789012345678901234567890123456
//49 dgr 47.8943 min N0017 dgr 28.0003 min E
/**
 * @brief  Read position data from GPS NMEA output saves them
 * @param  pointer to unsigned char array to save data
 * @param  pointer to unsigned char array with NMEA GPS output
 * @param  number index of G letter in "$GLL..." (output of positionIdx function)
 * @retval bool value, if operation end successful returns true otherwise false
 */
bool GPS_LoadPosition(unsigned char *output, unsigned char *rec, int index) {
	if (rec[index + 4] != (char) 0x2c) {
		for (int var = index + 4; var < index + 6; ++var) {
			output[var - index - 4] = rec[var];
		}
		char sup[] = " deg ";
		for (int var = 2; var < 2 + sizeof(sup); ++var) {
			output[var] = sup[var - 2];
		}
		for (int var = index + 6; var < index + 13; ++var) {
			output[var - index + 1] = rec[var];
		}
		char sup1[] = " min ";
		for (int var = 14; var < 14 + sizeof(sup1); ++var) {
			output[var] = sup1[var - 14];
		}
		output[19] = rec[index + 14]; //add latitude direction
		output[20] = (char) 0x20;

		// load alt deg
		for (int var = index + 16; var < index + 19; ++var) {
			output[var - index + 5] = rec[var];
		}
		// add deg unit
		for (int var = 24; var < 24 + sizeof(sup); ++var) {
			output[var] = sup[var - 24];
		}
		//add alt min
		for (int var = index + 19; var < index + 28; ++var) {
			output[var - index + 10] = rec[var];
		}
		//add min unit
		for (int var = 36; var < 36 + sizeof(sup1); ++var) {
			output[var] = sup1[var - 36];
		}
		output[41] = rec[index + 27]; //add altitude direction
		return true;
	} else {
		return false;
	}

}

/**
 * @brief  Read position data from GPS NMEA output saves them
 * @param  pointer to unsigned char array to save data
 * @param  pointer to unsigned char array with NMEA GPS output
 * @param  number index of P letter in "$PRM..." (output of courseIdx function)
 * @retval bool value, if operation end successful returns true otherwise false
 */
void GPS_LoadCourse(unsigned char *output, unsigned char *rec, int index) {
	int commaCount = 0;
	int move = 1;
	while (commaCount != 8) {
		if (rec[index + move] == (char) 0x2c) {
			commaCount += 1;
			move += 1;
		} else {
			move += 1;
		}
	}
	int var = 0;
	while (rec[index + move] != (char) 0x2c) {
		output[var] = rec[index + move];
		var += 1;
		move += 1;
	}
}

/**
 * @brief  turns GPS module to stand by mode
 * @param chosen UART interface
 * @retval none
 */
void GPS_Standby(UART_HandleTypeDef *huart) {
	unsigned char command[] = "$PMTK161,0*28\r\n";
	HAL_UART_Transmit(huart, command, sizeof(command), 100);
}

/**
 * @brief turns GPS module to full on mode
 * @param chosen UART interface
 * @retval none
 */
void GPS_WakeUp(UART_HandleTypeDef *huart) {
	unsigned char command[] = " ";
	HAL_UART_Transmit(huart, command, sizeof(command), 10);
}
