//
//  CinemaService.swift
//  Bakalarka
//
//  Created by i332916 on 28/04/2018.
//  Copyright © 2018 i332916. All rights reserved.
//

import Foundation


class CinemaService {
    
    func getCinemas() -> [Cinema] {
        var cinemaArray: [Cinema] = [Cinema]()
        let urlString = "http://127.0.0.1:5000/cinema"
        let url = URL(string: urlString)!
        URLSession.shared.dataTask(with: url) { (data, response, err)
            in
            guard let data = data else {return}
            //            guard let dataAsString = String(data: data, encoding: .utf8) else {return}
            do{
                //                print (dataAsString)
                cinemaArray = try JSONDecoder().decode([Cinema].self, from: data)
            }catch let jsonError{
                print (jsonError)
            }
            }.resume()
        while cinemaArray.count == 0 {
            usleep(1000)
        }
        return cinemaArray
    }
}
