using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Web;
using System.Web.Mvc;
using TesteUrlExterna.Models;

namespace TesteUrlExterna.Controllers
{
  public class HomeController : Controller
  {
    const string urlGetSession = "http://bhd050100222:8021/rm/api/TOTVSEducacional/GetSession?key={0}";

    public ActionResult Index()
    {
      return View();
    }

    public ActionResult About()
    {
      ViewBag.Message = "Your application description page.";

      return View();
    }

    public ActionResult Contact()
    {
      ViewBag.Message = "Your contact page.";

      return View();
    }

    public ActionResult URLExternaEdu(string context, string key)
    {
      string retorno = string.Empty;
      string url = String.Format(urlGetSession, key);

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
      request.AutomaticDecompression = DecompressionMethods.GZip;

      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
      using (Stream stream = response.GetResponseStream())
      using (StreamReader reader = new StreamReader(stream))
      {
        retorno = reader.ReadToEnd();
      }

      ContextoEducacionalJson contexto = JsonConvert.DeserializeObject<ContextoEducacionalJson>(retorno);

      ViewBag.ContextoEducacional = contexto.data[0];

      return View();
    }
  }
}