//
//  ProgrammaticCollectionViewController.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 4/8/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import Foundation

/// A base view controller with a collection view.
@objc open class ProgrammaticCollectionViewController: ScrollingViewController {
    
    public var collectionView: CollectionView?
    public var topConstraint: NSLayoutConstraint?
    public var bottomConstraint: NSLayoutConstraint?

    open override func loadView() {
        let view = UIView()
        view.backgroundColor = .white
        
        let collection = createCollectionView()
        view.addSubview(collection)
        let constraints = NSLayoutConstraint.constraintPinSubview(toSuperview: collection)
        topConstraint = constraints?[ConstraintTop] as? NSLayoutConstraint
        bottomConstraint = constraints?[ConstraintBot] as? NSLayoutConstraint
        
        collectionView = collection
        scrollView = collectionView
        self.view = view
    }
    
    /// A place to register cells with the collectionView
    open func registerCells() {}

    open override func viewDidLoad() {
        super.viewDidLoad()
        registerCells()
    }

    /// Creates the layout for the collection.
    open func createCollectionViewLayout() -> UICollectionViewLayout {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
        return layout
    }

    /// Creates the collection view.
    open func createCollectionView() -> CollectionView {
        let collection = CollectionView(frame: .zero, collectionViewLayout: createCollectionViewLayout())
        collection.dataSource = self
        collection.delegate = self
        return collection
    }

    deinit {
        collectionView?.delegate = nil
        collectionView?.dataSource = nil
    }
}

extension ProgrammaticCollectionViewController: UICollectionViewDataSource {
    open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 0
    }
    
    open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return UICollectionViewCell()
    }
    
    open func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
}

extension ProgrammaticCollectionViewController: UICollectionViewDelegate {
}
