package com.agent.management.repository;

import com.agent.management.model.entity.TagGroup;
import com.agent.management.repository.json.AbstractJsonRepository;
import com.agent.management.repository.json.JsonStore;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Component
public class TagGroupRepository extends AbstractJsonRepository<TagGroup> {

    public TagGroupRepository(JsonStore store) {
        super(store, new TypeReference<>() {});
    }

    @Override
    protected String getFileName() {
        return "tag-groups.json";
    }

    @Override
    protected Long getId(TagGroup entity) {
        return entity.getId();
    }

    @Override
    protected void setId(TagGroup entity, Long id) {
        entity.setId(id);
    }

    public List<TagGroup> findAllByOrderBySortOrderAsc() {
        List<TagGroup> list = new ArrayList<>(findAll());
        list.sort(Comparator.comparingInt(g -> g.getSortOrder() == null ? Integer.MAX_VALUE : g.getSortOrder()));
        return list;
    }
}
