import {axios} from "@/requests/axios.config";

const prefix = "/courseWare";

function createCourseWare(data) {
    return axios.post(`${prefix}/create`, data);
}

function setCanDownloadById(id, canDownload) {
    return axios.post(`${prefix}/setCanDownloadById`, null, {params: {id, canDownload}});
}

function uploadCourseWareFile(name, file) {
    let data = new FormData();
    data.append("name", name);
    data.append("file", file);
    return axios.post(`${prefix}/uploadCourseWareFile`, data);
}

function deleteCourseWare(id) {
    return axios.delete(`${prefix}/delete`, {params: {id}});
}

function getCourseWaresByCourseId(courseId) {
    return axios.get(`${prefix}/getCourseWaresByCourseId`, {params: {courseId}});
}

function getCourseWareById(id) {
    return axios.get(`${prefix}/getById`, {params: {id}});
}

function checkCourseWareNameUnique(courseId, name) {
    return axios.get(`${prefix}/checkName`, {params: {courseId, name}});
}

function changeCourseWareName(id, newName) {
    return axios.post(`${prefix}/changeCourseWareName`, null, {
        params: {
            id,
            newName,
        }
    });
}

function getCourseWareUrlById(id) {
    return axios.get(`${prefix}/getUrlById`, {params: {id}});
}

function getCourseIdById(id) {
    return axios.get(`${prefix}/getCourseIdById`, {params: {id}});
}

function previewpdf(fileUrl) {
    return axios({
        methods: "get",
        url: "/courseWare/readPdfFile",
        params: {
            fileUrl: fileUrl
        },
        responseType: "blob",
        headers:{
            "Content-Type": "application/json"
        }
    });
}

function getCanDownloadById(id) {
    return axios.get(`${prefix}/getCanDownloadById`, {params: {id}});
}

const courseWareAPIs = {
    createCourseWare,
    setCanDownloadById,
    deleteCourseWare,
    getCourseWaresByCourseId,
    getCourseWareById,
    uploadCourseWareFile,
    checkCourseWareNameUnique,
    changeCourseWareName,
    getCourseWareUrlById,
    previewpdf,
    getCourseIdById,
    getCanDownloadById
};

export default courseWareAPIs;
