//
//  MovieService.swift
//  Bakalarka
//
//  Created by i332916 on 01/04/2018.
//  Copyright © 2018 i332916. All rights reserved.
//

import Foundation

class MovieService {
    
    func getMovies() -> [Movie] {
        var movieArray: [Movie] = [Movie]()
        let urlString = "http://127.0.0.1:5000/movie"
         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)
                movieArray = try JSONDecoder().decode([Movie].self, from: data)
            }catch let jsonError{
                print (jsonError)
            }
            }.resume()
        while movieArray.count == 0 {
            usleep(1000)
        }
        return movieArray
    }
}
