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

import UIKit
import VDSColorTokens
import VDS
open class TabsModel: MoleculeModelProtocol {
    
    public static var identifier: String = "tabs"
    public var id: String = UUID().uuidString
    
    open var tabs: [TabItemModel]
    
    open var style: Surface?
        
    open var orientation: Tabs.Orientation = .horizontal
    open var indicatorPosition: Tabs.IndicatorPosition = .bottom
    open var overflow: Tabs.Overflow = .scroll
    open var fillContainer: Bool = false
    open var size: Tabs.Size = .medium
    open var borderLine: Bool = true
    open var minWidth: CGFloat?
    public var backgroundColor: Color?

    // Must be capped to 0...(tabs.count - 1)
    open var selectedIndex: Int = 0
    
    private enum CodingKeys: String, CodingKey {
        case id
        case moleculeName
        case tabs
        case backgroundColor
        case selectedIndex
        case orientation
        case indicatorPosition
        case fillContainer
        case overflow
        case size
        case borderLine
        case minWidth
        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)
        id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
        tabs = try typeContainer.decode([TabItemModel].self, forKey: .tabs)
        style = try typeContainer.decodeIfPresent(Surface.self, forKey: .style)
        
        if let fillContainer = try typeContainer.decodeIfPresent(Bool.self, forKey: .fillContainer) {
            self.fillContainer = fillContainer
        }

        if let index = try typeContainer.decodeIfPresent(Int.self, forKey: .selectedIndex) {
            selectedIndex = index
        }
        
        if let orientation = try typeContainer.decodeIfPresent(VDS.Tabs.Orientation.self, forKey: .orientation) {
            self.orientation = orientation
        }

        if let indicatorPosition = try typeContainer.decodeIfPresent(VDS.Tabs.IndicatorPosition.self, forKey: .indicatorPosition) {
            self.indicatorPosition = indicatorPosition
        }

        if let overflow = try typeContainer.decodeIfPresent(VDS.Tabs.Overflow.self, forKey: .overflow) {
            self.overflow = overflow
        }

        if let size = try typeContainer.decodeIfPresent(VDS.Tabs.Size.self, forKey: .orientation) {
            self.size = size
        }
        
        if let borderLine = try typeContainer.decodeIfPresent(Bool.self, forKey: .borderLine) {
            self.borderLine = borderLine
        }

        if let minWidth = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .minWidth) {
            self.minWidth = minWidth
        }

    }
              
    open func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(tabs, forKey: .tabs)
        try container.encode(selectedIndex, forKey: .selectedIndex)
        try container.encode(fillContainer, forKey: .fillContainer)
        try container.encodeIfPresent(style, forKey: .style)
        try container.encode(orientation, forKey: .orientation)
        try container.encode(overflow, forKey: .overflow)
        try container.encode(size, forKey: .size)
        try container.encode(indicatorPosition, forKey: .indicatorPosition)
        try container.encode(borderLine, forKey: .borderLine)
        try container.encodeIfPresent(minWidth, forKey: .minWidth)
    }
}



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)
    }
}
