//
//  CinemaDetailViewController.swift
//  Bakalarka
//
//  Created by i332916 on 29/04/2018.
//  Copyright © 2018 i332916. All rights reserved.
//

import UIKit

class CinemaDetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  
    
    var cinema: Cinema?
    
    @IBOutlet var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = self.cinema?.name
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var numberOfRows = 0;
        for screening in cinema!.screening_dates! {
            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 cinema!.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;
        for screeningDates in cinema!.screening_dates!{
            for screening in screeningDates.screenings!{
                if(selectedScreening == (indexPath as NSIndexPath).row){
                    cell.cinemaLable.text = screening.film_name
                    cell.screeningTimeLable.text = screening.screening_start
                    return cell
                }
                selectedScreening = selectedScreening + 1
            }
        }
        return cell;
    }

    /*
    // 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.
    }
    */

}
