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

import UIKit
import VDSColorTokens
import VDS
import MVMCore

public typealias FacadeElements = (fill: UIColor?, text: UIColor?, border: UIColor?)

open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWatcherFieldProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    //Making static property as class property so that subclasses can override getter function of the property
    open class var identifier: String { "button" }
    public var id: String = UUID().uuidString
    
    public var accessibilityIdentifier: String?
    public var accessibilityText: String?
    public var title: String
    public var action: ActionModelProtocol
    public var enabled: Bool = true
    public var width: CGFloat?
    public var style: Use = .primary
    public var size: VDS.Button.Size = .large
    public var groupName: String = ""
    public var inverted: Bool = false
    public var accessibilityTraits: UIAccessibilityTraits?
    public var disabledAccessibilityTraits: UIAccessibilityTraits?
    public var backgroundColor: Color?

    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    /// Temporary binding mechanism for the view to update on enable changes.
    public var updateUI: ActionBlock?
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    public init(with title: String, action: ActionModelProtocol) {
        self.title = title
        self.action = action
    }
    
    public init(secondaryButtonWith title: String, action: ActionModelProtocol) {
        self.title = title
        self.action = action
        style = .secondary
    }
    
    public init(primaryButtonWith title: String, action: ActionModelProtocol) {
        self.title = title
        self.action = action
        style = .primary
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case accessibilityIdentifier
        case accessibilityText
        case title
        case inverted
        case action
        case enabled
        case style
        case size
        case groupName
        case width
        case accessibilityTraits
        case disabledAccessibilityTraits
    }
    
    //--------------------------------------------------
    // 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
        accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
        accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
        title = try typeContainer.decode(String.self, forKey: .title)
        action = try typeContainer.decodeModel(codingKey: .action)
        
        ///Style captured from the JSON
        if let style = try typeContainer.decodeIfPresent(Use.self, forKey: .style) {
            self.style = style
        } else if let style = decoder.context?.value(forKey: CodingKeys.style.stringValue) as? Use { ///Reading the style param from context which is set is molecules, ex: TwoButtonView
            self.style = style
        }
        
        if let size = try typeContainer.decodeIfPresent(VDS.Button.Size.self, forKey: .size) {
            self.size = size
        }
        
        if let enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) {
            self.enabled = enabled
        }
        
        if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
            self.groupName = groupName
        }
        
        width = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .width)
        
        if let inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
            self.inverted = inverted
        } else {
            try setInverted(deprecatedFrom: decoder)
        }
        
        accessibilityTraits = try typeContainer.decodeIfPresent(UIAccessibilityTraits.self, forKey: .accessibilityTraits)
        
        disabledAccessibilityTraits = try typeContainer.decodeIfPresent(UIAccessibilityTraits.self, forKey: .disabledAccessibilityTraits)
    }
    
    private enum DeprecatedCodingKeys: String, CodingKey {
        case fillColor
        case textColor
        case borderColor
        case backgroundColor
    }
    
    private func setInverted(deprecatedFrom decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: DeprecatedCodingKeys.self)
        if let enabledFillColor = try typeContainer.decodeIfPresent(Color.self, forKey: .fillColor) {
            if (self.style == .secondary) {
                self.inverted = enabledFillColor.uiColor.isDark()
            } else {
                self.inverted = !enabledFillColor.uiColor.isDark()
            }
        }
        
        if let enabledTextColor = try typeContainer.decodeIfPresent(Color.self, forKey: .textColor) {
            if (self.style == .secondary) {
                self.inverted = !enabledTextColor.uiColor.isDark()
            } else {
                self.inverted = enabledTextColor.uiColor.isDark()
            }
        }
        
        if let enabledBorderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .borderColor) {
            if (self.style == .secondary) {
                self.inverted = !enabledBorderColor.uiColor.isDark()
            }
        }
    
        if let backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) {
            if (self.style == .secondary) {
                self.inverted = backgroundColor.uiColor.isDark()
            } else {
                self.inverted = !backgroundColor.uiColor.isDark()
            }
        }
        
    }
    
    open 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(title, forKey: .title)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(inverted, forKey: .inverted)
        try container.encodeModel(action, forKey: .action)
        try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
        try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
        try container.encode(style, forKey: .style)
        try container.encode(size, forKey: .size)
        try container.encodeIfPresent(groupName, forKey: .groupName)
        try container.encodeIfPresent(width, forKey: .width)
        try container.encodeIfPresent(accessibilityTraits, forKey: .accessibilityTraits)
        try container.encodeIfPresent(disabledAccessibilityTraits, forKey: .disabledAccessibilityTraits)
    }
}
