
// imageFiles provides functions to retrieve image files
// The image files are embedded in the binary

package imageFiles_test

import "seekia/internal/imageFiles"

import "seekia/internal/helpers"
import "seekia/internal/imagery"
import "seekia/internal/cryptography/blake3"

import "testing"


func TestEmojis(t *testing.T){

	numberOfEmojis := 3631

	// We make sure no duplicate emoji identifiers exist in the categories
	foundIdentifiersMap := make(map[int]struct{})

	categoryNamesList := imageFiles.GetEmojiCategoriesList()

	for _, categoryName := range categoryNamesList{

		emojisList, err := imageFiles.GetEmojisInCategoryList(categoryName)
		if (err != nil) {
			t.Fatal("Failed to get emojis in category list: " + err.Error())
		}

		for _, emojiIdentifier := range emojisList{

			if (emojiIdentifier < 1 || emojiIdentifier > numberOfEmojis){
				emojiIdentifierString := helpers.ConvertIntToString(emojiIdentifier)
				t.Fatal("Invalid emoji identifier found in category " + categoryName + ": " + emojiIdentifierString)
			}

			_, exists := foundIdentifiersMap[emojiIdentifier]
			if (exists == true){

				emojiIdentifierString := helpers.ConvertIntToString(emojiIdentifier)
				t.Fatal("Duplicate emoji found in category " + categoryName + ": " + emojiIdentifierString)
			}
			
			foundIdentifiersMap[emojiIdentifier] = struct{}{}
		}
	}

	// We use this map to make sure no file duplicates exist
	fileHashesMap := make(map[string]struct{})

	for i:=1; i <= numberOfEmojis; i++{

		emojiFileBytes, err := imageFiles.GetEmojiFileBytesFromIdentifier(i)
		if (err != nil) {
			t.Fatal("Failed to get emoji from identifier: " + err.Error())	
		}

		fileHash, err := blake3.GetBlake3HashAsHexString(16, emojiFileBytes)
		if (err != nil){
			t.Fatal("Failed to hash emoji file bytes: " + err.Error())
		}

		_, exists := fileHashesMap[fileHash]
		if (exists == true){
			emojiIdentifierString := helpers.ConvertIntToString(i)
			t.Fatal("Duplicate emoji exists: " + emojiIdentifierString)
		}

		fileHashesMap[fileHash] = struct{}{}

		// We make sure each emoji exists within a category
		_, exists = foundIdentifiersMap[i]
		if (exists == false){
			emojiIdentifierString := helpers.ConvertIntToString(i)
			t.Fatal("Emoji is missing from categories list: " + emojiIdentifierString)
		}
	}

	// Now we make sure svg reader can read all svgs
	// This will take a while

	for i:=1; i <= numberOfEmojis; i++{
		
		emojiFileBytes, err := imageFiles.GetEmojiFileBytesFromIdentifier(i)
		if (err != nil) {
			t.Fatal("Failed to get emoji from identifier: " + err.Error())	
		}

		_, err = imagery.ConvertSVGImageFileBytesToGolangImage(emojiFileBytes)
		if (err != nil){
			emojiIdentifierString := helpers.ConvertIntToString(i)
			t.Fatal("Failed to convert emoji " + emojiIdentifierString + " to go image: " + err.Error())
		}
	}
}


