using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Configuration;
using System.Windows;
using System.Windows.Controls;
using WPF_Sample.Common;
using System;
using System.Windows.Documents;

namespace WPF_Sample.Views
{
    /// <summary>
    /// Interaction logic for Configuration.xaml
    /// Configuration Namespace conflict so class name is Settings
    /// </summary>
    public partial class Settings : Page
    {
        public Settings()
        {
            InitializeComponent();
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
            ServiceModelSectionGroup serviceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(configuration);
            ClientSection clientSection = serviceModelSectionGroup.Client;
            var el_1 = clientSection.Endpoints[0];
            txtSoapV1Url.Text = el_1.Address.ToString();

            var el_2 = clientSection.Endpoints[1];
            txtSoapV2Url.Text = el_2.Address.ToString();
        }


        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            LoadSettings();
        }


        public void Save()
        {
            
            Properties.Settings.Default["WSSharedKey"] = txtWSSharedKey.Text;
            Properties.Settings.Default["WSPassword"] = txtWSPassword.Text;
            Properties.Settings.Default["CustomerAlias"] = txtCustomerAlias.Text;
            Properties.Settings.Default["SSOSharedKey"] = txtSSOSharedKey.Text;
            Properties.Settings.Default["WSToken"] = txtToken.Text;

            Properties.Settings.Default.Save();

        }

        private void LoadSettings()
        {
            txtWSSharedKey.Text = Properties.Settings.Default["WSSharedKey"].ToString();
            txtWSPassword.Text = Properties.Settings.Default["WSPassword"].ToString();
            txtCustomerAlias.Text = Properties.Settings.Default["CustomerAlias"].ToString();
            txtSSOSharedKey.Text = Properties.Settings.Default["SSOSharedKey"].ToString();
            txtToken.Text = Properties.Settings.Default["WSToken"].ToString();

        }


        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            Save();
        }

        private void btnHelp_Click(object sender, RoutedEventArgs e)
        {
            HelpViewer helpWin = new HelpViewer();
            helpWin.Show();
            helpWin.LoadHelp("configuration.html", "Configuration");
        }

        private void btnTokenUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Save();
                AppUtil.DoEvents();
                txtToken.Text = "Validating Credentials Please Wait...";
                AppUtil.DoEvents();
                txtToken.Text = ServiceUtil.CreateToken_V2();
            }

              //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)
            {
                txtToken.Text = fe.Detail.Message;

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

            }
            catch (Exception ex)
            {
                string err = "Please check the credentials and be sure the customer account is configured correctly.  If the issue persists contact support...\n";
                err += ex.Message;
                txtToken.Text = err;
            }
        }

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Run run = new Run("Testing...");
                lnkPingTest.Inlines.Clear();
                lnkPingTest.Inlines.Add(run);
                AppUtil.DoEvents();
                using (var client = new STWS_V2.ServiceRouterClient())
                {
                    STWS_V2.PingTestRequest request = new STWS_V2.PingTestRequest();
                    STWS_V2.PingTestResponse response = client.PingTest(request);

                    if (response.PingTestResult == true)
                    {
                        MessageBox.Show("Request to V2 Service Successfull!", "Ping Test", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {
                        MessageBox.Show("Request to V2 Service FAILED!", "Ping Test", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    
                }
                Run run1 = new Run("Ping Test");
                lnkPingTest.Inlines.Clear();
                lnkPingTest.Inlines.Add(run1);
                AppUtil.DoEvents();

            }
            catch (Exception)
            {
                MessageBox.Show("The endpoint is not responding.  Please try again later...", "Ping Test", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            txtToken.Text = "";
        }




    }
}
