//
//  ImagiumOptions.swift
//
//
//  Created by Thibaut Schmitt on 24/01/2022.
//

// CPD-OFF

import ArgumentParser
import Foundation
import ToolCore

// swiftlint:disable no_grouping_extension

struct ImagesOptions: ParsableArguments {

    @Flag(
        name: .customShort("f"),
        help: "Should force script execution"
    )
    var forceExecution = false

    @Flag(
        name: .customShort("F"),
        help: "Regenerate all images"
    )
    var forceExecutionAndGeneration = false

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

    @Option(
        help: "Xcassets path where to generate images.",
        transform: { $0.replaceTiltWithHomeDirectoryPath() }
    )
    var xcassetsPath: String

    @Option(
        help: "Tell if it will generate static properties or not",
        completion: .list(["true", "false"])
    )
    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: "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: "SwiftUI extension name. If not specified, no extension will be generated.")
    var extensionName: String?

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

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

// MARK: - Computed var

extension ImagesOptions {

    // MARK: - SwiftUI

    var extensionFileName: String? {
        guard let extensionName else { return nil }

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

    var extensionFilePath: String? {
        guard let extensionOutputPath, let extensionFileName else { return nil }

        return "\(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 extensionOutputPath, let extensionFileNameUIKit else { return nil }

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

    // MARK: -

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

// CPD-ON
