#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <exception>
#include <iostream>
#include"ANSLicense.h"

//#include <Windows.h>
//#include <Wincrypt.h>

int ANSFinalTemplate_ActivationExample() {
	std::string  privateKey = "AQlSAiRTNtS7X20=";
	std::string URL = "http://localhost:3001/";
	ANSCENTER::ANSLSHelper ansHelper(privateKey, URL);
	//std::string licenseTempatePath = "C:\\Projects\\ANLS\\LicenseTemplates\\ANSFinalTemplate.xml";
	//bool loaded = ansHelper.LoadLicenseTemplate(licenseTempatePath);
	//std::cout << "Load license template from file:" << loaded << std::endl;
	// For fun to get HWID
	std::string hwid = ansHelper.GetCurrentHardwareId();
	std::cout << "HWID:" << hwid << std::endl;
	std::string registrationName = "Alex Nguyen";
	int productId = 1002;
	int enableFeature = 1;
	// Generate key based on productId
	std::string generatedKey = ansHelper.GenerateKey(productId, registrationName, enableFeature, false);
	std::cout << "Key:" << generatedKey << std::endl;
	bool isLicenseKeyValid = ansHelper.IsLicenseKeyValid(generatedKey, registrationName);
	std::cout << "Is license key valid:" << isLicenseKeyValid << std::endl;
	enableFeature = 0;
	bool result = ansHelper.CheckLicenseKey(generatedKey, registrationName, productId, enableFeature);
	std::cout << "validation result:" << result << std::endl;
	std::cout << "enable feature:" << enableFeature << std::endl;
	std::string activationKey = "";
	int validationResult;
	validationResult = ansHelper.ValidateLicense(generatedKey, registrationName, activationKey);
	validationResult = ansHelper.ActivateLicense(generatedKey, registrationName, activationKey);
	std::cout << "validation result:" << validationResult << std::endl;
	std::cout << "activation Key:" << activationKey << std::endl;
	std::string   trialKey = "7JBVC-PGKW4-BCLXF-ERTJQ-JKHBT-KVLHR";
	std::string licenseData = ansHelper.GetLicenseData(trialKey);
	std::cout << "License Data" << licenseData << std::endl;
	std::string offlineActivationData = "";
	int resultOL= ansHelper.GenerateOfflineActivationData(generatedKey, registrationName, offlineActivationData);
	std::cout << "Offline Activation Data" << offlineActivationData << std::endl;
	return 0;
}
int GenerateKey() {
	
		std::string  privateKey = "AQlSAiRTNtS7X20=";
		std::string URL = "http://localhost:3001/";
		ANSCENTER::ANSLSHelper ansHelper(privateKey, URL);
		std::string licenseTempatePath = "C:\\Projects\\ANLS\\LicenseTemplates\\ANSFinalTemplate.xml";
		bool loaded = ansHelper.LoadLicenseTemplate(licenseTempatePath);
		std::cout << "Load license template from file:" << loaded << std::endl;
		// For fun to get HWID
		std::string hwid = ansHelper.GetCurrentHardwareId();
		std::cout << "HWID:" << hwid << std::endl;
		std::string registrationName = "Thinh Nguyen";
		int productId = 1002;
		int enableFeature = 12;
		int efea = 0;
		// Generate key based on productId
		std::string generatedKey = ansHelper.GenerateKey(productId, registrationName, enableFeature, false);
		std::cout << "Key:" << generatedKey << std::endl;
		bool valid= ansHelper.CheckLicenseKey(generatedKey,registrationName, productId, efea);
		std::cout << "License Valid:" << valid << " - feature:"<<efea<<std::endl;
		std::string st = std::to_string(efea);
		std::cout << "st:" << st<< std::endl;
		std::string licenseData= ansHelper.GetLicenseData(generatedKey);
		std::cout << "License Data:" << licenseData << std::endl;

		// Activate license key
		std::string licenseKey = "YGYSC-ABEE2-7UWKA-P9KQY-DJ95M-9TL5W";
		std::string activationKey = "";
		int validationResult = ansHelper.ActivateLicense(generatedKey, registrationName, activationKey);
		std::cout << "Activation Ket:" << activationKey << std::endl;

		return 0;

}
int ActivateOfflineLicense() {
	//std::string  privateKey = "AQlSAiRTNtS7X20=";
	//std::string URL = "http://localhost:3001/";
	std::string  privateKey = "AQlSAiRTNtS7X20=";
	std::string URL = "https://licensingservice.anscenter.com/";
	ANSCENTER::ANSLSHelper ansHelper(privateKey, URL);

	std::string hwid = ansHelper.GetCurrentHardwareId();
	std::cout << "HWID:" << hwid << std::endl;
	std::string registrationName = "Alex Nguyen";
	int productId = 1002;
	int enableFeature = 1;


	std::string activationKey;
	std::string licenseKey = ansHelper.GenerateKey(productId, registrationName, enableFeature, false);
	std::cout << "Key:" << licenseKey << std::endl;

	int result = ansHelper.ActivateLicenseWithCustomHWID(licenseKey, registrationName, hwid,activationKey);
	std::cout << "Result:" << result << std::endl;

	std::cout << "Actication Key:" << activationKey << std::endl;
	//int result = ansHelper.InstallOfflineLicense(licenseKey, registrationName, activationKey);
	//std::cout << "Offline Activation Data:" << result << std::endl;

	return 0;
}
int ExtractModelZipFileTest() {
	std::string zipFileName = "C:\\Projects\\Models\\ansgenericodcpu.zip"; // Replace with your ZIP file path
	std::string modelOutputFolder;
	ANSCENTER::ANSLSHelper::ExtractModelZipFile(zipFileName,"","MyTest", modelOutputFolder);
	std::cout << "Model output folder:" << modelOutputFolder << std::endl;
	std::string zipFilename = "C:\\Projects\\Models\\MyTest1.zip";
	ANSCENTER::ANSLSHelper::ZipFolerWithPassword(modelOutputFolder, zipFilename, "Sh7O7nUe7vJ/417W0gWX+dSdfcP9hUqtf/fEqJGqxYL3PedvHubJag==");
	return 0;
}
int GetLicenseDataExample() {

	std::string  privateKey = "AQlSAiRTNtS7X20=";
	std::string URL = "http://localhost:3001/";
	ANSCENTER::ANSLSHelper ansHelper(privateKey, URL);
	std::string licenseTempatePath = "C:\\Projects\\ANLS\\LicenseTemplates\\ANSFinalTemplate.xml";
	bool loaded = ansHelper.LoadLicenseTemplate(licenseTempatePath);
	std::cout << "Load license template from file:" << loaded << std::endl;
	std::string licenseKey = "HE2JL-9JSFA-2QQ9K-FGSQQ-64KK9";
	std::string licenseData = ansHelper.GetLicenseData(licenseKey);
	std::cout << "License Data:" << licenseData << std::endl;

	return 0;

}
int TestLocalTemplate() {

	std::string  privateKey = "AQnZnrKokD4Qvbc=";
	std::string URL =  "https://licensingservice.anscenter.com/"; //"http://localhost:3001/"
	ANSCENTER::ANSLSHelper ansHelper(privateKey, URL);
	std::string licenseTempatePath = "C:\\Projects\\ANLS\\LicenseTemplates\\ANSFinalTemplate.xml";
	bool loaded = ansHelper.LoadLicenseTemplate(licenseTempatePath);
	std::cout << "Load license template from file:" << loaded << std::endl;
	// For fun to get HWID
	std::string hwid = ansHelper.GetCurrentHardwareId();
	std::cout << "HWID:" << hwid << std::endl;
	std::string registrationName = "ANSVIS";
	int productId = 1002;
	int enableFeature = 0;
	int efea = 0;

	std::string licenseKey = "7NBZA-FMVEN-ZDZ4K-KXFBK-TUUES-8PHMA";//ansHelper.GenerateKey(productId, registrationName, enableFeature, false);

	std::string licenseData = ansHelper.GetLicenseData(licenseKey);
	std::cout << "License Data:" << licenseData << std::endl;

	// Activate license key
	std::string activationKey = "";
	int validationResult = ansHelper.ActivateLicense(licenseKey, registrationName, activationKey);
	std::cout << "Activation Ket:" << activationKey << std::endl;

	return 0;

}
int GenerateOfflineActivationData() {

	std::string  privateKey = "AQlSAiRTNtS7X20=";
	std::string URL = "http://localhost:3001/";
	ANSCENTER::ANSLSHelper ansHelper(privateKey, URL);
	std::string licenseTempatePath = "C:\\Projects\\ANLS\\LicenseTemplates\\ANSFinalTemplate.xml";
	bool loaded = ansHelper.LoadLicenseTemplate(licenseTempatePath);
	std::cout << "Load license template from file:" << loaded << std::endl;
	// For fun to get HWID
	std::string hwid = ansHelper.GetCurrentHardwareId();
	std::cout << "HWID:" << hwid << std::endl;
	std::string registrationName = "Thinh Nguyen";
	int productId = 1002;
	int enableFeature = 12;
	int efea = 0;
	
	// Generate key based on productId
	std::string generatedKey = ansHelper.GenerateKey(productId, registrationName, enableFeature, false);
	std::cout << "Key:" << generatedKey << std::endl;
	std::string offlineActivationData = "";
	// Generate offline activationd data
	ansHelper.GenerateOfflineActivationData(generatedKey, registrationName, offlineActivationData);
	std::cout << "Offline Activation Data:" << offlineActivationData << std::endl;

	return 0;

}
int PrepareEgdeModel() {
	std::string zipFileName = "C:\\Projects\\AICamera\\Documentation\\Model\\GenericModel.zip"; // Replace with your ZIP file path
	std::string configContent = "";
	std::string labelMapContent = "";
	std::string modelZipFileContent = "";
	ANSCENTER::ANSLSHelper::PrepareEdgeModel(zipFileName, "", "egde123", "GenericModel", "3456", configContent, labelMapContent, modelZipFileContent);

	std::cout << "configContent:" << configContent << std::endl;
	std::cout << "labelMapContent:" << labelMapContent << std::endl;
	std::cout << "modelZipFileContent:" << modelZipFileContent << std::endl;

	return 0;
}
int ValidateExample() {

	std::string  privateKey = "AQlSAiRTNtS7X20=";
	std::string URL =  "https://licensingservice.anscenter.com/";
	ANSCENTER::ANSLSHelper ansHelper(privateKey, URL);
	std::string licenseTempatePath = "C:\\Projects\\ANLS\\LicenseTemplates\\ANSFinalTemplate.xml";
	bool loaded = ansHelper.LoadLicenseTemplate(licenseTempatePath);
	std::cout << "Load license template from file:" << loaded << std::endl;
	// For fun to get HWID
	std::string hwid = ansHelper.GetCurrentHardwareId();
	std::cout << "HWID:" << hwid << std::endl;
	std::string registrationName = "ANSVIS";
	std::string licenseKey = "7NB8A-V5DSA-YKHHX-52JGM-UJCQE-3K9VL";//GV6SA-AAA7W-6YSWG-9QRRN-QKUAF-EHYMH
	std::string activationKey;
	// Generate key based on productId
	std::string licenseData = ansHelper.GetLicenseData(licenseKey);
	std::cout << "licenseData:" << licenseData << std::endl;

	ansHelper.ValidateLicense(licenseKey, registrationName, activationKey);
	std::cout << "activationKey:" << activationKey << std::endl;
	bool validationResult = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey,10, registrationName);
	std::cout << "validationResult:" << validationResult << std::endl;

	return 0;

}
int getSystemInformation() {
	std::string systemInfo=ANSCENTER::ANSLSHelper::GetSystemHardwareInformation();
	std::cout << "system info:" << systemInfo << std::endl;
	std::string validationData = "QU5OSFVCLUxWAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==";
	std::string decodedData = ANSCENTER::ANSLicenseHelper::DecodeValidationData(validationData.c_str());
	std::cout << "validation data:" << decodedData << std::endl;
	std::string licenseString = ANSCENTER::ANSLicenseHelper::ListLicenses();
	std::cout << "Licenses=" << licenseString << std::endl;
	return 1;
}
int ModelConversionTest() {
	std::string modelZipFile = "C:\\Programs\\TrainingAIs\\ODHUB\\New folder\\Mask_1.zip";
	std::string modelPassword = "Sh7O7nUe7vJ/417W0gWX+dSdfcP9hUqtf/fEqJGqxYL3PedvHubJag==";
	int result = ANSModelConversion(modelZipFile.c_str(), modelPassword.c_str());
	std::cout << "Model conversion result:" << result << std::endl;
	return 0;
}

int RemoveTrialLicenseKey() {
	std::string registrationName = "ANSVIS";
	bool result = ANSCENTER::ANSLicenseHelper::RemoveTrialLicenseKeys(registrationName);
	std::cout << "Remove trial license key:" << result << std::endl;
	return 0;
}


int ActivateOfflineLicenseCHWID() {
	std::string  privateKey = "AQlSAiRTNtS7X20=";
	std::string URL = "https://licensingservice.anscenter.com/";
	ANSCENTER::ANSLSHelper ansHelper(privateKey, URL);

	std::string hwid = "HZ47Y-MDEFC-CCTBB-JVCGJ-Y3AHA";
	std::string registrationName = "ANSTS";
	int productId = 1008;
	int enableFeature = 1;

	std::string activationKey;
	std::string licenseKey = "8ABZB-MDY6K-CSL85-NXH4L-GL6G3-U9E3S";
	std::cout << "Key:" << licenseKey << std::endl;

	int result = ansHelper.ActivateLicenseWithCustomHWID(licenseKey, registrationName, hwid, activationKey);
	std::cout << "Result:" << result << std::endl;

	std::cout << "Actication Key:" << activationKey << std::endl;
	//int result = ansHelper.InstallOfflineLicense(licenseKey, registrationName, activationKey);
	//std::cout << "Offline Activation Data:" << result << std::endl;

	return 0;
}
int InstallOfflineActivationKey() {
	std::string  privateKey = "AQlSAiRTNtS7X20=";
	std::string URL = "https://licensingservice.anscenter.com/";
	ANSCENTER::ANSLSHelper ansHelper(privateKey, URL);
	std::string registrationName = "ANSTS";
	std::string activationKey = "GGNAA-AGA9T-TH74A-BSX63-N3Y6W-7277F";
	std::string licenseKey = "8AB2A-GJVND-K4SXB-W5S2D-E4ZA8-45ZFK";
	int result = ansHelper.InstallOfflineLicense(licenseKey, registrationName, activationKey);
	std::cout << "Offline Activation Data:" << result << std::endl;
	return 0;
}
// Test validate license with invalid license key
int TestValidateExample() {

	std::string  privateKey = "AQlSAiRTNtS7X20=";
	std::string URL = "https://licensingservice.anscenter.com/";
	ANSCENTER::ANSLSHelper ansHelper(privateKey, URL);
	//std::string licenseTempatePath = "C:\\Projects\\ANLS\\LicenseTemplates\\ANSFinalTemplate.xml";
	//bool loaded = ansHelper.LoadLicenseTemplate(licenseTempatePath);
	//std::cout << "Load license template from file:" << loaded << std::endl;
	// For fun to get HWID
	std::string systemhwid = ansHelper.GetCurrentHardwareId();
	//std::string hwid =  "HZ47Y-MDEFC-CCTBB-JVLYU-JLAEA";// ansHelper.GetCurrentHardwareId();
	//std::cout << "HWID:" << hwid << std::endl;
	std::cout << "System HWID:" << systemhwid << std::endl;
	//std::string registrationName = "ANSTS";
	//std::string licenseKey = "8ABTB-BPEQE-H3BR5-G53JX-8LW2N-MMXA2";// "8AB3A-3HVJX-YMU4T-7KXX4-B3XYS-A249S";// "7NB8A-V5DSA-YKHHX-52JGM-UJCQE-3K9VL";//GV6SA-AAA7W-6YSWG-9QRRN-QKUAF-EHYMH
	//std::string activationKey;
	//// Generate key based on productId
	//std::string licenseData = ansHelper.GetLicenseData(licenseKey);
	//std::cout << "licenseData:" << licenseData << std::endl;

	////ansHelper.ValidateLicenseWithCustomHWID(licenseKey, registrationName, hwid,activationKey);
	//ansHelper.ValidateLicense(licenseKey, registrationName, activationKey);

	//std::cout << "activationKey:" << activationKey << std::endl;
	//bool validationResult = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 10, registrationName);
	//std::cout << "validationResult:" << validationResult << std::endl;

	return 0;

}

int main()
{
	//TestValidateExample();
	//std::cout << "_MSC_VER: " << _MSC_VER << std::endl;
	//std::cout << "Start tesing..." << std::endl;
	//RemoveTrialLicenseKey();
	getSystemInformation();
	//ValidateExample();
	//GenerateOfflineActivationData();
	//TestLocalTemplate();
	//GetLicenseDataExample();
	//GenerateKey();
	//ANSFinalTemplate_ActivationExample();
	//InstallOfflineActivationKey();
	//ActivateOfflineLicenseCHWID();
	//ActivateOfflineLicense();	
	// Utility
	//ANSCENTER::EngineType engineType = ANSCENTER::ANSLicenseHelper::CheckHardwareInformation();
	//std::cout << "Engine Type:" << engineType << std::endl;
	//ExtractModelZipFileTest();
	//PrepareEgdeModel();
	std::cin.get();
	//ModelConversionTest();
	//getSystemInformation();
	//ExtractModelZipFileTest();
	std::cout << "End of program" << std::endl;
}

