//
//  FirstViewController.swift
//  Bakalarka
//
//  Created by i332916 on 31/03/2018.
//  Copyright © 2018 i332916. All rights reserved.
//

import UIKit

class FirstViewController: UICollectionViewController {
    fileprivate let reuseIdentifier = "MovieCell"
    fileprivate let sectionInsets = UIEdgeInsets(top: 20.0, left: 20.0, bottom: 50.0, right: 20.0)
    fileprivate let itemsPerRow: CGFloat = 2

    var movieArray = [Movie]()
    var images = [UIImage]()
    var selectedMovie: MoviePhotoCell?
    @IBOutlet var collection: UICollectionView!
    
    override func viewDidAppear(_ animated: Bool) {
        navigationController?.navigationBar.prefersLargeTitles = true
    }
    override func viewDidLoad() {
       
        super.viewDidLoad()
        
//        let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
//        self.navigationController?.navigationBar.titleTextAttributes = attributes
        navigationController?.navigationBar.prefersLargeTitles = true
        let movieService = MovieService()
        self.movieArray = movieService.getMovies()
        for movie in movieArray{
            let image_url = URL(string: movie.poster!)
            let data = try? Data(contentsOf: image_url!)
            let image = UIImage(data: data!)
            self.images.append(image!)
        }
        collection.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap)))
        
    }
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @objc func tap(sender: UITapGestureRecognizer){
        if let indexPath = self.collectionView?.indexPathForItem(at: sender.location(in: self.collectionView)) {
            let cell = self.collectionView?.cellForItem(at: indexPath)
            let movieCell = cell as! MoviePhotoCell
//            print(movieCell.MovieName.text!)
            selectedMovie = movieCell
            performSegue(withIdentifier: "MovieScreeningSegue", sender: cell)
        } else {
            print("collection view was tapped")
        }
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destinationVC = segue.destination as! MovieViewController;
        destinationVC.movie = selectedMovie
    }
}

// MARK: - UICollectionViewDataSource
extension FirstViewController {
    //1
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    //2
    override func collectionView(_ collectionView: UICollectionView,
                                 numberOfItemsInSection section: Int) -> Int {
        return images.count
    }
    
    //3
    override func collectionView(_ collectionView: UICollectionView,
                                 cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //1
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
                                                      for: indexPath) as! MoviePhotoCell
        //2
        let moviePoster = images[(indexPath as NSIndexPath).row]
        //3
        cell.MoviePoster.image = moviePoster
//        cell.MovieName.text = self.movieArray[(indexPath as NSIndexPath).row].name
//        cell.MovieName.textColor = UIColor.black
        cell.movie = self.movieArray[(indexPath as NSIndexPath).row]
        return cell
    }
}

extension FirstViewController : UICollectionViewDelegateFlowLayout {
    //1
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        //2
        let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
        let availableWidth = view.frame.width - paddingSpace
        let widthPerItem = availableWidth / itemsPerRow
        
        return CGSize(width: widthPerItem, height: widthPerItem * 1.4)
    }
    
    //3
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt section: Int) -> UIEdgeInsets {
        return sectionInsets
    }
    
    // 4
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return sectionInsets.left
    }
}

