//
//  VDS-TextStyle.swift
//  MVMCoreUI
//
//  Created by Matt Bruce on 1/25/23.
//  Copyright © 2023 Verizon Wireless. All rights reserved.
//

import Foundation
import VDS

extension Styler.Font {
    //Converts Legacy Font to a VDS.TextStyle
    public func vdsTextStyle() -> VDS.TextStyle? {
        
        //ensure that the current Styler.Font isn't Legacy Version
        //if it is, here is the conversion to the updated version
        var actualFont: Styler.Font = self
        switch self {
        case .Title2XLarge: actualFont = .RegularTitle2XLarge
        case .TitleXLarge: actualFont = .RegularTitleXLarge
        case .H1: actualFont = .RegularTitle2XLarge
        case .H32: actualFont = .RegularTitleXLarge
        case .H2: actualFont = .RegularTitleXLarge
        case .B20: actualFont = .RegularBodyLarge
        case .H3: actualFont = .BoldTitleMedium
        case .B1: actualFont = .RegularTitleSmall
        case .B2: actualFont = .RegularBodySmall
        case .B3: actualFont = .RegularMicro
        default: break
        }
       
        let updatedRaw = actualFont.rawValue.replacingOccurrences(of: "Regular", with: "")
        let newRaw = updatedRaw.prefix(1).lowercased() + updatedRaw.dropFirst()
        guard let style = VDS.TextStyle(rawValue: newRaw) else { return nil }
        return style
    }
    
    public func vdsSubsetStyle<T: EnumSubset>() throws -> T {
        guard let style = vdsTextStyle(), let rawValue = style.toStandardStyle().rawValue as? T.RawValue, let standardStyle = T(rawValue: rawValue) else {
            let err = "\(rawValue) was not found in the \(T.self), only these cases exist:\r\(T.allCases)"
            throw MVMCoreError.errorObject(MVMCoreErrorObject(title: "\(T.self) conversion Issue",
                                                              messageToLog: err,
                                                              code: 999,
                                                              domain: ErrorDomainNative,
                                                              location: #file)!)
        }
        return standardStyle
    }
}

extension VDS.Font {
    internal static func from(fontName: String) -> Self? {
        Self.allCases.filter({$0.fontName == fontName }).first
    }
}
