import Foundation
import CoreData

@objc(Link)
public class Link: NSManagedObject {

}

extension Link {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Link> {
        return NSFetchRequest<Link>(entityName: "Link")
    }

    @NSManaged public var alias: String
    @NSManaged public var originalURL: String?
    @NSManaged public var text: String?
    @NSManaged public var linkType: String
    @NSManaged public var clickCount: Int32
    @NSManaged public var createdAt: Date
    @NSManaged public var updatedAt: Date
    @NSManaged public var linkDescription: String?
    @NSManaged public var tags: NSSet?
    @NSManaged public var clickLogs: NSSet?
    @NSManaged public var changeLogs: NSSet?

}

// MARK: Generated accessors for tags
extension Link {

    @objc(addTagsObject:)
    @NSManaged public func addToTags(_ value: Tag)

    @objc(removeTagsObject:)
    @NSManaged public func removeFromTags(_ value: Tag)

    @objc(addTags:)
    @NSManaged public func addToTags(_ values: NSSet)

    @objc(removeTags:)
    @NSManaged public func removeFromTags(_ values: NSSet)

}

// MARK: Generated accessors for clickLogs
extension Link {

    @objc(addClickLogsObject:)
    @NSManaged public func addToClickLogs(_ value: ClickLog)

    @objc(removeClickLogsObject:)
    @NSManaged public func removeFromClickLogs(_ value: ClickLog)

    @objc(addClickLogs:)
    @NSManaged public func addToClickLogs(_ values: NSSet)

    @objc(removeClickLogs:)
    @NSManaged public func removeFromClickLogs(_ values: NSSet)

}

// MARK: Generated accessors for changeLogs
extension Link {

    @objc(addChangeLogsObject:)
    @NSManaged public func addToChangeLogs(_ value: ChangeLog)

    @objc(removeChangeLogsObject:)
    @NSManaged public func removeFromChangeLogs(_ value: ChangeLog)

    @objc(addChangeLogs:)
    @NSManaged public func addToChangeLogs(_ values: NSSet)

    @objc(removeChangeLogs:)
    @NSManaged public func removeFromChangeLogs(_ values: NSSet)

}

extension Link : Identifiable {

    public var id: NSManagedObjectID {
        return self.objectID
    }

    var linkTypeEnum: LinkType {
        get {
            return LinkType(rawValue: linkType) ?? .link
        }
        set {
            linkType = newValue.rawValue
        }
    }

    var sortedTags: [Tag] {
        let tagSet = tags as? Set<Tag> ?? []
        return tagSet.sorted { $0.name < $1.name }
    }

    var sortedClickLogs: [ClickLog] {
        let clickLogSet = clickLogs as? Set<ClickLog> ?? []
        return clickLogSet.sorted { $0.clickedAt > $1.clickedAt }
    }

    var isTemplate: Bool {
        return originalURL?.contains("{") == true && originalURL?.contains("}") == true
    }

    func hasParametersInURL() -> Bool {
        return isTemplate
    }
}

enum LinkType: String, CaseIterable {
    case link = "LINK"
    case custom = "CUSTOM"
    case template = "TEMPLATE"

    var displayName: String {
        switch self {
        case .link:
            return NSLocalizedString("link", comment: "Link type")
        case .custom:
            return NSLocalizedString("custom", comment: "Custom type")
        case .template:
            return NSLocalizedString("template", comment: "Template type")
        }
    }
}
