package com.iqser.red.service.persistence.management.v1.processor.acl;

import java.util.List;
import java.util.Optional;

import org.springframework.security.acls.domain.AbstractPermission;
import org.springframework.security.acls.model.Permission;

import lombok.Getter;

public class RedPermission extends AbstractPermission implements Permission {

    public static final RedPermission VIEW_OBJECT = new RedPermission("VIEW_OBJECT", 1 << 0, 'V', 100, false); // 1

    public static final RedPermission ACCESS_OBJECT = new RedPermission("ACCESS_OBJECT", 1 << 1, 'A', 200, false); // 2

    public static final RedPermission REVIEW = new RedPermission("REVIEW", 1 << 2, 'W', 30, true); // 4

    public static final RedPermission APPROVE = new RedPermission("APPROVE", 1 << 3, 'A', 20, true); // 8

    public static final RedPermission OWNER = new RedPermission("OWNER", 1 << 4, 'O', 10, false); // 16

    private static final List<RedPermission> ALL_PERMISSIONS = List.of(VIEW_OBJECT, ACCESS_OBJECT, REVIEW, APPROVE, OWNER);

    @Getter
    private final String name;

    @Getter
    private final int sort;

    @Getter
    private final boolean isChangeable;


    protected RedPermission(String name, int mask, char code, int sort, boolean isChangeable) {

        super(mask, code);
        this.name = name;
        this.sort = sort;
        this.isChangeable = isChangeable;
    }


    public static Optional<RedPermission> resolvePermission(Integer mask) {

        if (mask == null) {
            return Optional.empty();
        }
        return ALL_PERMISSIONS.stream().filter(p -> p.getMask() == mask).findAny();
    }

}
