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

import Foundation
import VDSColorTokens

open class TabBarModel: MoleculeModelProtocol {
    public static var identifier: String = "tabBar"
    open var tabs: [TabBarItemModel]
    
    private var _backgroundColor: Color?
    open var backgroundColor: Color? {
        get {
            if let backgroundColor = _backgroundColor { return backgroundColor }
            if let style = style,
               style == .dark { return Color(uiColor: VDSColor.backgroundPrimaryDark) }
            return Color(uiColor: VDSColor.backgroundPrimaryLight)
        }
        set {
            _backgroundColor = newValue
        }
    }

    private var _selectedColor: Color?
    open var selectedColor: Color {
        get {
            if let selectedColor = _selectedColor { return selectedColor }
            if let style = style,
                style == .dark { return Color(uiColor: VDSColor.elementsPrimaryOndark) }
            return Color(uiColor: VDSColor.elementsPrimaryOnlight)
        }
        set {
            _selectedColor = newValue
        }
    }
    
    private var _unSelectedColor: Color?
    open var unSelectedColor: Color {
        get {
            if let unSelectedColor = _unSelectedColor { return unSelectedColor }
            if let style = style,
                style == .dark { return Color(uiColor: VDSColor.elementsSecondaryOndark) }
            return Color(uiColor: VDSColor.elementsSecondaryOnlight)
        }
        set {
            _unSelectedColor = newValue
        }
    }
    
    open var style: NavigationItemStyle? = .dark
    
    // Must be capped to 0...(tabs.count - 1)
    open var selectedTab: Int = 0
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case tabs
        case selectedColor
        case unSelectedColor
        case selectedTab
        case style
    }
    
    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
        }
        if let navStyle = try typeContainer.decodeIfPresent(NavigationItemStyle.self, forKey: .style) {
            style = navStyle
        }
    }
              
    open 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.encodeIfPresent(_backgroundColor, forKey: .backgroundColor)
        try container.encodeIfPresent(_selectedColor, forKey: .selectedColor)
        try container.encodeIfPresent(_unSelectedColor, forKey: .unSelectedColor)
        try container.encode(selectedTab, forKey: .selectedTab)
        try container.encodeIfPresent(style, forKey: .style)
    }
}

open class TabBarItemModel: Codable {
    open var title: String?
    open var image: String
    open var action: ActionModelProtocol
    open 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)
    }
              
    open 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)
    }
}
