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

import UIKit


@objcMembers public class LineModel: MoleculeModelProtocol {
    //--------------------------------------------------
    // 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:
     - standard (1 height, silver)
     - thin (1 height, black)
     - medium (2 height, black)
     - heavy (4 height, black)
     - none (hidden)
     */
    public enum Style: String, Codable {
        case standard
        case thin
        case medium
        case heavy
        case none
    }
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public static var identifier: String = "line"
    public var type: Style = .standard
    public var frequency: Frequency? = .allExceptTop
    
    //TODO: use color insted of backgroundColor. Needs server changes
    // public var color: Color?
    public var backgroundColor: Color?
    public var backgroundColor_inverted: Color = Color(uiColor: .mvmWhite)
    public var inverted: Bool = false
    
    // Use this to show vertical line
    // Default is false
    public var useVerticalLine: Bool?
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public init(type: Style) {
        self.type = type
        self.useVerticalLine = false
    }
    
    public init(verticalLineOf type: Style, backgroundColor: Color? = nil) {
        self.type = type
        self.backgroundColor = backgroundColor
        self.useVerticalLine = true
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case type
        case backgroundColor
        case backgroundColor_inverted
        case color
        case frequency
        case inverted
        case useVerticalLine
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        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
        }
        
        if let backgroundColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor_inverted) {
            self.backgroundColor_inverted = backgroundColor_inverted
        }
        
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        useVerticalLine = try typeContainer.decodeIfPresent(Bool.self, forKey: .useVerticalLine)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(type, forKey: .type)
        try container.encode(inverted, forKey: .inverted)
        try container.encodeIfPresent(frequency, forKey: .frequency)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(backgroundColor_inverted, forKey: .backgroundColor_inverted)
        try container.encodeIfPresent(useVerticalLine, forKey: .useVerticalLine)
    }
}
