//
//  ViewController.swift
//  BindCollectionViews
//
//  Created by Scott Gardner on 6/4/16.
//  Copyright © 2016 Scott Gardner. All rights reserved.
//

import UIKit
import RxSwift
import RxDataSources

extension String: IdentifiableType {
    public typealias Identity = String
    public var identity: Identity { return self }
}

struct AnimatedSectionModel {
    let title: String
    var data: [String]
}

extension AnimatedSectionModel: AnimatableSectionModelType {
    typealias Item = String
    typealias Identity = String
    var identity: Identity { return title }
    var items: [Item] { return data }
    
    init(original: AnimatedSectionModel, items: [String]) {
        self = original
        data = items
    }
}

class ViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var addBarButtonItem: UIBarButtonItem!
    @IBOutlet var longPressGestureRecognizer: UILongPressGestureRecognizer!
    
    let disposeBag = DisposeBag()
    let dataSource = RxCollectionViewSectionedAnimatedDataSource<AnimatedSectionModel>()
    
    let data = Variable([
        AnimatedSectionModel(title: "Section: 0", data: ["0-0"])
        ])
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        dataSource.cellFactory = { _, collectionView, indexPath, title in
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! Cell
            cell.titleLabel.text = title
            return cell
        }
        
        dataSource.supplementaryViewFactory = { dataSource, collectionView, kind, indexPath in
            let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! Header
            header.titleLabel.text = dataSource.sectionAtIndex(indexPath.section).title
            return header
        }
        
        data.asDriver()
        .drive(collectionView.rx_itemsWithDataSource(dataSource))
            .addDisposableTo(disposeBag)
        
        addBarButtonItem.rx_tap.asDriver()
            .driveNext { [unowned self] in
                let section = self.data.value.count
                let items: [String] = {
                    var items = [String]()
                    let random = Int(arc4random_uniform(6)) + 1
                    (0...random).forEach {
                        items.append("\(section)-\($0)")
                    }
                    
                    return items
                }()
                
                self.data.value += [AnimatedSectionModel(title: "Section: \(section)", data: items)]
            }
            .addDisposableTo(disposeBag)
        
        longPressGestureRecognizer.rx_event
            .subscribeNext { [unowned self] in
                switch $0.state {
                case .Began:
                    guard let selectedIndexPath = self.collectionView.indexPathForItemAtPoint($0.locationInView(self.collectionView)) else { break }
                    self.collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
                case .Changed:
                    self.collectionView.updateInteractiveMovementTargetPosition($0.locationInView($0.view!))
                case .Ended:
                    self.collectionView.endInteractiveMovement()
                default:
                    self.collectionView.cancelInteractiveMovement()
                }
            }
            .addDisposableTo(disposeBag)
    }
    
}
