//
//  StoragesHelper+Params.swift
//  QuickBrickXray
//
//  Created by Alex Zchut on 22/11/2021.
//  Copyright © 2021 Applicaster Ltd. All rights reserved.
//

import AccordionSwift
import Foundation

public extension StoragesHelper {
    typealias ChildItemData = (parentItem: StorageNamespace, childItem: StorageItem, storageType: StorageType)
    typealias ParentItemData = (parentItem: StorageNamespace, storageType: StorageType)
    typealias StorageData = (namespaces: [StorageNamespace], storageType: StorageType)

    static func childParams(for indexPath: IndexPath,
                            in dataSource: DataSource<Parent<StorageNamespace, StorageItem>>) -> ChildItemData?
    {
        guard let storageType = StorageType.fromString(dataSource.headerTitle(inSection: indexPath.section)),
              let expandedParentIndexPath = dataSource.indexOfFirstExpandedParent(),
              let parentItem = dataSource.item(atRow: expandedParentIndexPath.row,
                                               inSection: expandedParentIndexPath.section),
              let childItem = dataSource.childItem(atRow: indexPath.row,
                                                   inSection: expandedParentIndexPath.section,
                                                   parentIndex: expandedParentIndexPath.row,
                                                   currentPos: expandedParentIndexPath.row)
        else {
            return nil
        }

        return (parentItem: parentItem.item, childItem: childItem, storageType: storageType)
    }

    static func parentParams(for indexPath: IndexPath,
                             in dataSource: DataSource<Parent<StorageNamespace, StorageItem>>) -> ParentItemData?
    {
        guard let storageType = StorageType.fromString(dataSource.headerTitle(inSection: indexPath.section)),
              let parentItem = dataSource.item(atRow: indexPath.row,
                                               inSection: indexPath.section)
        else {
            return nil
        }

        return (parentItem: parentItem.item, storageType: storageType)
    }

    static func storageParams(for index: Int,
                              in dataSource: DataSource<Parent<StorageNamespace, StorageItem>>) -> StorageData?
    {
        guard let storageType = StorageType.fromString(dataSource.headerTitle(inSection: index)),
              index < dataSource.sections.count
        else {
            return nil
        }
        let section = dataSource.sections[index]
        let namespaces = section.items.map(\.item)
        return (namespaces: namespaces, storageType: storageType)
    }

    static func storageParams(in dataSource: DataSource<Parent<StorageNamespace, StorageItem>>) -> [StorageData] {
        var storages: [StorageData] = []
        for (index, section) in dataSource.sections.enumerated() {
            if let storageType = StorageType.fromString(dataSource.headerTitle(inSection: index)) {
                let namespaces = section.items.map(\.item)
                storages.append((namespaces: namespaces, storageType: storageType))
            }
        }

        return storages
    }
}
