//
//  CategoriesListInteractor.swift
//  ShoppingList
//
//  Created by Mihai on 3/18/17.
//  Copyright (c) 2017 Mihai. All rights reserved.
//
//  This file was generated by the Clean Swift HELM Xcode Templates
//

import UIKit

protocol CategoriesListInteractorInput: CategoriesListViewControllerOutput {
}

protocol CategoriesListInteractorOutput {
    func presentDataEvent(response: CategoriesListScene.GetCategories.Response)
}

protocol CategoriesListDataSource {
    var selectedCategory: Category? { get }
}

protocol CategoriesListDataDestination {
    
}

class CategoriesListInteractor: CategoriesListInteractorInput, CategoriesListDataSource, CategoriesListDataDestination {
    internal var selectedCategory: Category?

    var dataSource: AnyTableSource<Category> {
        get {
            return internalDataSource
        }
    }
    var output: CategoriesListInteractorOutput!
    var categoriesStore: CategoryStoreProtocol!
    var itemDataSource: ItemDataSource<Category>! {
        didSet {
            internalDataSource = AnyTableSource<Category>(tableSource: itemDataSource)
        }
    }
    
    private var internalDataSource: AnyTableSource<Category>!
    
    // MARK: Business logic
    func getCategories(request: CategoriesListScene.GetCategories.Request) {
        itemDataSource = ItemDataSource<Category>()
        categoriesStore.getItems { (event) in
            DispatchQueue.main.async {
                let e = event
                var newEvent = ListEvent()
                switch e {
                case .itemInserted(let item):
                    newEvent = self.itemDataSource.add(item: item)
                case .itemUpdated(let item):
                    newEvent = self.itemDataSource.update(item: item)
                case .initialData(let items):
                    for item in items {
                        _ = self.itemDataSource.add(item: item)
                    }
                    newEvent = ListEvent()
                case .itemDeleted(let name):
                    newEvent = self.itemDataSource.remove(key: name)
                }
                self.output.presentDataEvent(response: CategoriesListScene.GetCategories.Response(dataEvent: newEvent))
            }
        }
    }
    
    func moveCategory(request: CategoriesListScene.MoveCategory.Request) {
        itemDataSource.moveItem(from: request.fromIndex, to: request.toIndex)
        var location = 0
        if request.toIndex.row > 0 {
            location = itemDataSource.items[request.toIndex.row - 1].location
        }
        for i in request.toIndex.row..<itemDataSource.items.count {
            itemDataSource.items[i].location = location
            categoriesStore.updateLocation(category: itemDataSource.items[i], location: location)
            location = location + 1
        }
    }
    
    func selectCategory(request: CategoriesListScene.SelectCategory.Request) {
        selectedCategory = itemDataSource.item(forIndexPath: request.index)
    }
}
