//
//  ArrowModel.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 2/20/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit


open class ArrowModel: MoleculeModelProtocol, EnableableModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String {
        return "arrow"
    }
    
    public var moleculeName: String?
    public var backgroundColor: Color?
    public var disabledColor: Color = Color(uiColor: .mvmCoolGray3)
    public var color: Color = Color(uiColor: .mvmBlack)
    public var color_inverted: Color = Color(uiColor: .mvmWhite)
    public var degrees: Float = 0
    public var lineWidth: CGFloat = 1
    public var height: CGFloat = 12
    public var width: CGFloat = 12
    public var enabled: Bool = true
    public var inverted: Bool = false
    
    //--------------------------------------------------
    // MARK: - Enum
    //--------------------------------------------------
    
    /// Conveniece for readability of arrow pointing direction.
    public enum Direction: Float {
        case right = 0
        case upperRight = 45
        case up = 90
        case upperLeft = 135
        case left = 180
        case bottomLeft = 225
        case down = 270
        case bottomRight = 315
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init() { }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case disabledColor
        case color
        case degrees
        case lineWidth
        case height
        case width
        case enabled
        case inverted
    }
    
    //--------------------------------------------------
    // 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)
        
        if let disabledColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledColor) {
            self.disabledColor = disabledColor
        }
        
        if let inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
            self.inverted = inverted
        }
        
        if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .color) {
            self.color = color
        }
        
        if let enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) {
            self.enabled = enabled
        }
        
        if let degrees = try typeContainer.decodeIfPresent(Float.self, forKey: .degrees) {
            self.degrees = degrees
        }
        
        if let lineWidth = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .lineWidth) {
            self.lineWidth = lineWidth
        }
        
        if let height = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .height) {
            self.height = height
        }
        
        if let width = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .width) {
            self.width = width
        }
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(disabledColor, forKey: .disabledColor)
        try container.encode(color, forKey: .color)
        try container.encode(degrees, forKey: .degrees)
        try container.encode(lineWidth, forKey: .lineWidth)
        try container.encode(width, forKey: .width)
        try container.encode(height, forKey: .height)
        try container.encode(enabled, forKey: .enabled)
    }
}
