//
//  UIImage+Extensions.swift
//  WoWonderiOS
//
//  Created by iMac on 06/09/24.
//  Copyright © 2024 ScriptSun. All rights reserved.
//

import Foundation
import UIKit

enum FontAwesomeFontType {
    case regular5, solid5, brands6, duotone6, light6Pro, regular6Pro, solid6Pro, thin6Pro, compatibility4, classic
}

extension UIImage {
    
    static func fromFontAwesome(iconUnicode: String, fontType: FontAwesomeFontType, size: CGFloat = 24, color: UIColor = .accent) -> UIImage? {
        // Define font name based on the font type
        let fontName: String
        switch fontType {
        case .regular5:
            fontName = "FontAwesome5Free-Regular"
        case .solid5:
            fontName = "FontAwesome5Free-Solid"
        case .brands6:
            fontName = "FontAwesome6Brands-Regular"
        case .duotone6:
            fontName = "FontAwesome6Duotone-Solid"
        case .light6Pro:
            fontName = "FontAwesome6Pro-Light"
        case .regular6Pro:
            fontName = "FontAwesome6Pro-Regular"
        case .solid6Pro:
            fontName = "FontAwesome6Pro-Solid"
        case .thin6Pro:
            fontName = "FontAwesome6Pro-Thin"
        case .compatibility4:
            fontName = "FontAwesomev4Compatibility-Regular"
        case .classic:
            fontName = "FontAwesome"
        }
        
        // Create a UIFont with the provided font name and size
        guard let font = UIFont(name: fontName, size: size) else {
            print("Failed to load the font \(fontName)")
            return nil
        }
        
        // Create an attributed string with the font and color
        let attributes: [NSAttributedString.Key: Any] = [
            .font: font,
            .foregroundColor: color
        ]
        
        let attributedString = NSAttributedString(string: iconUnicode, attributes: attributes)
        let imageSize = attributedString.size()
        
        // Create an image context
        UIGraphicsBeginImageContextWithOptions(imageSize, false, 0.0)
        
        // Draw the attributed string into the context
        attributedString.draw(at: .zero)
        
        // Capture the image from the context
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        // Clean up the context
        UIGraphicsEndImageContext()
        return image
    }
    
}
