//
//  StepModel.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 7/6/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit
import MVMCoreUI

open class StepModel: MoleculeModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    @DecodableDefault.UUIDString public var id: String
    public var backgroundColor: Color?
    
    public static var identifier: String = "step"
    public var state: State?
    public var headline: LabelModel
    public var bodyTop: LabelModel?
    public var bodyBottom: LabelModel?
    public var iconColor: Color?
    public var lineColor: Color?
    
    //--------------------------------------------------
    // MARK: - Enum
    //--------------------------------------------------
    
    public enum State: String, Codable {
        case complete
        case incomplete
        case invalid
        case cancel
        
        func image() -> UIImage? {
            
            switch self {
            case .complete:
                return UIImage(named: "icon_tracker_complete")?.withRenderingMode(.alwaysTemplate)
                
            case .incomplete:
                return UIImage(named: "icon_tracker_incomplete")?.withRenderingMode(.alwaysTemplate)
                
            case .invalid:
                return UIImage(named:"icon_tracker_invalid")?.withRenderingMode(.alwaysTemplate)
                
            case .cancel:
                return UIImage(named:"mf_orange_failed_checkmark")?.withRenderingMode(.alwaysTemplate)
            }
        }
        
        func color() -> UIColor {
            
            switch self {
            case .complete:
                return .mvmGreen
                
            case .incomplete, .invalid:
                return .mvmCoolGray3
                
            case .cancel:
                return .mvmOrangeAA
            }
        }
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case backgroundColor
        case state
        case headline
        case bodyTop
        case bodyBottom
        case iconColor
        case lineColor
    }
    
    //--------------------------------------------------
    // 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)
        state = try typeContainer.decodeIfPresent(State.self, forKey: .state)
        headline = try typeContainer.decode(LabelModel.self, forKey: .headline)
        bodyTop = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .bodyTop)
        bodyBottom = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .bodyBottom)
        iconColor = try typeContainer.decodeIfPresent(Color.self, forKey: .iconColor)
        lineColor = try typeContainer.decodeIfPresent(Color.self, forKey: .lineColor)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encode(headline, forKey: .headline)
        try container.encodeIfPresent(bodyTop, forKey: .bodyTop)
        try container.encodeIfPresent(bodyBottom, forKey: .bodyBottom)
        try container.encodeIfPresent(iconColor, forKey: .iconColor)
        try container.encodeIfPresent(lineColor, forKey: .lineColor)
    }
}
