package io.delphiplatform.api.v3.view.util;

import org.springframework.web.context.request.NativeWebRequest;

import java.io.IOException;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import io.delphiplatform.api.util.CollectionUtils;
import io.delphiplatform.api.v3.model.video.YoutubeContentType;

public class ApiUtil {

    public static void setExampleResponse(NativeWebRequest req, String contentType, String example) {
        try {
            HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class);
            res.setCharacterEncoding("UTF-8");
            res.addHeader("Content-Type", contentType);
            res.getWriter().print(example);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * ToDo: Remove method in DAE-1481 and use contentTypes param
     */
    public static Set<YoutubeContentType> getFilteredContentTypes(Set<YoutubeContentType> contentTypes) {
        Set<YoutubeContentType> allowedContentTypes = Set
            .of(YoutubeContentType.PARTNER_UPLOADED, YoutubeContentType.PREMIUM_UGC);
        if (CollectionUtils.isNotEmpty(contentTypes)) {
            contentTypes.removeIf(e -> !allowedContentTypes.contains(e));
        }
        if (CollectionUtils.isEmpty(contentTypes)) {
            return allowedContentTypes;
        }
        return contentTypes;
    }

}
