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

import CoreVideo
import Foundation
import ToolCore

enum TagsGenerator {

    static func writeExtensionFiles(
        sections: [Section],
        lang: String,
        tags: [String],
        isStatic: Bool,
        extensionName: String,
        extensionFilePath: String,
        visibility: ExtensionVisibility
    ) {
        // Get extension content
        let extensionFileContent = Self.getExtensionContent(
            sections: sections,
            lang: lang,
            tags: tags,
            isStatic: isStatic,
            extensionName: extensionName,
            visibility: visibility
        )

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

    // MARK: - Extension content

    static func getExtensionContent(
        sections: [Section],
        lang: String,
        tags: [String],
        isStatic: Bool,
        extensionName: String,
        visibility: ExtensionVisibility
    ) -> String {
        [
            Self.getHeader(
                extensionClassname: extensionName,
                isStatic: isStatic
            ),
            Self.getProperties(
                sections: sections,
                lang: lang,
                tags: tags,
                isStatic: isStatic,
                visibility: visibility
            ),
            Self.getFooter()
        ]
            .joined(separator: "\n")
    }

    // MARK: - Extension part

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

        \(isStatic ? "typelias Tags = String\n\n" : "")import UIKit

        extension \(extensionClassname) {
        """
    }

    private static func getProperties(
        sections: [Section],
        lang: String,
        tags: [String],
        isStatic: Bool,
        visibility: ExtensionVisibility
    ) -> String {
        sections
            .compactMap { section in
                // Check that at least one string will be generated
                guard section.hasOneOrMoreMatchingTags(tags: tags) else {
                    return nil // Go to next section
                }

                var res = "\n    // MARK: - \(section.name)"
                section.definitions.forEach { definition in
                    guard definition.hasOneOrMoreMatchingTags(inputTags: tags) == true else {
                        return // Go to next definition
                    }

                    let property = definition.getProperty(
                        forLang: lang,
                        visibility: visibility,
                        isStatic: isStatic
                    )
                    res += "\n\n\(property)"
                }
                return res
            }
            .joined(separator: "\n")
    }

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

        """
    }
}
