using UnityEngine;
using System.Collections;
using System;

public class NetworkService {
   private const string xmlApi = "http://api.openweathermap.org/data/2.5/weather?q=Chicago,us&mode=xml";

   private bool IsResponseValid(WWW www) {   
      if (www.error != null) {
         Debug.Log("Nieprawidłowe połączenie.");
         return false;
      }
      else if (string.IsNullOrEmpty(www.text)) {
         Debug.Log("Nieprawidłowe dane.");
         return false;
      }
      else {   // Wszystko w porządku.
         return true;
      }
   }

   private IEnumerator CallAPI(string url, Action<string> callback) {
      WWW www = new WWW(url);        
      yield return www;                   
      
      if (!IsResponseValid(www))
         yield break;                
      
      callback(www.text);     
   }

   public IEnumerator GetWeatherXML(Action<string> callback) {
      return CallAPI(xmlApi, callback);   
   }
}
