//
//  TabsModel.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 1/13/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit
import VDSColorTokens

open class TabsModel: MoleculeModelProtocol {
    public static var identifier: String = "tabs"
    open var tabs: [TabItemModel]
    
    open var style: NavigationItemStyle?
    
    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
        }
    }
    
    private var _selectedBarColor: Color?
    open var selectedBarColor: Color {
        get {
            if let selectedBarColor = _selectedBarColor { return selectedBarColor }
            if let style = style,
               style == .dark { return Color(uiColor: VDSColor.elementsPrimaryOndark) }
            return Color(uiColor: VDSColor.paletteRed)
        }
        set {
            _selectedBarColor = newValue
        }
    }
    
    // Must be capped to 0...(tabs.count - 1)
    open var selectedIndex: Int = 0
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case tabs
        case backgroundColor
        case selectedColor
        case unselectedColor
        case selectedBarColor
        case selectedIndex
        case style
    }
    
    public init(with tabs: [TabItemModel]) {
        self.tabs = tabs
    }
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        tabs = try typeContainer.decode([TabItemModel].self, forKey: .tabs)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        _selectedColor = try typeContainer.decodeIfPresent(Color.self, forKey: .selectedColor)
        _unselectedColor = try typeContainer.decodeIfPresent(Color.self, forKey: .unselectedColor)
        _selectedBarColor = try typeContainer.decodeIfPresent(Color.self, forKey: .selectedBarColor)
        style = try typeContainer.decodeIfPresent(NavigationItemStyle.self, forKey: .style)
        if let index = try typeContainer.decodeIfPresent(Int.self, forKey: .selectedIndex) {
            selectedIndex = index
        }
    }
              
    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.encodeIfPresent(_selectedBarColor, forKey: .selectedBarColor)
        try container.encode(selectedIndex, forKey: .selectedIndex)
        try container.encodeIfPresent(style, forKey: .style)
    }
}



open class TabItemModel: Codable {
    open var label: LabelModel
    open var action: ActionModelProtocol?
    
    public init(label: LabelModel) {
        self.label = label
    }
    
    private enum CodingKeys: String, CodingKey {
        case label
        case action
    }
    
    open func setDefaults() {
        if label.textAlignment == nil {
            label.textAlignment = .center
        }
    }
    
    public init(with label: LabelModel, action: ActionModelProtocol?) {
        self.label = label
        self.action = action
        setDefaults()
    }
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        label = try typeContainer.decode(LabelModel.self, forKey: .label)
        action = try typeContainer.decodeModelIfPresent(codingKey: .action)
        setDefaults()
    }
              
    open func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeModel(label, forKey: .label)
        try container.encodeModelIfPresent(action, forKey: .action)
    }
}
