using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AyaNovaQBI
{
    public partial class auth : Form
    {
        public auth()
        {
            InitializeComponent();
        }

        private void auth_Load(object sender, EventArgs e)
        {
            edServerUrl.Text = Properties.Settings.Default.serverurl;
        }

        private async void btnTest_Click(object sender, EventArgs e)
        {
            if (!ValidateAndCleanServerAddress()) return;
            btnLogin.Enabled = btnTest.Enabled = false;
            var result = await util.InitAndConfirmAddressAsync(edServerUrl.Text);
            btnLogin.Enabled = btnTest.Enabled = true;
            if (result == "OK")
            {
                MessageBox.Show("Server URL is GOOD!");
            }
            else
            {
                MessageBox.Show("Server could not be reached at that URL\n" + result);
            }
        }

        private async void btnLogin_Click(object sender, EventArgs e)
        {
            if (!ValidateAndCleanServerAddress()) return;
            btnLogin.Enabled = btnTest.Enabled = false;
            // if (!util.Initialized)
            //{
            var result = await util.InitAndConfirmAddressAsync(edServerUrl.Text);
            if (result != "OK")
            {
                MessageBox.Show("Server could not be reached at that URL\n" + result);
                btnLogin.Enabled = btnTest.Enabled = true;
                return;
            }
            // }
            var res = await util.AuthenticateAsync(edUserName.Text, edPassword.Text);
            if (!res)
            {
                MessageBox.Show("Login failed");
                btnLogin.Enabled = btnTest.Enabled = true;
                this.DialogResult = DialogResult.Cancel;
                return;
            }

            
            btnLogin.Enabled = btnTest.Enabled = true;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private bool ValidateAndCleanServerAddress()
        {
            var serverUrl = edServerUrl.Text;
            Uri u;
            try
            {
                u = new Uri(serverUrl);
                var scheme = u.Scheme;
                var host = u.Host;
                var port = u.Port;

                edServerUrl.Text = scheme + "://" + host + ":" + port + "/" + util.API_BASE_ROUTE;

                //client url for notification default links, help links etc
                if ((scheme == "https" && port == 443) || (scheme == "http" && port == 80))
                    util.GuessClientUrl = scheme + "://" + host + "/";
                else
                    util.GuessClientUrl = scheme + "://" + host + ":" + port + "/";

            }
            catch (Exception ex)
            {
                MessageBox.Show("Server address not valid\n" + ex.Message);
                return false;
            }

            //ok, it's some kind of url, now format it for api access


            return true;
        }


    }
}
