//
//  MovieViewController.swift
//  Bakalarka
//
//  Created by i332916 on 14/04/2018.
//  Copyright © 2018 i332916. All rights reserved.
//

import UIKit

class MovieViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    
    var movie: MoviePhotoCell?
    @IBOutlet var moviePoster: UIImageView!
    
    @IBOutlet var tableView: UITableView!
    @IBOutlet var lengthLable: UILabel!
    @IBOutlet var ratingLable: UILabel!
    @IBOutlet var genreLable: UILabel!
    
    override func viewDidAppear(_ animated: Bool) {
        navigationController?.navigationBar.prefersLargeTitles = false
    }
    override func viewDidLoad() {
        super.viewDidLoad()
          navigationController?.navigationBar.prefersLargeTitles = false
        self.navigationItem.title = movie!.movie!.name
        self.moviePoster.image = movie?.MoviePoster.image
        genreLable.text?.append(movie!.movie!.genre!)
//        lengthLable.text?.append(movie!.movie!.)
        if let rating = movie!.movie!.ratings{
            ratingLable.text?.append(rating)
        }else{
            ratingLable.text?.append("???")
        }
      
        
        // Do any additional setup after loading the view.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var numberOfRows = 0;
        for screening in movie!.movie!.screening_dates! {
//            numberOfRows = numberOfRows + 1
            numberOfRows = numberOfRows + screening.screenings!.count
        }
        return numberOfRows;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let row = (indexPath as NSIndexPath).row
        var selectedScreening = 0;
        for screeningDates in movie!.movie!.screening_dates!{
            for screening in screeningDates.screenings!{
                if(selectedScreening == row){
                    if let url = screening.screening_buy_link {
                        UIApplication.shared.openURL(URL(string: url)!)
                    }
                }
                selectedScreening = selectedScreening + 1
            }
    }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "screeningCell",for: indexPath) as! ScreeningCell
        var selectedScreening = 0;
        var row = (indexPath as NSIndexPath).row
        for screeningDates in movie!.movie!.screening_dates!{
            for screening in screeningDates.screenings!{
//                if(row == 0){
//                    cell.cinemaLable.text = screeningDates.date
//                    return cell
//                }
                if(selectedScreening == row){
                    cell.cinemaLable.text = screening.cinema_name
                    cell.screeningTimeLable.text = screening.screening_start
                    return cell
                }
                selectedScreening = selectedScreening + 1
            }
        }
        return cell;
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
         let destinationVC = segue.destination as! MovieDetailsViewController;
        destinationVC.movie = movie!.movie!
        destinationVC.moviePoster = self.moviePoster.image;
    }

    /*
    // 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?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

