import Foundation
import UnstringifyFramework

// swiftlint:disable:next function_body_length
func template(keys: [String], formattedKeys: [FormatKey], richKeys: [String], richFormattedKeys: [FormatKey], templatePath: String? = nil) throws -> String {
    if let templatePath = templatePath {
        var templateContent = try readFile(at: templatePath)
        templateContent = replaceVariables(content: templateContent, keys: keys, formattedKeys: formattedKeys, richKeys: richKeys, richFormattedKeys: richFormattedKeys)
        return templateContent
    }

    var file: String =
        """
            // Generated by Unstringify.
            // DO NOT EDIT!

            import Foundation
            import Unstringified

            private final class _Unstringified {}

            extension Unstringified {
            \tpublic var localizableStringsTableName: String? {
            \t\treturn nil
            \t}

            \tpublic var localizableStringsBundle: Bundle? {
            \t\tlet _UnstringifiedBundle = Bundle(for: _Unstringified.self)
            \t\tguard _UnstringifiedBundle.bundleIdentifier != Bundle.main.bundleIdentifier else {
            \t\t\treturn Bundle.main
            \t\t}
            \t\tlet bundleURL = _UnstringifiedBundle.bundleURL
            \t\tlet bundleName = bundleURL.lastPathComponent
            \t\tlet resource = (bundleName as NSString).deletingPathExtension
            \t\tguard let path = _UnstringifiedBundle.path(forResource: resource, ofType: "bundle") else {
            \t\t\treturn nil
            \t\t}
            \t\treturn Bundle(path: path)
            \t}
            }

            public enum Text: String, Unstringified {
            \tpublic typealias StringType = String
            \tcase $KEYS_ARRAY
            }

            public enum Format: Unstringified {
            \tpublic typealias StringType = String
            \tcase $FORMATED_KEYS_ARRAY
            }

            public enum RichText: String, Unstringified {
            \tpublic typealias StringType = NSAttributedString
            \tcase $RICH_KEYS_ARRAY
            }

            public enum RichFormat: Unstringified {
            \tpublic typealias StringType = NSAttributedString
            \tcase $FORMATED_RICH_KEYS_ARRAY
            }

            """.replacingOccurrences(of: "\t", with: "    ")

    file = replaceVariables(content: file, keys: keys, formattedKeys: formattedKeys, richKeys: richKeys, richFormattedKeys: richFormattedKeys)

    return file
}

func replaceVariables(content: String, keys: [String], formattedKeys: [FormatKey], richKeys: [String], richFormattedKeys: [FormatKey]) -> String {
    var newContent = content
    newContent = newContent.replacingOccurrences(of: "$KEYS_ARRAY", with: keys.joined(separator: ", "))
    newContent = newContent.replacingOccurrences(of: "$FORMATED_KEYS_ARRAY", with: formattedKeys.map { $0.description }.joined(separator: ", "))
    newContent = newContent.replacingOccurrences(of: "$RICH_KEYS_ARRAY", with: richKeys.joined(separator: ", "))
    newContent = newContent.replacingOccurrences(of: "$FORMATED_RICH_KEYS_ARRAY", with: richFormattedKeys.map { $0.description }.joined(separator: ", "))
    return newContent
}
