using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ServiceModel;

namespace WPF_Sample.Views.Examples
{
    /// <summary>
    /// Interaction logic for UserInfoAdd.xaml
    /// </summary>
    public partial class UserInfoAdd : Window
    {
        public UserInfoAdd()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {

            if (String.IsNullOrWhiteSpace(txtFirstName.Text))
            {
                MessageBox.Show("Please provide a valid First Name.", "Data Exception", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            if (String.IsNullOrWhiteSpace(txtLastname.Text))
            {
                MessageBox.Show("Please provide a valid Last Name.", "Data Exception", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            userInfoAdd();

        }
    


        #region Private WS Example Methods

        /// <summary>
        /// Use this sample of code to call the userInfoAdd web service
        /// </summary>
        private void userInfoAdd()
        {
            try
            {
                using (var client = new STWS.ServiceRouterClient())
                {
                    //Create User
                    STWS.UserInfoAddRequest addRequest = new STWS.UserInfoAddRequest();
                    addRequest.user = new STWS.UserInfoModel
                    {
                        AuthToken = Properties.Settings.Default["WSToken"].ToString(),
                        FirstName = txtFirstName.Text,
                        LastName = txtLastname.Text,
                        Address1 = txtAddress1.Text,
                        Address2 = txtAddress2.Text,
                        EmpIdentifier = txtEmployee.Text
                    };
                    STWS.UserInfoAddResponse addResponse = client.UserInfoAdd(addRequest);

                    string userMsg = addResponse.UserInfoAddResult.Message[0];
                    if (addResponse.UserInfoAddResult.Status == STWS.UserInfoActivityStatus.Failed)
                    {
                        string msg = String.Format("User add Error.{0}Repsonse Message: {1}", Environment.NewLine, userMsg);
                        MessageBox.Show(msg, "User Add Message", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        string msg = String.Format("User add successfully.{0}Repsonse Message: {1}", Environment.NewLine, userMsg);
                        MessageBox.Show(msg, "User Add Message", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }

            }
            catch (FaultException<STWS.ServiceException> fe)
            {
                MessageBox.Show(fe.Detail.Message, "Service Exception", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            catch (EndpointNotFoundException)
            {
                MessageBox.Show("The endpoint is not responding.  Please try again later...", "Service Exception", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Service Exception", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        #endregion



        private void QuickFill_Click(object sender, RoutedEventArgs e)
        {
            Random random = new Random();
            int randomNumber = random.Next(0, 99999);

            txtEmployee.Text = String.Format("{0}", randomNumber.ToString());
            txtFirstName.Text = String.Format("FirstNameTest-{0}", randomNumber.ToString());
            txtLastname.Text = String.Format("LastNameTest-{0}", randomNumber.ToString());
            txtAddress1.Text = String.Format("Address1-Test {0}", randomNumber.ToString());
            txtAddress2.Text = String.Format("Address2-Test {0}", randomNumber.ToString());
        
        }



        
    }
}
