using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;

namespace ApiTestUygulamasi
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://renklirecete.saglik.gov.tr/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                
                //----------------------------------------------------------------------TOKEN OLUŞTUR---------------------------------------------------------
                TokenOlustur(client).Wait();
                //----------------------------------------------------------------------TOKEN OLUŞTUR---------------------------------------------------------
                
                //----------------------------------------------------------------------REÇETE LİSTE GETİR----------------------------------------------------
                ReceteListeGetir(client).Wait();
                //----------------------------------------------------------------------REÇETE LİSTE GETİR----------------------------------------------------

            }
        }
        
        private static async Task TokenOlustur(HttpClient client)
        {
            var receteToken = new ReceteToken()
            {
                DoktorTc = "11111111111",//11 hane olmak zorunda
                TesisKodu = "11000001",//belirli bir karakter sınır yok
                Sifre = "MTIzNDU2",
                KullaniciAdi = "11900012016"//11 hane olmak zorunda
            };
            HttpResponseMessage res = await client.PostAsJsonAsync("api/receteapi/tokenolustur", receteToken);
            IEnumerable<string> tokens;
            res.Headers.TryGetValues("Token", out tokens);
            Console.WriteLine(tokens.FirstOrDefault());
            Console.WriteLine("Token başarıyla oluşturuldu.");
        }

        private static async Task ReceteListeGetir(HttpClient client)
        {
            var values = new Dictionary<string, string>();
            values.Add("kullaniciAdi", "11900012016");
            values.Add("sifre", "MTIzNDU2");
            values.Add("tesisKodu", "11000001");
            values.Add("baslangic", "01/12/2016");
            values.Add("bitis", "01/01/2017");
            HttpResponseMessage response = await client.GetAsync("api/receteapi/recetelistegetir" + Utilities.BuildUrlParametersString(values));
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            
            //istenirse direk modele cast edilebilir
            var result = JsonConvert.DeserializeObject<ReceteListe>(responseBody);

            Console.WriteLine("Reçete listesi başarıyla alındı.");
            Console.WriteLine(responseBody);
            Console.ReadLine();
        }
    }

}
