//
//  LineModel.swift
//  MVMCoreUI
//
//  Created by Suresh, Kamlesh on 10/28/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit
import VDSColorTokens
import VDS

public class LineModel: MoleculeModelProtocol, Invertable {
    //--------------------------------------------------
    // MARK: - Enums
    //--------------------------------------------------
    
    /**
     The frequency of the line in a moleculeList:
     - all (between all cells, above top, below bottom)
     - allExceptTop (between all cells, below bottom)
     - allExceptBottom (between all cells, above top)
     - between (between all cells)
     */
    public enum Frequency: String, Codable  {
        case all
        case allExceptTop
        case allExceptBottom
        case between
    }
    
    /**
     The style of the line:
     - secondary (VDS Secondary)
     - primary (VDS Primary)
     - standard (VDS Secondary) - deprecated
     - thin (VDS Primar) - deprecated
     - medium (VDS Primar)
     - heavy (VDS Primar)
     - none (hidden)
     */
    public enum Style: String, Codable {
        case secondary
        case primary
        case standard
        case thin
        case medium
        case heavy
        case none
    }
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "line"
    public var id: String = UUID().uuidString
    public var backgroundColor: Color?

    public var type: Style = .secondary
    public var frequency: Frequency? = .allExceptTop
        
    public var inverted: Bool = false
    
    public var orientation: VDS.Line.Orientation = .horizontal
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(type: Style) {
        self.type = type
    }
    
    public init(verticalLineOf type: Style) {
        self.type = type
        orientation = .vertical
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case type
        case frequency
        case inverted
        case useVerticalLine
        case orientation
    }
    
    //--------------------------------------------------
    // 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
        
        if let type = try typeContainer.decodeIfPresent(Style.self, forKey: .type) {
            self.type = type
        }
        
        if let frequency = try typeContainer.decodeIfPresent(Frequency.self, forKey: .frequency) {
            self.frequency = frequency
        }
        
        if let inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
            self.inverted = inverted
        }
        
        /// adding code to look for the old useVerticalLine or the new orientation
        if let useVerticalLine = try typeContainer.decodeIfPresent(Bool.self, forKey: .useVerticalLine) {
            orientation = useVerticalLine ? .vertical : .horizontal
        } else if let orientation = try typeContainer.decodeIfPresent(VDS.Line.Orientation.self, forKey: .orientation) {
            self.orientation = orientation
        }
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(id, forKey: .id)
        try container.encode(type, forKey: .type)
        try container.encode(inverted, forKey: .inverted)
        try container.encodeIfPresent(frequency, forKey: .frequency)
        try container.encode(orientation == .vertical, forKey: .useVerticalLine)
    }
}
