package cn.reghao.tnb.content.app.vod.service;

import cn.reghao.tnb.content.api.constant.PostScope;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.user.api.iface.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

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

/**
 * @author reghao
 * @date 2023-09-18 09:19:53
 */
@Service
public class ContentPermission {
    @DubboReference(check = false)
    private UserService userService;

    public List<Integer> getUserScopes() {
        long userId = UserContext.getUserId();
        return getUserScopes(userId);
    }

    public List<Integer> getUserScopes(long loginUser) {
        List<Integer> scopes = new ArrayList<>(List.of(PostScope.PUBLIC.getCode()));
        if (userService.isVip(loginUser)) {
            scopes.add(PostScope.PROTECT.getCode());
        }

        return scopes;
    }

    public boolean hasPermission(long owner, int scope, long loginUser) {
        if (loginUser == owner || scope == PostScope.PUBLIC.getCode()) {
            return true;
        }

        boolean isVip = userService.isVip(loginUser);
        boolean isFriend = false;
        if (scope == PostScope.PROTECT.getCode()) {
            return isVip;
        } else if (scope == PostScope.FRIEND.getCode()) {
            return isFriend;
        }

        return false;
    }
}
