package com.lion.lionwebsite.Util;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;

import java.io.IOException;
import java.net.ServerSocket;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Data
public class CustomUtil {

    public static final double ONE_KB = 1024;

    public static final double ONE_MB = ONE_KB * ONE_KB;

    public static final double ONE_GB = ONE_KB * ONE_MB;

    public static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static ObjectMapper objectMapper = new ObjectMapper();

    public static String fileSizeToString(long fileSize){
        if (fileSize < ONE_KB) {
            return fileSize + "B";
        } else if (fileSize >= ONE_KB && fileSize < ONE_MB) {
            return  String.format("%.2f", (fileSize / ONE_KB)) + "KB";
        } else if (fileSize >= ONE_MB && fileSize < ONE_GB) {
            return String.format("%.2f", (fileSize / ONE_MB)) + "MB";
        } else{
            return String.format("%.2f", (fileSize / ONE_GB)) + "GB";
        }
    }

    public static long stringToFileSize(String fileSizeString){
        String preNum = "";
        String unit = "";

        Pattern patternWithDot = Pattern.compile("(\\d+.\\d+)(([A-Z]|[a-z])+)");
        Matcher matcherWithDot = patternWithDot.matcher(fileSizeString);
        if(matcherWithDot.find()){
            preNum = matcherWithDot.group(1);
            unit = matcherWithDot.group(2);
        }
        else {
            Pattern patternWithoutDot = Pattern.compile("(\\d+)(([A-Z]|[a-z])+)");
            Matcher matcherWithoutDot = patternWithoutDot.matcher(fileSizeString);
            if (matcherWithoutDot.find()) {
                preNum = matcherWithoutDot.group(1);
                unit = matcherWithoutDot.group(2);
            }
        }

        double num = Double.parseDouble(preNum);

        return switch (unit) {
            case "B" -> (long) num;
            case "KB", "KiB" -> (long) (ONE_KB * num);
            case "MB", "MiB" -> (long) (ONE_MB * num);
            case "GB", "GiB" -> (long) (ONE_GB * num);
            default -> 0;
        };
    }

    public static DateTimeFormatter dateTimeFormatter(){
        return dateTimeFormatter;
    }

    public static String now(){
        return dateTimeFormatter.format(LocalDateTime.now());
    }

    /**
     * 寻找一定数量的可用端口
     *
     * @return  可用端口的起始位置  -1为没有（几乎没有可能）
     */
    public static short _findIdlePort(){
        for(int i=20000; i<65535; i++){
            try(ServerSocket ignored = new ServerSocket(i)){
                ignored.close();
                return (short) i;
            }catch (IOException ignored) {
            }
        }
        return -1;
    }

    public static void fourZeroFour(HttpServletResponse response){
        try{
            response.sendError(404);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
