//
//  LinkModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 1/13/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit
import VDS

open class LinkModel: ButtonModelProtocol, MoleculeModelProtocol, EnableableModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public class var identifier: String { "link" }
    public var id: String = UUID().uuidString
    
    public var backgroundColor: Color?
    public var accessibilityIdentifier: String?
    public var title: String
    public var accessibilityText: String?
    public var action: ActionModelProtocol
    public var enabled = true
    
    public var inverted = false
    public var size: TextLink.Size = .small
    
    public var shouldMaskRecordedView: Bool? = false
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(title: String, action: ActionModelProtocol) {
        self.title = title
        self.action = action
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case backgroundColor
        case accessibilityIdentifier
        case accessibilityText
        case title
        case action
        case enabled
        case inverted
        case size
        case shouldMaskRecordedView
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
        title = try typeContainer.decode(String.self, forKey: .title)
        accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
        action = try typeContainer.decodeModel(codingKey: .action)
        
        if let enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) {
            self.enabled = enabled
        }
        
        if let inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
            self.inverted = inverted
        }
        
        if let size = try typeContainer.decodeIfPresent(TextLink.Size.self, forKey: .size) {
            self.size = size
        }
        
        shouldMaskRecordedView = try typeContainer.decodeIfPresent(Bool.self, forKey: .shouldMaskRecordedView) ?? shouldMaskRecordedView
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(title, forKey: .title)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
        try container.encodeModel(action, forKey: .action)
        try container.encode(inverted, forKey: .inverted)
        try container.encode(enabled, forKey: .enabled)
        try container.encodeIfPresent(size, forKey: .size)
        try container.encode(shouldMaskRecordedView, forKey: .shouldMaskRecordedView)
    }
}

extension LinkModel {
    public var surface: Surface { inverted ? .dark : .light }
}
