//
//  Emoji.swift
//  Pachyderm
//
//  Created by Shadowfacts on 9/8/18.
//  Copyright © 2018 Shadowfacts. All rights reserved.
//

import Foundation
import WebURL

public struct Emoji: Codable, Sendable {
    public let shortcode: String
    // these shouldn't need to be WebURLs as they're not external resources,
    // but some instances (pleroma?) has emoji urls that Foundation considers malformed so we use WebURL to be more lenient
    public let url: WebURL
    public let staticURL: WebURL
    public let visibleInPicker: Bool
    public let category: String?
    
    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        
        self.shortcode = try container.decode(String.self, forKey: .shortcode)
        self.url = try container.decode(WebURL.self, forKey: .url)
        self.staticURL = try container.decode(WebURL.self, forKey: .staticURL)
        self.visibleInPicker = try container.decode(Bool.self, forKey: .visibleInPicker)
        self.category = try container.decodeIfPresent(String.self, forKey: .category)
    }
    
    private enum CodingKeys: String, CodingKey {
        case shortcode
        case url
        case staticURL = "static_url"
        case visibleInPicker = "visible_in_picker"
        case category
    }
}

extension Emoji: CustomDebugStringConvertible {
    public var debugDescription: String {
        return ":\(shortcode):"
    }
}

extension Emoji: Equatable {
    public static func ==(lhs: Emoji, rhs: Emoji) -> Bool {
        return lhs.shortcode == rhs.shortcode && lhs.url == rhs.url
    }
}
