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

import Foundation
import VDS
import VDSColorTokens
import MVMCore

open class TooltipModel: MoleculeModelProtocol {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    public static var identifier: String = "tooltip"
    
    @DecodableDefault.UUIDString public var id: String
    
    public var backgroundColor: Color?
    
    public var size: VDS.Tooltip.Size = .medium
    
    public var fillColor: VDS.Tooltip.FillColor = .primary

    public var closeButtonText: String = { MVMCoreUIUtility.hardcodedString(withKey: "AccCloseButton") ?? "Close" }()

    public var title: String?
    
    public var content: String?
    
    public var molecule: MoleculeModelProtocol?
    
    /// surface of the object
    public var surface: VDS.Surface = .light

    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case backgroundColor
        case closeButtonText
        case title
        case content
        case contentView
        case size
        case fillColor
        case surface
    }
    
    required public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
        self.backgroundColor = try container.decodeIfPresent(Color.self, forKey: .backgroundColor)

        self.title = try container.decodeIfPresent(String.self, forKey: .title)
        self.content = try container.decodeIfPresent(String.self, forKey: .content)
        
        self.molecule = try container.decodeModelIfPresent(codingKey: .contentView)

        if let closeButtonText = try container.decodeIfPresent(String.self, forKey: .closeButtonText) {
            self.closeButtonText = closeButtonText
        }

        if let surface = try container.decodeIfPresent(VDS.Surface.self, forKey: .surface) {
            self.surface = surface
        }

        if let size = try container.decodeIfPresent(VDS.Tooltip.Size.self, forKey: .size) {
            self.size = size
        }
        
        if let fillColor = try container.decodeIfPresent(VDS.Tooltip.FillColor.self, forKey: .fillColor) {
            self.fillColor = fillColor
        }
    }
    
    public 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.encode(surface, forKey: .surface)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)

        try container.encodeIfPresent(size, forKey: .size)
        try container.encodeIfPresent(fillColor, forKey: .fillColor)

        try container.encodeIfPresent(closeButtonText, forKey: .closeButtonText)
        try container.encodeIfPresent(title, forKey: .title)
        try container.encodeIfPresent(content, forKey: .content)

        try container.encodeModelIfPresent(molecule, forKey: .contentView)
    }

}
