//
//  Glyph.swift
//  lottie-swift
//
//  Created by Brandon Withrow on 1/9/19.
//

import Foundation

/// A model that holds a vector character
final class Glyph: Codable {

  // MARK: Lifecycle

  required init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: Glyph.CodingKeys.self)
    character = try container.decode(String.self, forKey: .character)
    fontSize = try container.decode(Double.self, forKey: .fontSize)
    fontFamily = try container.decode(String.self, forKey: .fontFamily)
    fontStyle = try container.decode(String.self, forKey: .fontStyle)
    width = try container.decode(Double.self, forKey: .width)
    if
      container.contains(.shapeWrapper),
      let shapeContainer = try? container.nestedContainer(keyedBy: ShapeKey.self, forKey: .shapeWrapper),
      shapeContainer.contains(.shapes)
    {
      shapes = try shapeContainer.decode([ShapeItem].self, ofFamily: ShapeType.self, forKey: .shapes)
    } else {
      shapes = []
    }
  }

  // MARK: Internal

  /// The character
  let character: String

  /// The font size of the character
  let fontSize: Double

  /// The font family of the character
  let fontFamily: String

  /// The Style of the character
  let fontStyle: String

  /// The Width of the character
  let width: Double

  /// The Shape Data of the Character
  let shapes: [ShapeItem]

  func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)

    try container.encode(character, forKey: .character)
    try container.encode(fontSize, forKey: .fontSize)
    try container.encode(fontFamily, forKey: .fontFamily)
    try container.encode(fontStyle, forKey: .fontStyle)
    try container.encode(width, forKey: .width)

    var shapeContainer = container.nestedContainer(keyedBy: ShapeKey.self, forKey: .shapeWrapper)
    try shapeContainer.encode(shapes, forKey: .shapes)
  }

  // MARK: Private

  private enum CodingKeys: String, CodingKey {
    case character = "ch"
    case fontSize = "size"
    case fontFamily = "fFamily"
    case fontStyle = "style"
    case width = "w"
    case shapeWrapper = "data"
  }

  private enum ShapeKey: String, CodingKey {
    case shapes
  }
}
