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

import UIKit
import VDSColorTokens

open class LinkModel: ButtonModelProtocol, MoleculeModelProtocol, EnableableModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public class var identifier: String { "link" }
    
    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 enabledColor = Color(uiColor: VDSColor.elementsPrimaryOnlight)
    public var enabledColor_inverted = Color(uiColor: VDSColor.elementsPrimaryOndark)
    public var disabledColor = Color(uiColor: VDSColor.interactiveDisabledOnlight)
    public var disabledColor_inverted = Color(uiColor: VDSColor.interactiveDisabledOndark)
    public var activeColor = Color(uiColor: VDSColor.interactiveActiveOnlight)
    public var activeColor_inverted = Color(uiColor: VDSColor.interactiveActiveOndark)
    
    public var inverted = false
    public var size:linkFontSize = linkFontSize.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 moleculeName
        case backgroundColor
        case accessibilityIdentifier
        case accessibilityText
        case title
        case action
        case enabled
        case enabledColor
        case enabledColor_inverted
        case disabledColor
        case disabledColor_inverted
        case activeColor
        case activeColor_inverted
        case inverted
        case size
        case shouldMaskRecordedView
    }

    public enum linkFontSize: String, Codable  {
        case small
        case large
    }
    
    //--------------------------------------------------
    // MARK: - Method
    //--------------------------------------------------
    
    func getFont(_ type: linkFontSize) -> UIFont {
        switch type {
        case .small:
            return MFStyler.fontRegularBodySmall()
        case .large:
            return MFStyler.fontRegularBodyLarge()
        }
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        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 enabledColor = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledColor) {
            self.enabledColor = enabledColor
        }
        
        if let enabledColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledColor_inverted) {
            self.enabledColor_inverted = enabledColor_inverted
        }
        
        if let disabledColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledColor) {
            self.disabledColor = disabledColor
        }
        
        if let disabledColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledColor_inverted) {
            self.disabledColor_inverted = disabledColor_inverted
        }

        if let activeColor = try typeContainer.decodeIfPresent(Color.self, forKey: .activeColor) {
            self.activeColor = activeColor
        }
        
        if let activeColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .activeColor_inverted) {
            self.activeColor_inverted = activeColor_inverted
        }
        if let size = try typeContainer.decodeIfPresent(linkFontSize.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(title, forKey: .title)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encodeModel(action, forKey: .action)
        try container.encode(inverted, forKey: .inverted)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(enabledColor, forKey: .enabledColor)
        try container.encode(enabledColor_inverted, forKey: .enabledColor_inverted)
        try container.encode(disabledColor, forKey: .disabledColor)
        try container.encode(disabledColor_inverted, forKey: .disabledColor_inverted)
        try container.encode(activeColor, forKey: .activeColor)
        try container.encode(activeColor_inverted, forKey: .activeColor_inverted)
        try container.encodeIfPresent(size, forKey: .size)
        try container.encode(shouldMaskRecordedView, forKey: .shouldMaskRecordedView)
    }
}
