//
//  StringiumOptions.swift
//  
//
//  Created by Thibaut Schmitt on 10/01/2022.
//

import ArgumentParser
import Foundation
import ToolCore

// swiftlint:disable no_grouping_extension

struct StringiumOptions: ParsableArguments {

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

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

    @Option(
        name: .customLong("output-path"),
        help: "Path where to find the .xcStrings file or the lproj folders.",
        transform: { $0.replaceTiltWithHomeDirectoryPath() }
    )
    fileprivate var outputPathRaw: String

    @Option(
        name: .customLong("langs"),
        help: "Langs to generate."
    )
    fileprivate var langsRaw: String

    @Option(help: "Default langs.")
    var defaultLang: String

    @Option(
        name: .customLong("tags"),
        help: "Tags to generate."
    )
    fileprivate var tagsRaw: String = "ios iosonly iosOnly notranslation"

    @Option(help: "Generate static properties. False by default")
    var staticMembers: Bool = false

    @Option(help: "Tell if it will generate xcStrings file or lproj file. True by default")
    var xcStrings: Bool = true

    @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: "Bundle where the asset are generated"
    )
    var assetBundle: AssetBundle = .main

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

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

    @Option(help: "Extension suffix: {extensionName}+{extensionSuffix}.swift")
    var extensionSuffix: String?
}

// MARK: - Private var getter

extension StringiumOptions {

    var stringsFileOutputPath: String {
        var outputPath = outputPathRaw
        if outputPath.last == "/" {
            outputPath = String(outputPath.dropLast())
        }
        return outputPath
    }

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

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

// MARK: - Computed var

extension StringiumOptions {

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

    var extensionFilePath: String? {
        if let extensionOutputPath, let extensionFileName {
            return "\(extensionOutputPath)/\(extensionFileName)"
        }
        return nil
    }

    var inputFilenameWithoutExt: String {
        URL(fileURLWithPath: inputFile)
            .deletingPathExtension()
            .lastPathComponent
    }

    var xcStringsFilePath: String {
        "\(stringsFileOutputPath)/\(inputFilenameWithoutExt).xcstrings"
    }

    var lprojPathFormat: String {
        "\(stringsFileOutputPath)/%@.lproj/\(inputFilenameWithoutExt).strings"
    }
}
