/*
 * Copyright (C) 2019 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
 * This application is licensed under GPLv2. See the LICENSE file in the root directory of the project.
 */

package piwigo

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"os"
)

type PiwigoCategory struct {
	Id       int
	ParentId int
	Name     string
	Key      string
}

func buildLookupMap(categories map[int]*PiwigoCategory) map[string]*PiwigoCategory {
	categoryLookups := map[string]*PiwigoCategory{}
	for _, category := range categories {
		logrus.Debugf("Loaded existing category %s", category.Key)
		categoryLookups[category.Key] = category
	}
	return categoryLookups
}

func buildCategoryMap(statusResponse *getCategoryListResponse) map[int]*PiwigoCategory {
	categories := map[int]*PiwigoCategory{}
	for _, category := range statusResponse.Result.Categories {
		categories[category.ID] = &PiwigoCategory{Id: category.ID, ParentId: category.IDUppercat, Name: category.Name, Key: category.Name}
	}
	return categories
}

func buildCategoryKeys(categories map[int]*PiwigoCategory) {
	for _, category := range categories {
		if category.ParentId == 0 {
			category.Key = category.Name
			continue
		}

		key := category.Name
		parentId := category.ParentId
		for parentId != 0 {
			parent := categories[parentId]
			// as we build the category as a directory hierarchy,
			// we have to use the path separator to construct the path key
			key = fmt.Sprintf("%s%c%s", parent.Name, os.PathSeparator, key)
			parentId = parent.ParentId
		}

		category.Key = key
	}
}
