//
//  FontsOptions.swift
//  
//
//  Created by Thibaut Schmitt on 17/01/2022.
//

import ArgumentParser
import Foundation
import ToolCore

// swiftlint:disable no_grouping_extension

struct FontsOptions: ParsableArguments {

    @Flag(name: [.customShort("f"), .customShort("F")], help: "Should force generation")
    var forceGeneration = false

    @Argument(help: "Input files where fonts ared defined.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
    var inputFile: String

    @Option(help: "Tell if it will generate static properties or methods")
    var staticMembers: Bool = false

    @Option(
        name: .customLong("visibility"),
        help: "Visibility of extension and properties. Possibles values: public, private, package, internal. Default is internal",
        completion: .list(["public", "private", "package", "internal"])
    )
    var extensionVisibility: ExtensionVisibility = .internal

    @Option(help: "Path where to generate the extension.", transform: { $0.replaceTiltWithHomeDirectoryPath() })
    var extensionOutputPath: String

    @Option(help: "Extension name. If not specified, it will generate an Font extension.")
    var extensionName: String = Fonts.defaultExtensionName

    @Option(help: "Extension name. If not specified, no extension will be generated.")
    var extensionNameUIKit: String?

    @Option(help: "Extension suffix. Ex: MyApp, it will generate {extensionName}+FontsMyApp.swift")
    var extensionSuffix: String?

    @Option(name: .customLong("info-plist-paths"), help: "Info.plist paths (array). Will be used to update UIAppFonts content")
    fileprivate var infoPlistPathsRaw: String = ""
}

// MARK: - Computed var

extension FontsOptions {

    // MARK: - SwiftUI

    var extensionFileName: String {
        if let extensionSuffix {
            return "\(extensionName)+\(extensionSuffix).swift"
        }
        return "\(extensionName).swift"
    }

    var extensionFilePath: String {
        "\(extensionOutputPath)/\(extensionFileName)"
    }

    // MARK: - UIKit

    var extensionFileNameUIKit: String? {
        guard let extensionNameUIKit else { return nil }

        if let extensionSuffix {
            return "\(extensionNameUIKit)+\(extensionSuffix).swift"
        }
        return "\(extensionNameUIKit).swift"
    }

    var extensionFilePathUIKit: String? {
        guard let extensionFileNameUIKit else { return nil }

        return "\(extensionOutputPath)/\(extensionFileNameUIKit)"
    }

    // MARK: - 

    var infoPlistPaths: [String] {
        infoPlistPathsRaw
            .split(separator: " ")
            .map { String($0) }
    }
}
