//
//  Label.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 10/3/19.
//  Copyright © 2019 Suresh, Kamlesh. All rights reserved.
//


@objcMembers open class LabelModel: MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    open class var identifier: String { "label" }
    public var backgroundColor: Color?
    public var text: String
    public var accessibilityText: String?
    public var textColor: Color?
    public var fontStyle: Styler.Font?
    public var fontName: String?
    public var fontSize: CGFloat?
    public var textAlignment: NSTextAlignment?
    public var attributes: [LabelAttributeModel]?
    public var html: String?
    public var hero: Int?
    public var makeWholeViewClickable: Bool?
    public var numberOfLines: Int?
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case text
        case accessibilityText
        case textColor
        case backgroundColor
        case fontStyle
        case fontName
        case fontSize
        case textAlignment
        case attributes
        case html
        case hero
        case makeWholeViewClickable
        case numberOfLines
    }
    
    enum AttributeTypeKey: String, CodingKey {
        case type
    }
     
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(text: String) {
        self.text = text
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        text = try typeContainer.decode(String.self, forKey: .text)
        accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
        textColor = try typeContainer.decodeIfPresent(Color.self, forKey: .textColor)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        fontStyle = try typeContainer.decodeIfPresent(Styler.Font.self, forKey: .fontStyle)
        fontName = try typeContainer.decodeIfPresent(String.self, forKey: .fontName)
        fontSize = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .fontSize)
        textAlignment = try typeContainer.decodeIfPresent(NSTextAlignment.self, forKey: .textAlignment)
        attributes = try typeContainer.decodeModelsIfPresent(codingKey: .attributes)
        html = try typeContainer.decodeIfPresent(String.self, forKey: .html)
        hero = try typeContainer.decodeIfPresent(Int.self, forKey: .hero)
        makeWholeViewClickable = try typeContainer.decodeIfPresent(Bool.self, forKey: .makeWholeViewClickable)
        numberOfLines = try typeContainer.decodeIfPresent(Int.self, forKey: .numberOfLines)
    }
    
    open func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(moleculeName, forKey: .moleculeName)
        try container.encode(text, forKey: .text)
        try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
        try container.encodeIfPresent(textColor, forKey: .textColor)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(fontStyle, forKey: .fontStyle)
        try container.encodeIfPresent(fontName, forKey: .fontName)
        try container.encodeIfPresent(fontSize, forKey: .fontSize)
        try container.encodeIfPresent(textAlignment, forKey: .textAlignment)
        try container.encodeModelsIfPresent(attributes, forKey: .attributes)
        try container.encodeIfPresent(html, forKey: .html)
        try container.encodeIfPresent(hero, forKey: .hero)
        try container.encodeIfPresent(makeWholeViewClickable, forKey: .makeWholeViewClickable)
        try container.encodeIfPresent(numberOfLines, forKey: .numberOfLines)
    }
}
