package common

import (
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/utils/elptr"
)

func TestRemoveParsedS19HexFiles(t *testing.T) {
	var exampleECU UpdateManifestECU = UpdateManifestECU{
		Files: []*UpdateManifestFile{
			{
				Parsed: nil,
			},
			{
				Parsed:elptr.ElPtr(false),
			},
			{
				Parsed:elptr.ElPtr(true),
			},
			{
				Parsed: nil,
			},
			{
				Parsed:elptr.ElPtr(true),
			},
			{
				Parsed:elptr.ElPtr(false),
			},
		},
	}
	exampleECU.RemoveParsedS19HexFiles()
	nonS19Count := 0
	parsedS19Count := 0
	originalS19Count := 0

	for _, y := range exampleECU.Files{
		if y.Parsed == nil{
			nonS19Count += 1
		}else{
			if *y.Parsed{
				parsedS19Count += 1
			}else{
				originalS19Count += 1
			}
		}
	}

	if nonS19Count != 2 {
		t.Logf("Expected %d nons19, got %d\n", 2, nonS19Count)
		t.Fail()
	}
	if parsedS19Count != 0 {
		t.Logf("Expected %d parsed s19, got %d\n", 0, parsedS19Count)
		t.Fail()
	}
	if originalS19Count != 2 {
		t.Logf("Expected %d original s19, got %d\n", 2, originalS19Count)
		t.Fail()
	}
}

func TestRemoveOriginalS19HexFiles(t *testing.T) {
	var exampleECU UpdateManifestECU = UpdateManifestECU{
		Files: []*UpdateManifestFile{
			{
				Parsed: nil,
			},
			{
				Parsed:elptr.ElPtr(false),
			},
			{
				Parsed:elptr.ElPtr(true),
			},
			{
				Parsed: nil,
			},
			{
				Parsed:elptr.ElPtr(true),
			},
			{
				Parsed:elptr.ElPtr(false),
			},
		},
	}

	exampleECU.RemoveOriginalS19HexFiles()
	nonS19Count := 0
	parsedS19Count := 0
	originalS19Count := 0

	for _, y := range exampleECU.Files{
		if y.Parsed == nil{
			nonS19Count += 1
		}else{
			if *y.Parsed{
				parsedS19Count += 1
			}else{
				originalS19Count += 1
			}
		}
	}

	if nonS19Count != 2 {
		t.Logf("Expected %d nons19, got %d\n", 2, nonS19Count)
		t.Fail()
	}
	if parsedS19Count != 2 {
		t.Logf("Expected %d parsed s19, got %d\n", 2, parsedS19Count)
		t.Fail()
	}
	if originalS19Count != 0 {
		t.Logf("Expected %d original s19, got %d\n", 0, originalS19Count)
		t.Fail()
	}

}
