package de.thpeetz.kontor.admin.services;

import java.util.Collection;
import java.util.List;

import de.thpeetz.kontor.admin.data.*;
import de.thpeetz.kontor.admin.repository.*;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class AdminService {


    private final ProfileRepository profileRepository;
    private final PermissionRepository permissionRepository;
    private final AssignmentRepository assignmentRepository;

    public AdminService(ProfileRepository profileRepository,
            PermissionRepository permissionRepository,
            AssignmentRepository assignmentRepository) {
        this.profileRepository = profileRepository;
        this.permissionRepository = permissionRepository;
        this.assignmentRepository = assignmentRepository;
    }

    public List<Profile> findAllProfiles() { return profileRepository.findAll(); }

    public Profile getProfile(String userName) {
        log.debug("get Profile {}", userName);
        return profileRepository.findByUserName(userName);
    }

    public List<Permission> findAllPermissions() { return permissionRepository.findAll(); }

    public Collection<Permission> findAllPermissions(String stringFilter) {
        if (stringFilter == null || stringFilter.isEmpty()) {
            return permissionRepository.findAll();
        } else {
            return permissionRepository.search(stringFilter);
        }
    }

    public Permission addPermission(String permissionName) {
        Permission permission = permissionRepository.findByName(permissionName);
        if (permission == null) {
            log.info("Permission {} was not found, will create it.", permissionName);
            permission = new Permission();
            permission.setName(permissionName);
            permissionRepository.save(permission);
        }
        return permission;
    }

    public void savePermission(Permission permission) {
        if (permission == null) {
            log.warn("Permission is null. Can't save it.");
        }
        log.info("savePermission: permission={}", permission);
        permissionRepository.save(permission);
    }

    public void deletePermission(Permission permission) {
        if (permission == null) {
            log.warn("Permission is null. Can't delete it.");
            return;
        }
        log.info("deletePermission: permission={}", permission);
        permissionRepository.delete(permission);
    }

    public List<Assignment> findAllAssignments() {
        return assignmentRepository.findAll();
    }

    public void saveAssignment(Assignment assignment) {
        if (assignment == null) {
            log.warn("Assignment is null. Can't save it.");
            return;
        }
        log.info("saveAssignment: assignment={}", assignment);
        assignmentRepository.save(assignment);
    }

    public void deleteAssignment(Assignment assignment) {
        if (assignment == null) {
            log.warn("Assignment is null. Can't delete it.");
            return;
        }
        log.info("deleteAssignment: assignment={}", assignment);
        assignmentRepository.delete(assignment);
    }

    public String getProfileFullName(String userName) {
        log.debug("get Fullname für Profile {}", userName);
        Profile profile = profileRepository.findByUserName(userName);
        if (profile == null) {
            log.info("keinen Eintrag für {} gefunden", userName);
            return userName;
        } else {
            log.info("Voller Name des Profile {}: {}", userName, profile.getFullName());
            return profile.getFullName();
        }
    }
}
