using System;
using System.Diagnostics;
using System.ServiceModel;
using System.Windows;
using System.Windows.Controls;
using WPF_Sample.Common;

namespace WPF_Sample.Views.Examples
{
    /// <summary>
    /// Interaction logic for SSO.xaml
    /// </summary>
    public partial class SSO : Page
    {

        #region Constructor

        public SSO()
        {
            InitializeComponent();
            radioByEmployee.IsChecked = true;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Used the STWS web service to get an SSOLink by the EmployeeID
        /// </summary>
        private void getByEmployeeID()
        {
            try
            {
                //Create the Reqeust Payload
                string linkSSOUrl;

                var input = new STWS_V2.GetSSOLinkIn()
                {
                    CustomerAlias = Properties.Settings.Default["CustomerAlias"].ToString(),
                    SSOSharedKey = Properties.Settings.Default["SSOSharedKey"].ToString(),
                    EmpIdentifier = txtEmployeeID.Text,
                    Action = "SELECT-EMPID"
                };

                var request = new STWS_V2.GetSSOLinkRequest(input);

                using (var client = new STWS_V2.ServiceRouterClient())
                {
                    var response = client.GetSSOLink(request);
                    linkSSOUrl = response.GetSSOLinkResult.Results.WebURL;
                    txtResponse.Text = linkSSOUrl;
                }
            }
            //The service will throw an exception of FaultException<STWS.ServiceException>
            //Please wrap all service calls with this exception to read the correct exception message
            catch (FaultException<STWS_V2.ServiceException> fe)
            {
                txtResponse.Text = fe.Detail.Message;
            }
            catch (EndpointNotFoundException)
            {
                txtResponse.Text = "The endpoint is not responding.  Please try again later...";

            }

        }

        /// <summary>
        /// Used the STWS web service to get an SSOLink by the LoginID
        /// </summary>
        private void getByLogin()
        {
            try
            {
                //Create the Reqeust Payload
                string linkSSOUrl = string.Empty;
                var ssoRequestParams = new STWS_V2.GetSSOLinkIn()
                {
                    CustomerAlias = Properties.Settings.Default["CustomerAlias"].ToString(),
                    SSOSharedKey = Properties.Settings.Default["SSOSharedKey"].ToString(),
                    LoginID = txtLoginID.Text,
                    LoginPass = txtPassword.Text,
                    Action = "SELECT-LOGIN-FULL"
                };

                //Request the Service
                var request = new STWS_V2.GetSSOLinkRequest(ssoRequestParams);
                using (var client = new STWS_V2.ServiceRouterClient())
                {
                    var response = client.GetSSOLink(request);
                    linkSSOUrl = response.GetSSOLinkResult.Results.WebURL;
                }

                txtResponse.Text = linkSSOUrl;
            }
            //The service will throw an exception of FaultException<STWS.ServiceException>
            //Please wrap all service calls with this exception to read the correct exception message
            catch (FaultException<STWS.ServiceException> fe)
            {
                txtResponse.Text = fe.Detail.Message;

            }
            catch (EndpointNotFoundException)
            {
                txtResponse.Text = "The endpoint is not responding.  Please try again later...";

            }
        }

        private void toggleViewMode()
        {
            bool isValid = radioByEmployee.IsChecked ?? false;
            if (isValid)
            {
                txtEmployeeID.IsEnabled = true;
                txtLoginID.IsEnabled = false;
                txtPassword.IsEnabled = false;
            }
            else
            {
                txtEmployeeID.IsEnabled = false;
                txtLoginID.IsEnabled = true;
                txtPassword.IsEnabled = true;
            }

        }

        #endregion

        #region Private Event Methods

        private void btnGetSSOLink_Click(object sender, RoutedEventArgs e)
        {

            String SSOKey = Properties.Settings.Default["SSOSharedKey"].ToString();

            if (String.IsNullOrEmpty(SSOKey))
            {
                MessageBox.Show("Please provide a valid Single Sign-On Shared Key in the configuration section.", "SSO Test", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            Stopwatch sw = new Stopwatch();
            sw.Start();
            txtResponse.Text = "Loading...  Please Wait...";
            AppUtil.DoEvents();
            if (radioByEmployee.IsChecked == true)
            {
                getByEmployeeID();
            }
            else
            {
                getByLogin();
            }
            sw.Stop();
            lblProcessTime.Content = String.Format("Procces in: {0} seconds", sw.Elapsed.TotalSeconds);
        }

        private void radioByEmployee_Checked(object sender, RoutedEventArgs e)
        {
            toggleViewMode();
        }

        private void radioByLogin_Checked(object sender, RoutedEventArgs e)
        {
            toggleViewMode();
        }

        #endregion


    }
}
