/*
 * @Copyright (c) Huawei Technologies Co., Ltd. 2022-2024. All rights reserved.
 */

/**
 * @file
 * The file declars the FileUtils class.
 */
package mp4parser

import std.posix.*
import std.collection.*
import std.io.*
import std.console.Console
import stdx.logger.*

/**
 * The class is FileUtils
 * @author fiture
 * @since 0.32.5
 */
public class FileUtils {

    /**
     * The Function is init constructor
     *
     * @since 0.32.5
     */
    public init() {}
    /*
     * let member logger type is SimpleLogger
     * @since 0.32.5
     */
    let logger: SimpleLogger = SimpleLogger(BufferedOutputStream<OutputStream>(Console.stdOut))
    /**
     * The Function is isFilePathValid
     *
     * @param filePath of String
     *
     * @return Type of (String,String)
     * @since 0.32.5
     */
    public func isFilePathValid(filePath: String): (String,String) {

        let con: Option<Int64> = filePath.lastIndexOf("/")
        var lastSlash: Int64 = 0
        match (con) {
           case Some(v) => lastSlash = v
           case None => throw Exception("Invalid filepath")
        }
        let directoryName: String = filePath[0..lastSlash]
        let fileName: String = filePath[lastSlash + 1..]
        let confield: Option<Int64> = fileName.lastIndexOf(".")
        var len: Int64 = 0
        match (confield) {
           case Some(v) => len = v
           case None => throw Exception("Invalid filepath. Need to include filename")
        }
        let fields: String = fileName[len + 1..]
        logger.info("isFilePathValid lastSlash INDEX : "+"${lastSlash}"+" directoryName : "+"${directoryName}"+" fileName : "+"${fileName}")
        let isFile: Bool = isReg(fileName)
        let isDir: Bool = isDir(directoryName)
        if (!isFile) {
            logger.info("isFilePathValid isFile is  " + "${isFile}")
            throw Exception("${fileName}" + " not normal file")
        }
        if (!isDir) {
            logger.info("isFilePathValid isDir is  " + "${isDir}")
            throw Exception("isFilePathValid invalid directory")
        }
        logger.info("isFilePathValid filePath is valid")
        return (fileName,fields)
    }
}