//
//  MultiItemDropdownEntryFieldModel.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 2/9/21.
//  Copyright © 2021 Verizon Wireless. All rights reserved.
//

import Foundation


@objcMembers open class MultiItemDropdownEntryFieldModel: BaseItemPickerEntryFieldModel {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public override class var identifier: String { "multiDropdown" }
    
    public var components: [[String]] = [[]]
    public var selectedIndexes: [Int: Int] = [:]
    public var delimiters: [Int: String] = [:]
    
    //--------------------------------------------------
    // MARK: - Validation
    //--------------------------------------------------
    
    public override func formFieldValue() -> AnyHashable? {
        guard enabled else { return nil }
        guard !components.isEmpty && !selectedIndexes.isEmpty else { return nil }
        
        return selectedRowText
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    /// - parameter index: The index of the delimiter.
    /// - returns: The delimiter for a given index. Defaults to whitespace for valid index if no delimiters is provided. If invalid index, empty string.
    public func delimiter(for index: Int) -> String {

        guard index != components.count - 1 else { return "" }
        
        return delimiters[index, default: " "]
    }
    
    /// A string of the picker row concatenated by whitespace or delimiters if provided.
    public var selectedRowText: String {
        
        var text = ""
        
        for i in 0..<components.count {
            let pickerIndex = selectedIndexes[i] ?? 0
            text += components[i][pickerIndex] + delimiter(for: i)
        }
        
        return text
    }
    
    public var selectedIndexesArray: [Int] {
        
        var indexArray: [Int] = []
        
        for i in 0..<selectedIndexes.count {
            guard let selectIndex = selectedIndexes[i] else { return [] }
            indexArray.append(selectIndex)
        }
        
        return indexArray
    }
    
    public var delimiterArray: [String] {
        
        var array: [String] = []
        
        for i in 0..<delimiters.count {
            guard let delimiterIndex = delimiters[i] else { return [] }
            array.append(delimiterIndex)
        }
        
        return array
    }
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case components
        case selectedIndexes
        case delimiters
    }
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    required public init(from decoder: Decoder) throws {
        try super.init(from: decoder)
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        
        components = try typeContainer.decode([[String]].self, forKey: .components)
        
        if let delimiters = try typeContainer.decodeIfPresent([String].self, forKey: .delimiters) {
            for (index, delimiter) in delimiters.enumerated() {
                self.delimiters[index] = delimiter
            }
        }
        
        if let indexes = try typeContainer.decodeIfPresent([Int].self, forKey: .selectedIndexes) {
            for (component, index) in indexes.enumerated() {
                self.selectedIndexes[component] = index
            }
            
            baseValue = selectedRowText
        }
    }
    
    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(components, forKey: .components)
        try container.encode(selectedIndexesArray, forKey: .selectedIndexes)
        try container.encodeIfPresent(delimiterArray, forKey: .delimiters)
    }
}
