//
//  TabBarModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 5/28/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

public class TabBarModel: MoleculeModelProtocol {
    public static var identifier: String = "tabBar"
    public var backgroundColor: Color? = Color(uiColor: .white)
    public var tabs: [TabBarItemModel]
    public var selectedColor = Color(uiColor: .mvmBlack)
    public var unSelectedColor = Color(uiColor: .mvmCoolGray6)
    
    // Must be capped to 0...(tabs.count - 1)
    public var selectedTab: Int = 0
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case tabs
        case selectedColor
        case unSelectedColor
        case selectedTab
    }
    
    public init(with tabs: [TabBarItemModel]) {
        self.tabs = tabs
    }
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        tabs = try typeContainer.decode([TabBarItemModel].self, forKey: .tabs)
        if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) {
            backgroundColor = color
        }
        if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .unSelectedColor) {
            unSelectedColor = color
        }
        if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .selectedColor) {
            selectedColor = color
        }
        if let index = try typeContainer.decodeIfPresent(Int.self, forKey: .selectedTab) {
            selectedTab = index
        }
    }
              
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(tabs, forKey: .tabs)
        try container.encode(backgroundColor, forKey: .backgroundColor)
        try container.encode(selectedColor, forKey: .selectedColor)
        try container.encode(unSelectedColor, forKey: .unSelectedColor)
        try container.encode(selectedTab, forKey: .selectedTab)
    }
}

public class TabBarItemModel: Codable {
    var title: String?
    var image: String
    var action: ActionModelProtocol
    var accessibilityText: String?

    private enum CodingKeys: String, CodingKey {
        case title
        case image
        case action
        case accessibilityText
    }
    
    public init(with title: String?, image: String, action: ActionModelProtocol) {
        self.title = title
        self.image = image
        self.action = action
    }
        
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        title = try typeContainer.decodeIfPresent(String.self, forKey: .title)
        image = try typeContainer.decode(String.self, forKey: .image)
        action = try typeContainer.decodeModel(codingKey: .action)
        accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
    }
              
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(title, forKey: .title)
        try container.encode(image, forKey: .image)
        try container.encodeModel(action, forKey: .action)
        try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
    }
}
