#include <iostream>
#include <string>
#include <stdexcept>
#include <iomanip>
#include <vector>
#include <cstring>
#include <boost/algorithm/hex.hpp>
#include <boost/variant.hpp>
#include <boost/variant/apply_visitor.hpp>
 
// Stored VIN
std::string m_vinId {"3030303030"};
 
// Simulated payload
boost::variant<std::string, std::vector<uint8_t>> payload = R"({
  "message version": "1.0",
  "VIN": "",
  "COmmand Type": "ACTIVE",
  "COmmand 1": "5"
})";
 
// Function to verify VIN
bool verifyVIN(const std::string& payload) {
    // Find the position of "VIN" in the payload
    size_t vinPos = payload.find("\"VIN\"");
    if (vinPos == std::string::npos) {
        std::cout << "VIN not found in payload" << std::endl;
        return false;
    }
 
    // Extract the VIN value from the payload
    size_t startPos = payload.find("\"", vinPos + 5) + 1; // Find starting quote
    size_t endPos = payload.find("\"", startPos);         // Find ending quote
    std::string vinValue = payload.substr(startPos, endPos - startPos);
 
    // Check if the VIN value is empty
    if (vinValue.empty()) {
        std::cout << "VIN is empty" << std::endl;
        return false;
    }
 
    // Check if the received VIN matches the stored VIN
    if (vinValue != m_vinId) {
        std::cout << "Received VIN does not match stored VIN" << std::endl;
        return false;
    }
 
    std::cout << "VIN is valid and matches stored VIN: " << vinValue << std::endl;
    return true;
}
 
// Function to parse the SMS content (simulated payload here)
std::string parseSmscontent(const boost::variant<std::string, std::vector<uint8_t>>& payload) {
	std::string message;
 
	/* If the payload is a string, assign it to message */
	if (const std::string* strPayload = boost::get<std::string>(&payload)) {
		message = *strPayload;
	} else if (const std::vector<uint8_t>* binaryPayload = boost::get<std::vector<uint8_t>>(&payload)) {
		/* Convert binary payload to string if necessary */
		message.assign(binaryPayload->begin(), binaryPayload->end());
	} else {
		std::cout << "Unknown payload type!"<< std::endl;
		return "";
	}
 
    if (!verifyVIN(message)) {
        std::cout << "Invalid SMS content. Ignoring..." << std::endl;
        return "";
    }
 
    return message;
}
 
int main() {
    std::string message = parseSmscontent(payload);
 
    if (message.empty()) {
        std::cout << "Invalid SMS content. Ignoring... in main" << std::endl;
    } else {
        std::cout << "Processed message: " << message << std::endl;
    }
 
    return 0;
}