//
//  ButtonIconModel.swift
//  MVMCoreUI
//
//  Created by Matt Bruce on 4/12/24.
//  Copyright © 2024 Verizon Wireless. All rights reserved.
//

import Foundation
import VDS

open class ButtonIconModel: ButtonModelProtocol, MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    public static var identifier: String = "buttonIcon"
    public var id: String = UUID().uuidString
    public var backgroundColor: Color?
    
    //--------------------------------------------------
    // MARK: - VDS Properties
    //--------------------------------------------------
    public var surface: Surface { inverted ? .dark : .light }
    public var inverted: Bool = false
    public var accessibilityText: String?
    
    public var action: ActionModelProtocol
    
    public var kind =  ButtonIcon.Kind.ghost
    public var surfaceType = ButtonIcon.SurfaceType.colorFill
    public var iconName: Icon.Name = .info
    public var selectedIconName: Icon.Name?
    public var size =  ButtonIcon.Size.large
    public var customSize : Int?
    public var floating: Bool = false
    public var fitToIcon: Bool = false
    public var hideBorder: Bool = true
    public var showBadgeIndicator: Bool = false
    public var selectable: Bool = false
    public var iconOffset: CGPoint = .zero
    
    public var badgeIndicatorModel: VDS.ButtonIcon.BadgeIndicatorModel? {
        guard let model = badgeIndicator else { return nil }
        return .init(kind: model.kind,
                             fillColor: model.fillColor,
                             expandDirection: expandDirection,
                             size: model.size,
                             maximumDigits: model.maximumDigits,
                             width: model.width,
                             height: model.height,
                             number: model.number,
                             leadingCharacter: model.leadingCharacter,
                             trailingText: model.trailingText,
                             dotSize: model.dotSize,
                             verticalPadding: model.verticalPadding,
                             horizontalPadding: model.horizontalPadding,
                             hideDot: model.hideDot,
                             hideBorder: model.hideBorder)
    }

    private var badgeIndicator: BadgeIndicatorModel?
    private var expandDirection = ButtonIcon.BadgeIndicatorModel.ExpandDirection.right

    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    public init(with iconName: VDS.Icon.Name, action: ActionModelProtocol) {
        self.iconName = iconName
        self.action = action
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    private enum CodingKeys: String, CodingKey {
        case id
        case inverted
        case accessibilityText
        case action
        case badgeIndicator
        case expandDirection
        case kind
        case surfaceType
        case iconName
        case selectedIconName
        case size
        case customSize
        case floating
        case fitToIcon
        case hideBorder
        case showBadgeIndicator
        case selectable
        case iconOffset
    }

    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    required public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        action = try container.decodeModel(codingKey: .action)
        id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
        inverted = try container.decodeIfPresent(Bool.self, forKey: .inverted) ?? false
        accessibilityText = try container.decodeIfPresent(String.self, forKey: .accessibilityText)
        badgeIndicator = try container.decodeIfPresent(BadgeIndicatorModel.self, forKey: .badgeIndicator)
        expandDirection = try container.decodeIfPresent(ButtonIcon.BadgeIndicatorModel.ExpandDirection.self, forKey: .expandDirection) ?? .right
        kind = try container.decodeIfPresent(ButtonIcon.Kind.self, forKey: .kind) ?? .ghost
        surfaceType = try container.decodeIfPresent(ButtonIcon.SurfaceType.self, forKey: .kind) ?? .colorFill
        iconName = try container.decode(Icon.Name.self, forKey: .iconName)
        selectedIconName = try container.decodeIfPresent(Icon.Name.self, forKey: .selectedIconName)
        size = try container.decodeIfPresent(ButtonIcon.Size.self, forKey: .size) ?? .large
        customSize = try container.decodeIfPresent(Int.self, forKey: .customSize)
        floating = try container.decodeIfPresent(Bool.self, forKey: .floating) ?? false
        fitToIcon = try container.decodeIfPresent(Bool.self, forKey: .fitToIcon) ?? false
        hideBorder = try container.decodeIfPresent(Bool.self, forKey: .hideBorder) ?? false
        showBadgeIndicator = try container.decodeIfPresent(Bool.self, forKey: .showBadgeIndicator) ?? false
        selectable = try container.decodeIfPresent(Bool.self, forKey: .selectable) ?? false
        iconOffset = try container.decodeIfPresent(CGPoint.self, forKey: .iconOffset) ?? .zero
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(inverted, forKey: .inverted)
        try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
        try container.encodeIfPresent(badgeIndicator, forKey: .badgeIndicator)
        try container.encodeIfPresent(expandDirection, forKey: .expandDirection)
        try container.encodeIfPresent(kind, forKey: .kind)
        try container.encodeIfPresent(surfaceType, forKey: .kind)
        try container.encode(iconName, forKey: .iconName)
        try container.encodeIfPresent(selectedIconName, forKey: .selectedIconName)
        try container.encodeIfPresent(size, forKey: .size)
        try container.encodeIfPresent(customSize, forKey: .customSize)
        try container.encodeIfPresent(floating, forKey: .floating)
        try container.encodeIfPresent(fitToIcon, forKey: .fitToIcon)
        try container.encodeIfPresent(hideBorder, forKey: .hideBorder)
        try container.encodeIfPresent(showBadgeIndicator, forKey: .showBadgeIndicator)
        try container.encodeIfPresent(selectable, forKey: .selectable)
        try container.encodeIfPresent(iconOffset, forKey: .iconOffset)
    }
}
