import Foundation
import SourceKittenFramework
import SwiftLintCore
import SwiftSyntax

struct CommaRule: CorrectableRule, SourceKitFreeRule {
    var configuration = SeverityConfiguration<Self>(.warning)

    static let description = RuleDescription(
        identifier: "comma",
        name: "Comma Spacing",
        description: "There should be no space before and one after any comma",
        kind: .style,
        nonTriggeringExamples: #examples([
            "func abc(a: String, b: String) { }",
            "abc(a: \"string\", b: \"string\"",
            "enum a { case a, b, c }",
            "func abc(\n  a: String,  // comment\n  bcd: String // comment\n) {\n}",
            "func abc(\n  a: String,\n  bcd: String\n) {\n}",
            "#imageLiteral(resourceName: \"foo,bar,baz\")",
            """
                kvcStringBuffer.advanced(by: rootKVCLength)
                  .storeBytes(of: 0x2E /* '.' */, as: CChar.self)
                """,
            """
                public indirect enum ExpectationMessage {
                  /// appends after an existing message ("<expectation> (use beNil() to match nils)")
                  case appends(ExpectationMessage, /* Appended Message */ String)
                }
                """.asExample(excludeFromDocumentation: true),
        ]),
        triggeringExamples: #examples([
            "func abc(a: String↓ ,b: String) { }",
            "func abc(a: String↓ ,b: String↓ ,c: String↓ ,d: String) { }",
            "abc(a: \"string\"↓,b: \"string\"",
            "enum a { case a↓ ,b }",
            "let result = plus(\n    first: 3↓ , // #683\n    second: 4\n)",
            """
            Foo(
              parameter: a.b.c,
              tag: a.d,
              value: a.identifier.flatMap { Int64($0) }↓ ,
              reason: Self.abcd()
            )
            """,
            """
            return Foo(bar: .baz, title: fuzz,
                      message: My.Custom.message↓ ,
                      another: parameter, doIt: true,
                      alignment: .center)
            """,
            #"Logger.logError("Hat is too large"↓,  info: [])"#,
        ]),
        corrections: #corrections([
            "func abc(a: String↓,b: String) {}": "func abc(a: String, b: String) {}",
            "abc(a: \"string\"↓,b: \"string\"": "abc(a: \"string\", b: \"string\"",
            "abc(a: \"string\"↓  ,  b: \"string\"": "abc(a: \"string\", b: \"string\"",
            "enum a { case a↓  ,b }": "enum a { case a, b }",
            "let a = [1↓,1]\nlet b = 1\nf(1, b)": "let a = [1, 1]\nlet b = 1\nf(1, b)",
            "let a = [1↓,1↓,1↓,1]": "let a = [1, 1, 1, 1]",
            """
            Foo(
              parameter: a.b.c,
              tag: a.d,
              value: a.identifier.flatMap { Int64($0) }↓ ,
              reason: Self.abcd()
            )
            """: """
                Foo(
                  parameter: a.b.c,
                  tag: a.d,
                  value: a.identifier.flatMap { Int64($0) },
                  reason: Self.abcd()
                )
                """,
            """
            return Foo(bar: .baz, title: fuzz,
                      message: My.Custom.message↓ ,
                      another: parameter, doIt: true,
                      alignment: .center)
            """: """
                return Foo(bar: .baz, title: fuzz,
                          message: My.Custom.message,
                          another: parameter, doIt: true,
                          alignment: .center)
                """,
            #"Logger.logError("Hat is too large"↓,  info: [])"#:
                #"Logger.logError("Hat is too large", info: [])"#,
        ])
    )

    func validate(file: SwiftLintFile) -> [StyleViolation] {
        violationRanges(in: file).map {
            StyleViolation(ruleDescription: Self.description,
                           severity: configuration.severity,
                           location: Location(file: file, byteOffset: $0.0.location))
        }
    }

    private func violationRanges(in file: SwiftLintFile) -> [(ByteRange, shouldAddSpace: Bool)] {
        let syntaxTree = file.syntaxTree

        return syntaxTree
            .windowsOfThreeTokens()
            .compactMap { previous, current, next -> (ByteRange, shouldAddSpace: Bool)? in
                if current.tokenKind != .comma {
                    return nil
                }
                if !previous.trailingTrivia.isEmpty, !previous.trailingTrivia.containsBlockComments() {
                    let start = ByteCount(previous.endPositionBeforeTrailingTrivia)
                    let end = ByteCount(current.endPosition)
                    let nextIsNewline = next.leadingTrivia.containsNewlines()
                    return (ByteRange(location: start, length: end - start), shouldAddSpace: !nextIsNewline)
                }
                if !current.trailingTrivia.starts(with: [.spaces(1)]), !next.leadingTrivia.containsNewlines() {
                    let start = ByteCount(current.position)
                    let end = ByteCount(next.positionAfterSkippingLeadingTrivia)
                    return (ByteRange(location: start, length: end - start), shouldAddSpace: true)
                }
                return nil
            }
    }

    func correct(file: SwiftLintFile) -> Int {
        let initialNSRanges = Dictionary(
            uniqueKeysWithValues: violationRanges(in: file)
                .compactMap { byteRange, shouldAddSpace in
                    file.stringView
                        .byteRangeToNSRange(byteRange)
                        .flatMap { ($0, shouldAddSpace) }
                }
        )

        let violatingRanges = file.ruleEnabled(violatingRanges: Array(initialNSRanges.keys), for: self)
        guard violatingRanges.isNotEmpty else {
            return 0
        }

        var contents = file.contents
        for range in violatingRanges.sorted(by: { $0.location > $1.location }) {
            let contentsNSString = contents.bridge()
            let shouldAddSpace = initialNSRanges[range] ?? true
            contents = contentsNSString.replacingCharacters(in: range, with: ",\(shouldAddSpace ? " " : "")")
        }
        file.write(contents)
        return violatingRanges.count
    }
}

private extension Trivia {
    func containsBlockComments() -> Bool {
        contains { piece in
            if case .blockComment = piece {
                return true
            }
            return false
        }
    }
}
