//
//  SuperheroViewController.swift
//  flix
//
//  Created by Gerard Recinto on 11/24/17.
//  Copyright © 2017 Gerard Recinto. All rights reserved.
//

import UIKit

class SuperheroViewController: UIViewController, UICollectionViewDataSource {
  
  @IBOutlet weak var collectionView: UICollectionView!
  var movies: [[String: Any]] = []
  
  
  
  override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.dataSource = self
    let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    layout.minimumInteritemSpacing = 5
    layout.minimumLineSpacing = layout.minimumInteritemSpacing
    let cellsPerLine: CGFloat = 3
    let interItemSpacingTotal = layout.minimumInteritemSpacing * (cellsPerLine - 1)
    let width = collectionView.frame.size.width / cellsPerLine - interItemSpacingTotal / cellsPerLine
    layout.itemSize = CGSize(width: width, height: width * 3/2)
    fetchMovies()
    // Do any additional setup after loading the view.
  }
  
  
  func fetchMovies(){
    let url = URL(string: "https://api.themoviedb.org/3/movie/297762/similar?api_key=a07e22bc18f5cb106bfe4cc1f83ad8ed&language=en-US&page=1")!
    let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 10)
    let session = URLSession(configuration: .default, delegate: nil, delegateQueue: OperationQueue.main)
    let task = session.dataTask(with: request) { (data, response, error) in
      // This will run when the network request returns
      if let error = error {
        print(error.localizedDescription)
      } else if let data = data {
        //self.activityIndicator.startAnimating()
        let dataDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
        
        // TODO: Get the array of movies
        let movies = dataDictionary["results"] as! [[String: Any]]
        self.movies = movies
        self.collectionView.reloadData()
        //  self.refreshControl.endRefreshing()
        // TODO: Store the movies in a property to use elsewhere
        for movie in movies {
          let title = movie["title"] as! String
          print(title)
        }
        // TODO: Reload your table view data
        //   self.activityIndicator.stopAnimating()
      }
    }
    task.resume()        // Do any additional setup after loading the view.
  }
  
  
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return movies.count
  }
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PosterCell", for: indexPath) as! PosterCell
    let movie = movies[indexPath.item]
    if let posterPathString = movie[MovieKeys.posterPath] as? String{
      let baseURLString = "https://image.tmdb.org/t/p/w500"
      let posterURL = URL(string: baseURLString + posterPathString)!
      cell.posterImageView.af_setImage(withURL: posterURL)
    }
    return cell
    
  }
  
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  
  
  
  // MARK: - Navigation
  
  // In a storyboard-based application, you will often want to do a little preparation before navigation
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let cell = sender as! UICollectionViewCell
    if let indexPath = collectionView.indexPath(for: cell){
      let movie = movies[indexPath.item]
      let detailViewController = segue.destination as! DetailViewController
      detailViewController.movie = movie
      // Get the new view controller using segue.destinationViewController.
      // Pass the selected object to the new view controller.
    }
  }
  
}
