//
//  TiletModel.swift
//  MVMCoreUI
//
//  Created by Matt Bruce on 1/12/23.
//  Copyright © 2023 Verizon Wireless. All rights reserved.
//

import Foundation
import VDS

open class TileletModel: TileContainerBaseModel<Tilelet.Padding, Tilelet>, MoleculeModelProtocol {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    public static var identifier: String = "tilelet"
    public var id: String = UUID().uuidString
    public var backgroundColor: Color?
    
    public var badge: Tilelet.BadgeModel?
    public var title: LabelModel?
    public var subTitle: LabelModel?
    public var descriptiveIcon: Tilelet.DescriptiveIcon?
    public var directionalIcon: Tilelet.DirectionalIcon?
    public var textWidth: CGFloat?
    public var textPercentage: CGFloat?
    
    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case badge
        case title
        case subTitle
        case descriptiveIcon
        case directionalIcon
        case textWidth
        case textPercentage
    }
    required public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
        badge = try container.decodeIfPresent(Tilelet.BadgeModel.self, forKey: .badge)
        title = try container.decodeIfPresent(LabelModel.self, forKey: .title)
        subTitle = try container.decodeIfPresent(LabelModel.self, forKey: .subTitle)
        descriptiveIcon = try container.decodeIfPresent(Tilelet.DescriptiveIcon.self, forKey: .descriptiveIcon)
        directionalIcon = try container.decodeIfPresent(Tilelet.DirectionalIcon.self, forKey: .directionalIcon)
        textWidth = try container.decodeIfPresent(CGFloat.self, forKey: .textWidth)
        textPercentage = try container.decodeIfPresent(CGFloat.self, forKey: .textPercentage)
        try super.init(from: decoder)
    }
    
    public func titleModel(delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Tilelet.TitleModel? {
        guard let title else { return nil }
        let attrs = title.attributes?.toVDSLabelAttributeModel(delegateObject: delegateObject, additionalData: additionalData)
        
        do {
            if let style = title.fontStyle {
                return .init(text: title.text,
                             textAttributes: attrs,
                             standardStyle: try style.vdsSubsetStyle())
            }

        } catch MVMCoreError.errorObject(let object) {
            MVMCoreLoggingHandler.shared()?.addError(toLog: object)
        } catch { }
        
        return .init(text: title.text, textAttributes: attrs)
    }
    
    public func subTitleModel(delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Tilelet.SubTitleModel? {
        guard let subTitle else { return nil }
        let attrs = subTitle.attributes?.toVDSLabelAttributeModel(delegateObject: delegateObject, additionalData: additionalData)
        do {
            if let style = subTitle.fontStyle {
                return .init(text: subTitle.text,
                             otherStandardStyle: try style.vdsSubsetStyle(),
                             textAttributes: attrs)
            }
        } catch MVMCoreError.errorObject(let object) {
            MVMCoreLoggingHandler.shared()?.addError(toLog: object)
        } catch { }
        
        return .init(text: subTitle.text, textAttributes: attrs)
    }
    
    public override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(badge, forKey: .badge)
        try container.encodeIfPresent(title, forKey: .title)
        try container.encodeIfPresent(subTitle, forKey: .subTitle)
        try container.encodeIfPresent(descriptiveIcon, forKey: .descriptiveIcon)
        try container.encodeIfPresent(directionalIcon, forKey: .directionalIcon)
        try container.encodeIfPresent(textWidth, forKey: .textWidth)
        try container.encodeIfPresent(textPercentage, forKey: .textPercentage)
        try super.encode(to: encoder)
    }
}
