// LoadInfo : Load the EntityManager data from the storage
// and constract/reconstract the EntityManager
func LoadInfo(filePath string, secret []byte, el *EntityManager) error {
	prefix := ""
	if el == nil {
		return fmt.Errorf("Internal error: Entity list is nil")
	}
	stStorage, err := ss.LoadInfo(filePath, secret)
	if err != nil {
		logger.Error.Printf("%v", err)
		return fmt.Errorf("%v", err)
	}
	storage := stStorage.GetDecryptStorageData()
	if storage == nil {
		return fmt.Errorf("loadInfo: Storage is nil")
	}
	for key, value := range storage.Data {
		userType := strings.HasPrefix(key, getEntityStoreFmt(userTypeStr+prefix, entityToken, ""))
		groupType := strings.HasPrefix(key, getEntityStoreFmt(groupTypeStr+prefix, entityToken, ""))
		resourceType := strings.HasPrefix(key, getEntityStoreFmt(resourceTypeStr+prefix, entityToken, ""))
		permissionType := strings.HasPrefix(key, getEntityStoreFmt(permissionTypeStr+prefix, "", ""))
		var err error
		var name string
		var permission Permission
		var e *Entity
		var g *Group
		if userType {
			e, err = readEntityFromStorage(key, storage)
			name = e.Name
			el.Users[name] = &User{Entity: *e}
		} else if groupType {
			g, err = readGroupFromStorage(key, storage)
			name = g.Name
			el.Groups[name] = g
		} else if resourceType {
			e, err = readEntityFromStorage(key, storage)
			name = e.Name
			el.Resources[name] = &(Resource{Entity: *e})
		} else if permissionType {
			permission, err = readPermissionFromStorage(key, storage)
		}
		if err != nil {
			return fmt.Errorf("Error while reading file: '%s', string: '%s', error: %s", filePath, value, err)
		}
		// fmt.Println("key:", key, "Value:", value, "error:", err)
		if userType || groupType || resourceType {
			for propertyName, property := range defs.Serializers {
				data, err := property.ReadFromStorage(getPropertyStoreFmt(propertyName, name), storage)
				if err == nil { // the item exist for this entity
					err = el.AddPropertyToEntity(name, propertyName, data)
					if err != nil {
						fmt.Println("while reading property data", propertyName, "for entity", name, "error:", err)
						return err
					}
				}
			}
		} else if permissionType {
			el.AddPermission(permission)
		}
	}
	return nil
}
func playWithSecureStorage(storage *ss.SecureStorage, secret []byte) {
	fileName := "try.txt"
	defer os.Remove(fileName)
	err := storage.StoreInfo(fileName)
	if err != nil {
		fmt.Println("Error while saving:", err)
	}

	fmt.Println("Original data:")
	fmt.Println(storage.GetDecryptStorageData())

	keyText := fmt.Sprintf(keyFmt, 1)
	err = storage.RemoveItem(keyText)
	if err != nil {
		fmt.Println("Error while remove key:", err)
	}

	fmt.Println("After removing:")
	fmt.Println(storage.GetDecryptStorageData())

	sd, err := ss.LoadInfo(fileName, secret)
	if err != nil {
		fmt.Println("Error while reading:", err)
	}
	fmt.Println("The data that was read from file:", fileName)
	fmt.Println(sd.GetDecryptStorageData())
}
// StoreLoadTest : common store load testing for all the different properties
func StoreLoadTest(t *testing.T, userData interface{}, propertyName string) {
	filePath := "./tmp.txt"
	key := "key"
	secret := []byte("12345678")
	logger.Init(ioutil.Discard, ioutil.Discard, ioutil.Discard, ioutil.Discard)

	storage, err := ss.NewStorage(secret, false)
	if err != nil {
		t.Errorf("Fatal error: can't create storage, error: %v", err)
	}
	s := Serializers[propertyName]
	err = s.AddToStorage(key, userData, storage)
	if err != nil {
		t.Fatal("Fatal error: can't add to storage, error:", err)
	}
	err = s.AddToStorage(key, nil, storage)
	if err == nil {
		t.Errorf("Test fail: Successfully add undefined property to storage")
	}
	err = s.AddToStorage(key, userData, nil)
	if err == nil {
		t.Errorf("Test fail: Successfully add  property to nil storage")
	}
	storage.StoreInfo(filePath)
	storage, err = ss.LoadInfo(filePath, secret)
	if err != nil {
		t.Fatal("Fatal error: can't load from storage, error:", err)
	}
	_, err = s.ReadFromStorage(key, nil)
	loadStorage := storage.GetDecryptStorageData()

	if err == nil {
		t.Fatal("Fatal error: Read pass but storage is nil")
	}
	_, err = s.ReadFromStorage("", loadStorage)
	if err == nil {
		t.Fatal("Fatal error: Read pass but the key is empty")
	}
	_, err = s.ReadFromStorage(key, loadStorage)
	if err != nil {
		t.Fatal("Fatal error: can't load from storage, error:", err)
	}
	data, err := s.ReadFromStorage(key, loadStorage)
	if err != nil {
		t.Fatal("Fatal error: can't read from storage, error:", err)
	}
	if s.IsEqualProperties(userData, data) == false {
		t.Fatal("Fatal error: Data read from storage:", s.PrintProperties(data), "is not equal to the one that was write to storage:", userData)
	}
	if s.IsEqualProperties(userData, "") == true {
		t.Fatal("Fatal error: unequal properies were found equal")
	}
	logger.Trace.Println("Data:", s.PrintProperties(data))
}