//
//  TweetCollectionViewController.swift
//  DemoApp
//
//  Created by Jaihee Lee on 6/26/17.
//  Copyright © 2017 Twitter. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class TweetCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    var tweetIDs = [String]()
    var tweets = [TWTRTweet]()
    var tweetViews = [TWTRTweetView]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.estimatedItemSize = CGSize(width: self.view.frame.width - 20, height: 100.0)

        // Register cell classes

        let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        collectionView.delegate   = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .white
        collectionView.setCollectionViewLayout(layout, animated: true)
        collectionView.register(TweetCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.view.addSubview(collectionView)

        let client = TWTRAPIClient.withCurrentUser()
        // These are 40 Tweets gathered manually from @TwitterWomen and team list
        tweetIDs = ["887501877864157184", "887422223681699844", "886020352995770368", "886004210528944129", "885551779210907648", "885238919553400832", "884900813998440448", "884500991227207680", "884457185580732416", "880924631531569152", "887413255118897152", "887388673045544960", "887169021908369408", "887106427277291520", "887085114282319872", "887081820688310272", "887044403507769344", "887034086337822720", "886379158783221760", "886362573670555648", "886359162254270464", "886010291095457792", "885920800653561857", "885920657527132160", "885906706630299652", "885891254759825408", "885817132109492225", "885814925062483968", "885732773511680000", "885717018996989953", "885703673296306176", "885671865544450049", "885667255404511232", "885663207913803776", "885642012426358784", "885610141332025344", "885551016053166080", "885402524794052608", "885359270941700097", "885331504708018176","887501877864157184", "887422223681699844", "886020352995770368", "886004210528944129", "885551779210907648", "885238919553400832", "884900813998440448", "884500991227207680", "884457185580732416", "880924631531569152", "887413255118897152", "887388673045544960", "887169021908369408", "887106427277291520", "887085114282319872", "887081820688310272", "887044403507769344", "887034086337822720", "886379158783221760", "886362573670555648", "886359162254270464", "886010291095457792", "885920800653561857", "885920657527132160", "885906706630299652", "885891254759825408", "885817132109492225", "885814925062483968", "885732773511680000", "885717018996989953", "885703673296306176", "885671865544450049", "885667255404511232", "885663207913803776", "885642012426358784", "885610141332025344", "885551016053166080", "885402524794052608", "885359270941700097", "885331504708018176","887501877864157184", "887422223681699844", "886020352995770368", "886004210528944129", "885551779210907648", "885238919553400832", "884900813998440448", "884500991227207680", "884457185580732416", "880924631531569152", "887413255118897152", "887388673045544960", "887169021908369408", "887106427277291520", "887085114282319872", "887081820688310272", "887044403507769344", "887034086337822720", "886379158783221760", "886362573670555648", "886359162254270464", "886010291095457792", "885920800653561857", "885920657527132160", "885906706630299652", "885891254759825408", "885817132109492225", "885814925062483968", "885732773511680000", "885717018996989953", "885703673296306176", "885671865544450049", "885667255404511232", "885663207913803776", "885642012426358784", "885610141332025344", "885551016053166080", "885402524794052608", "885359270941700097", "885331504708018176","887501877864157184", "887422223681699844", "886020352995770368", "886004210528944129", "885551779210907648", "885238919553400832", "884900813998440448", "884500991227207680", "884457185580732416", "880924631531569152", "887413255118897152", "887388673045544960", "887169021908369408", "887106427277291520", "887085114282319872", "887081820688310272", "887044403507769344", "887034086337822720", "886379158783221760", "886362573670555648", "886359162254270464", "886010291095457792", "885920800653561857", "885920657527132160", "885906706630299652", "885891254759825408", "885817132109492225", "885814925062483968", "885732773511680000", "885717018996989953", "885703673296306176", "885671865544450049", "885667255404511232", "885663207913803776", "885642012426358784", "885610141332025344", "885551016053166080", "885402524794052608", "885359270941700097", "885331504708018176","887501877864157184", "887422223681699844", "886020352995770368", "886004210528944129", "885551779210907648", "885238919553400832", "884900813998440448", "884500991227207680", "884457185580732416", "880924631531569152", "887413255118897152", "887388673045544960", "887169021908369408", "887106427277291520", "887085114282319872", "887081820688310272", "887044403507769344", "887034086337822720", "886379158783221760", "886362573670555648", "886359162254270464", "886010291095457792", "885920800653561857", "885920657527132160", "885906706630299652", "885891254759825408", "885817132109492225", "885814925062483968", "885732773511680000", "885717018996989953", "885703673296306176", "885671865544450049", "885667255404511232", "885663207913803776", "885642012426358784", "885610141332025344", "885551016053166080", "885402524794052608", "885359270941700097", "885331504708018176","887501877864157184", "887422223681699844", "886020352995770368", "886004210528944129", "885551779210907648", "885238919553400832", "884900813998440448", "884500991227207680", "884457185580732416", "880924631531569152", "887413255118897152", "887388673045544960", "887169021908369408", "887106427277291520", "887085114282319872", "887081820688310272", "887044403507769344", "887034086337822720", "886379158783221760", "886362573670555648", "886359162254270464", "886010291095457792", "885920800653561857", "885920657527132160", "885906706630299652", "885891254759825408", "885817132109492225", "885814925062483968", "885732773511680000", "885717018996989953", "885703673296306176", "885671865544450049", "885667255404511232", "885663207913803776", "885642012426358784", "885610141332025344", "885551016053166080", "885402524794052608", "885359270941700097", "885331504708018176","887501877864157184", "887422223681699844", "886020352995770368", "886004210528944129", "885551779210907648", "885238919553400832", "884900813998440448", "884500991227207680", "884457185580732416", "880924631531569152", "887413255118897152", "887388673045544960", "887169021908369408", "887106427277291520", "887085114282319872", "887081820688310272", "887044403507769344", "887034086337822720", "886379158783221760", "886362573670555648", "886359162254270464", "886010291095457792", "885920800653561857", "885920657527132160", "885906706630299652", "885891254759825408", "885817132109492225", "885814925062483968", "885732773511680000", "885717018996989953", "885703673296306176", "885671865544450049", "885667255404511232", "885663207913803776", "885642012426358784", "885610141332025344", "885551016053166080", "885402524794052608", "885359270941700097", "885331504708018176","887501877864157184", "887422223681699844", "886020352995770368", "886004210528944129", "885551779210907648", "885238919553400832", "884900813998440448", "884500991227207680", "884457185580732416", "880924631531569152", "887413255118897152", "887388673045544960", "887169021908369408", "887106427277291520", "887085114282319872", "887081820688310272", "887044403507769344", "887034086337822720", "886379158783221760", "886362573670555648", "886359162254270464", "886010291095457792", "885920800653561857", "885920657527132160", "885906706630299652", "885891254759825408", "885817132109492225", "885814925062483968", "885732773511680000", "885717018996989953", "885703673296306176", "885671865544450049", "885667255404511232", "885663207913803776", "885642012426358784", "885610141332025344", "885551016053166080", "885402524794052608", "885359270941700097", "885331504708018176","887501877864157184", "887422223681699844", "886020352995770368", "886004210528944129", "885551779210907648", "885238919553400832", "884900813998440448", "884500991227207680", "884457185580732416", "880924631531569152", "887413255118897152", "887388673045544960", "887169021908369408", "887106427277291520", "887085114282319872", "887081820688310272", "887044403507769344", "887034086337822720", "886379158783221760", "886362573670555648", "886359162254270464", "886010291095457792", "885920800653561857", "885920657527132160", "885906706630299652", "885891254759825408", "885817132109492225", "885814925062483968", "885732773511680000", "885717018996989953", "885703673296306176", "885671865544450049", "885667255404511232", "885663207913803776", "885642012426358784", "885610141332025344", "885551016053166080", "885402524794052608", "885359270941700097", "885331504708018176"]

        SVProgressHUD.show(withStatus: "Loading")
        client.loadTweets(withIDs: tweetIDs) { [weak self] (tweet, error) in
            SVProgressHUD.dismiss()
            if let error = error {
                debugPrint("Error Loading Tweets: \(error.localizedDescription)")
            } else if let tweet = tweet {
                self?.tweets = tweet
                collectionView.reloadData()
            } else {
                debugPrint("An unexpected error has occured");
            }
        }
    }

    // MARK: UICollectionViewDataSource

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tweets.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath)
    }

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if let cell = cell as? TweetCollectionViewCell {
            cell.configure(with: tweets[indexPath.row])
        }
    }

    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if let cell = cell as? TweetCollectionViewCell {
            cell.pauseVideo()
        }
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10;
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10;

    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let bounds = CGSize(width: collectionView.frame.width - 20.0, height: CGFloat.greatestFiniteMagnitude)
        let tweet = tweets[indexPath.row]
        return TweetCollectionViewCell.sizeThatFits(for: bounds, with: tweet)
    }

}
