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

import UIKit
import VDSColorTokens

@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:
     - secondary (1 height, silver)
     - primary (1 height, black)
     - standard (1 height, silver) - deprecated
     - thin (1 height, black) - deprecated
     - medium (2 height, black)
     - heavy (4 height, black)
     - 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 type: Style = .secondary
    public var frequency: Frequency? = .allExceptTop
    
    //TODO: use color insted of backgroundColor. Needs server changes
    // public var color: Color?
    private var _backgroundColor: Color?
    public var backgroundColor: Color? {
        get {
            if let backgroundColor = _backgroundColor { return backgroundColor }
            if inverted {
                if type == .secondary || type == .standard { return Color(uiColor: VDSColor.paletteGray20) }
                return Color(uiColor: VDSColor.elementsPrimaryOndark)
            }
            if type == .secondary || type == .standard { return Color(uiColor: VDSColor.paletteGray85) }
            return Color(uiColor: VDSColor.elementsPrimaryOnlight)
        }
        set {
            _backgroundColor = newValue
        }
    }
    
    private var _thickness: CGFloat?
    public var thickness: CGFloat {
        get {
            if let thickness = _thickness { return thickness }
            switch type {
            case .heavy:
                return 4
            case .medium:
                return 2
            case .none:
                return 0
            default:
                return 1
            }
        }
        set {
            _thickness = newValue
        }
    }
    
    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
        case thickness
    }
    
    //--------------------------------------------------
    // 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
        }
        
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        useVerticalLine = try typeContainer.decodeIfPresent(Bool.self, forKey: .useVerticalLine)
        _thickness = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .thickness)
    }
    
    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(useVerticalLine, forKey: .useVerticalLine)
        try container.encodeIfPresent(_thickness, forKey: .thickness)
    }
}
