//
//  ImageExtensionGenerator.swift
//  
//
//  Created by Thibaut Schmitt on 14/02/2022.
//

import Foundation
import ToolCore

enum ImageExtensionGenerator {

    // MARK: - UIKit

    static func generateExtensionFile(
        images: [ParsedImage],
        isStatic: Bool,
        inputFilename: String,
        extensionName: String,
        extensionFilePath: String,
        isSwiftUI: Bool,
        visibility: ExtensionVisibility,
        assetBundle: AssetBundle
    ) {
        // Create extension conten1t
        let extensionContent = Self.getExtensionContent(
            images: images,
            isStatic: isStatic,
            extensionName: extensionName,
            inputFilename: inputFilename,
            isSwiftUI: isSwiftUI,
            visibility: visibility,
            assetBundle: assetBundle
        )

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

    static func getExtensionContent(
        images: [ParsedImage],
        isStatic: Bool,
        extensionName: String,
        inputFilename: String,
        isSwiftUI: Bool,
        visibility: ExtensionVisibility,
        assetBundle: AssetBundle
    ) -> String {
        [
            Self.getHeader(
                inputFilename: inputFilename,
                extensionClassname: extensionName,
                isSwiftUI: isSwiftUI
            ),
            Self.getProperties(
                images: images,
                isStatic: isStatic,
                isSwiftUI: isSwiftUI,
                visibility: visibility,
                assetBundle: assetBundle
            ),
            Self.getFooter()
        ]
            .joined(separator: "\n")
    }

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

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

        extension \(extensionClassname) {
        """
    }

    private static func getProperties(
        images: [ParsedImage],
        isStatic: Bool,
        isSwiftUI: Bool,
        visibility: ExtensionVisibility,
        assetBundle: AssetBundle
    ) -> String {
        images
            .map { parsedImage in
                let property = parsedImage.getImageProperty(
                    isStatic: isStatic,
                    isSwiftUI: isSwiftUI,
                    visibility: visibility,
                    assetBundle: assetBundle
                )
                return "\n\(property)"
            }
            .joined(separator: "\n")
    }

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

        """
    }
}
