//
//  FontToolContentGenerator.swift
//  
//
//  Created by Thibaut Schmitt on 13/12/2021.
//

import Foundation
import ToolCore

enum FontExtensionGenerator {

    private static func getFontNameEnum(
        fontsNames: [FontName],
        visibility: ExtensionVisibility
    ) -> String {
        var enumDefinition = "    \(visibility) enum FontName: String {\n"

        fontsNames.forEach {
            enumDefinition += "        case \($0.fontNameSanitize) = \"\($0.postscriptName)\"\n"
        }
        enumDefinition += "    }\n"

        return enumDefinition
    }

    static func writeExtensionFile(
        fontsNames: [FontName],
        isStatic: Bool,
        extensionName: String,
        extensionFilePath: String,
        isSwiftUI: Bool,
        visibility: ExtensionVisibility
    ) {
        // Create extension content
        let extensionContent = Self.getExtensionContent(
            fontsNames: fontsNames,
            isStatic: isStatic,
            extensionName: extensionName,
            isSwiftUI: isSwiftUI,
            visibility: visibility
        )

        // Write content
        let extensionFilePathURL = URL(fileURLWithPath: extensionFilePath)
        do {
            try extensionContent.write(to: extensionFilePathURL, atomically: false, encoding: .utf8)
        } catch {
            let error = FontsToolError.writeExtension(extensionFilePath, error.localizedDescription)
            print(error.description)
            Fonts.exit(withError: error)
        }
    }

    static func getExtensionContent(
        fontsNames: [FontName],
        isStatic: Bool,
        extensionName: String,
        isSwiftUI: Bool,
        visibility: ExtensionVisibility
    ) -> String {
        [
            Self.getHeader(
                extensionClassname: extensionName,
                isSwiftUI: isSwiftUI
            ),
            Self.getFontNameEnum(
                fontsNames: fontsNames,
                visibility: visibility
            ),
            Self.getFontMethods(
                fontsNames: fontsNames,
                isStatic: isStatic,
                isSwiftUI: isSwiftUI,
                visibility: visibility
            ),
            Self.getFooter()
        ]
            .joined(separator: "\n")
    }

    private static func getHeader(
        extensionClassname: String,
        isSwiftUI: Bool
    ) -> String {
        """
        // Generated by ResgenSwift.\(Fonts.toolName) \(ResgenSwiftVersion)

        import \(isSwiftUI ? "SwiftUI" : "UIKit")

        extension \(extensionClassname) {\n
        """
    }

    private static func getFontMethods(
        fontsNames: [FontName],
        isStatic: Bool,
        isSwiftUI: Bool,
        visibility: ExtensionVisibility
    ) -> String {
        let pragma = "    // MARK: - Getter"

        var propertiesOrMethods: [String] = fontsNames
            .unique()
            .map {
                $0.getProperty(
                    isStatic: isStatic,
                    isSwiftUI: isSwiftUI,
                    visibility: visibility
                )
            }

        propertiesOrMethods.insert(pragma, at: 0)
        return propertiesOrMethods.joined(separator: "\n\n")
    }

    private static func getFooter() -> String {
        """
        }

        """
    }
}
