package com.takensoft.common.util;

import org.springframework.stereotype.Component;


/**
 * @author 하석형
 * @since 2025.03.13
 * @modification
 *     since    |    author    | description
 *  2025.03.13  |    하석형     | 최초 등록
 *
 * 파일 관련 RestController
 */
@Component
public class FileUtil {

    /**
     * @param sizeStr - 파일사이즈(문자열)
     * @return long - 파일사이즈(바이트)
     *
     * 단위가 포함된 파일사이즈(문자열)를 바이트로 변환
     */
    public static long sizeStrToBytes(String sizeStr) {
        if (sizeStr == null || sizeStr.isEmpty()) {
            throw new IllegalArgumentException("입력된 파일 크기 값이 올바르지 않습니다.");
        }

        // 숫자 부분과 단위 부분 분리
        sizeStr = sizeStr.toUpperCase().trim(); // 대문자로 변환 후 공백 제거
        String numberPart = sizeStr.replaceAll("[^0-9.]", ""); // 숫자만 추출 (숫자 및 '.'를 제외한 문자 제거)
        String unitPart = sizeStr.replaceAll("[0-9.]", "").trim(); // 단위만 추출 (숫자 및 '.' 제거)

        // 숫자 값을 double로 변환
        double sizeValue = Double.parseDouble(numberPart);

        // 단위에 따라 바이트 변환
        switch (unitPart) {
            case "B":
                return (long) sizeValue; // 바이트 그대로
            case "KB":
                return (long) (sizeValue * 1024);
            case "MB":
                return (long) (sizeValue * 1024 * 1024);
            case "GB":
                return (long) (sizeValue * 1024 * 1024 * 1024);
            case "TB":
                return (long) (sizeValue * 1024 * 1024 * 1024 * 1024);
            case "PB":
                return (long) (sizeValue * 1024 * 1024 * 1024 * 1024 * 1024);
            default:
                throw new IllegalArgumentException("지원하지 않는 파일 크기 단위: " + unitPart);
        }
    }
}
