const fs = require("fs");
const path = require("path");

class MediaService {

    // ============================================
    // Create Directory if Not Exists
    // ============================================

    static ensureDirectory(directory) {

        if (!fs.existsSync(directory)) {
            fs.mkdirSync(directory, {
                recursive: true
            });
        }

    }

    // ============================================
    // Student Root Folder
    // media/students/{studentId}
    // ============================================

    static getStudentFolder(studentId) {

        const folder = path.join(
            __dirname,
            "..",
            "media",
            "students",
            studentId.toString()
        );

        this.ensureDirectory(folder);

        return folder;

    }

    // ============================================
    // Profile Folder
    // ============================================

    static getProfileFolder(studentId) {

        const folder = path.join(
            this.getStudentFolder(studentId),
            "profile"
        );

        this.ensureDirectory(folder);

        return folder;

    }

    // ============================================
    // Faces Folder
    // ============================================

    static getFacesFolder(studentId) {

        const folder = path.join(
            this.getStudentFolder(studentId),
            "faces"
        );

        this.ensureDirectory(folder);

        return folder;

    }

    // ============================================
    // Angle Folder
    // front / left / right / up / down
    // ============================================

    static getFaceAngleFolder(studentId, angle) {

        const folder = path.join(
            this.getFacesFolder(studentId),
            angle
        );

        this.ensureDirectory(folder);

        return folder;

    }

    // ============================================
    // Embedding Folder
    // ============================================

    static getEmbeddingFolder(studentId) {

        const folder = path.join(
            this.getStudentFolder(studentId),
            "embeddings"
        );

        this.ensureDirectory(folder);

        return folder;

    }

    // ============================================
    // Attendance Images
    // media/attendance/YYYY/MM/DD
    // ============================================

    static getAttendanceFolder(date = new Date()) {

        const year = date.getFullYear();

        const month = String(
            date.getMonth() + 1
        ).padStart(2, "0");

        const day = String(
            date.getDate()
        ).padStart(2, "0");

        const folder = path.join(

            __dirname,

            "..",

            "media",

            "attendance",

            year.toString(),

            month,

            day

        );

        this.ensureDirectory(folder);

        return folder;

    }

    // ============================================
    // Unknown Faces
    // ============================================

    static getUnknownFolder() {

        const folder = path.join(

            __dirname,

            "..",

            "media",

            "unknown"

        );

        this.ensureDirectory(folder);

        return folder;

    }

    // ============================================
    // Temporary Folder
    // ============================================

    static getTempFolder() {

        const folder = path.join(

            __dirname,

            "..",

            "media",

            "temp"

        );

        this.ensureDirectory(folder);

        return folder;

    }

    // ============================================
    // Move File
    // ============================================

    static moveFile(oldPath, newPath) {

        fs.renameSync(oldPath, newPath);

    }

    // ============================================
    // Delete File
    // ============================================

    static deleteFile(filePath) {

        if (fs.existsSync(filePath)) {

            fs.unlinkSync(filePath);

        }

    }

    // ============================================
    // File Exists
    // ============================================

    static exists(filePath) {

        return fs.existsSync(filePath);

    }

}

module.exports = MediaService;