using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Http;
using System.Net;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {

        static HttpClient loHttp = new HttpClient();
        static string baseUrl = "https://demo.prevengos.com/PrevengosAPI";
        internal ListadoEmpresas moEmpresas = null;

        public Form1()
        {
            InitializeComponent();

            cmdVerEnListview.Enabled = false;
            moEmpresas = null;

            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

            System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => { return true; };

        }

        private void msEstablecerCredenciales() 
        {
            cmdVerEnListview.Enabled = false;
            moEmpresas = null;

            loHttp.DefaultRequestHeaders.Accept.Clear();
            loHttp.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            loHttp.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(txtUsuario.Text + ":" + txtPass.Text)));
        }

        private async void cmdVersion_Click(object sender, EventArgs e)
        {
            msEstablecerCredenciales();

            Cursor.Current = Cursors.WaitCursor;
            Application.UseWaitCursor = true;

            try
            {

                //HttpResponseMessage response = await loHttp.GetAsync("/api/version");
                HttpResponseMessage response = await loHttp.GetAsync(baseUrl + "/api/version");
                HttpContent content = response.Content;
                           
                txtHttpStatusCode.Text = (int)response.StatusCode + " - " + response.StatusCode.ToString() ;

                string result = await content.ReadAsStringAsync();
                result=Newtonsoft.Json.JsonConvert.DeserializeObject<String>(result);

                txtHttpResponse.Text = result;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            Cursor.Current = Cursors.Default;
            Application.UseWaitCursor = false;

        }

        private async void cmdListadoEmpresas_Click(object sender, EventArgs e)
        {
            msEstablecerCredenciales();

            Cursor.Current = Cursors.WaitCursor;
            Application.UseWaitCursor = true;

            try
            {
                string lsUrl = baseUrl + "/api/gestion/empresas";
                string lsFiltrosUrl = "";

                if (chkPaginado.Checked)
                {
                    lsFiltrosUrl += "&pag=" + txtNumPag.Text;
                    lsFiltrosUrl += "&regPorPag=" + txtItemsPorPag.Text;
                }

                if (optEmpTodas.Checked) lsFiltrosUrl += "&estado=0";
                if (optEmpBajas.Checked) lsFiltrosUrl += "&estado=2";

                if (optEmpModuloPrevencion.Checked) lsFiltrosUrl += "&modulo=prevencion";
                if (optEmpModuloMedicina.Checked) lsFiltrosUrl += "&modulo=medicina";
                if (optEmpModuloCAE.Checked) lsFiltrosUrl += "&modulo=cae";

                if (lsFiltrosUrl != "")
                {
                    lsUrl += "?" + lsFiltrosUrl.Substring(1);
                }

                HttpResponseMessage response = await loHttp.GetAsync(lsUrl);
                HttpContent content = response.Content;

                txtHttpStatusCode.Text = (int)response.StatusCode + " - " + response.StatusCode.ToString();

                string result = await content.ReadAsStringAsync();

                if ((int)response.StatusCode == 200)
                {
                    moEmpresas = Newtonsoft.Json.JsonConvert.DeserializeObject<ListadoEmpresas>(result);
                    cmdVerEnListview.Enabled = true;
                }

                txtHttpResponse.Text = result;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            Cursor.Current = Cursors.Default;
            Application.UseWaitCursor = false;
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void cmdCerrar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cmdVerEnListview_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Application.UseWaitCursor = true;

            frmListadoEmpresas loForm = new frmListadoEmpresas();
            loForm.moListadoEmp = moEmpresas;
            loForm.msCargarLista();

            Cursor.Current = Cursors.WaitCursor;
            Application.UseWaitCursor = false;

            loForm.ShowDialog();
            loForm.Dispose();
            loForm = null;

            cmdVerEnListview.Enabled = false;
            txtHttpResponse.Text = "";
            txtHttpStatusCode.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtUsuario.Text = "usuario";
            txtPass.Text = "password";
        }
    }
}
