//
//  FontInfo.swift
//
//  Created by linjie jiang on 2023/2/27.
//

import UIKit

@objc
public class FontInfo: NSObject, Codable {
    var fontName: String?
    var systemFontStyle: SystemFontStyle?
    var systemFontWeight: SystemFontWeight?

    public enum SystemFontStyle: String, Codable {
        case normal, italic
    }

    public enum SystemFontWeight: String, Codable {
        case ultraLight, thin, light, regular, medium, semibold, bold, heavy, black

        func getFontWeight() -> UIFont.Weight {
            switch self {
            case .ultraLight:
                return .ultraLight
            case .thin:
                return .thin
            case .light:
                return .light
            case .regular:
                return .regular
            case .medium:
                return .medium
            case .semibold:
                return .semibold
            case .bold:
                return .bold
            case .heavy:
                return .heavy
            case .black:
                return .black
            }
        }
    }

    public func getFont(_ fontSize: Double) -> UIFont {
        if let fontName = fontName, let font = UIFont(name: fontName, size: fontSize) {
            return font
        } else if let fontStyle = systemFontStyle, fontStyle == .italic {
            return UIFont.italicSystemFont(ofSize: fontSize)
        } else if let systemFontWeight = systemFontWeight?.getFontWeight() {
            return UIFont.systemFont(ofSize: fontSize, weight: systemFontWeight)
        } else {
            return UIFont.systemFont(ofSize: fontSize)
        }
    }
}
