/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arrebol.taxiservicios.controller.system;

import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.core.Permission;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.enums.UserStatusType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import software.amazon.ion.impl.PrivateUtils;

/**
 *
 * @author Picasso
 */
public class UserHasPermissionControllerTest {

    private static final boolean PUBLIC_ACCESS = Boolean.TRUE;
    private static final boolean PRIVATE_ACCESS = Boolean.FALSE;
    private static final boolean ACTIVE = Boolean.TRUE;
    private static final boolean APP_MENU = Boolean.TRUE;
    private static final String USER_NAME_DIRECCION = "09ea84b8-b9d1-4a71-adfe-db653b2a9982";
    private static final String USER_NAME_TALLER = "53ec3b21-da17-4b95-a5e7-b77f095b43b4";
    private static final String USER_NAME_SUB_DIRECCION = "210c4b12-2550-4020-b88d-2ccf08a9c5dc";
    private static final String USER_NAME_ADMIN = "722314a5-78f3-11e9-85b3-022f6a7c1cb8";
    private static final boolean USER_HAS_PERMISSIONS = Boolean.TRUE;
    private static final List<UserStatusType> PARAM_IN_USER_STATUS_TYPE = new ArrayList<>();

    private UserHasPermissionController controller;

    public UserHasPermissionControllerTest() {
    }

    @Before
    public void setUp() {
        controller = new UserHasPermissionController();
        PARAM_IN_USER_STATUS_TYPE.add(UserStatusType.ENABLED);
        /*
        PARAM_IN_USER_STATUS_TYPE.add(UserStatusType.DISABLED);
        PARAM_IN_USER_STATUS_TYPE.add(UserStatusType.DELETED);
         */
    }

    //@Test
    public void getAllActivePrivatePermission() {
        try {
            List<Permission> permissions = controller.findActivePermissionByPublicAccess(PRIVATE_ACCESS);

            permissions.forEach(System.out::println);

            assertFalse(permissions.isEmpty());
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void getAllActivePublicPermission() {
        try {
            List<Permission> permissions = controller.findActivePermissionByPublicAccess(PUBLIC_ACCESS);

            permissions.forEach(System.out::println);

            assertFalse(permissions.isEmpty());
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void findActivePermissionByPublicAccess() {
        try {
            List<Permission> permissions = controller.findPermissionByUserId(USER_NAME_ADMIN, USER_HAS_PERMISSIONS);

            permissions.forEach(System.out::println);

            assertFalse(permissions.isEmpty());
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void findUsersNotRootByInStatus() {
        try {
            List<User> users = controller.findUsersNotRootByInStatus(USER_NAME_DIRECCION, PARAM_IN_USER_STATUS_TYPE, new Location());

            users.forEach(System.out::println);

            assertFalse(users.isEmpty());
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void changeUserStatusByStatus() {
        try {
            //assertTrue(controller.changeUserStatusByStatus(USER_NAME_TALLER, USER_NAME_DIRECCION, UserStatusType.ENABLED));
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void findApplicationMenuPermissions() {
        try {
            List<Permission> appMenuLst = controller.findApplicationMenuPermissions(PRIVATE_ACCESS, ACTIVE, APP_MENU);
            List<Permission> userPermissionLst = controller.findPermissionByUserId(USER_NAME_TALLER, USER_HAS_PERMISSIONS);

            Set<Permission> uniquePermission = new HashSet<>();

            userPermissionLst.forEach((uhp) -> {
                // load all new permission  from selected into a set(Unique list values)
                if (!uniquePermission.contains(uhp)) {
                    uniquePermission.add(uhp);
                }

                Optional<Permission> permission = appMenuLst.stream()
                        .filter((appMenu) -> appMenu.getName().equals(uhp.getParentName()))
                        .findAny();

                if (permission.isPresent()) {
                    if (!uniquePermission.contains(permission.get())) {
                        uniquePermission.add(permission.get());
                    }
                }
            });

            System.out.println("Total List<Permission> userPermissionLst " + userPermissionLst.size());
            //userPermissionLst.forEach(System.out::println);

            System.out.println("Total Set<Permission> uniquePermission " + uniquePermission.size());
            //uniquePermission.forEach(System.out::println);

            assertFalse(appMenuLst.isEmpty());
        } catch (Exception e) {
            assertFalse(Boolean.TRUE);
        }
    }

    private final Optional<Permission> findPermission(Collection<Permission> ps, String codeIsin) {
        // This stream will simply return any carnet that matches the filter.
        // It will be wrapped in a Optional object.
        // If no carnets are matched, an "Optional.empty" item will be returned
        return ps.stream().filter(p -> p.getName().equals(codeIsin)).findFirst();
    }
}
