using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Windows;
using System.Windows.Controls;
using WPF_Sample.Common;
using WPF_Sample.STWS;
using System.Diagnostics;

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

        #region Constructor

        public TimeCard()
        {
            InitializeComponent();
            userDataGrid.MaxHeight = 117;
            var myDate = DateTime.Now;
            var newDate = myDate.AddYears(-1);

            txtStartDate.SelectedDate = newDate;
            txtEndDate.SelectedDate = DateTime.Today;

        }

        #endregion

        #region Private Methods

        private void loadUserDataGrid(IEnumerable<object> items)
        {
            userDataGrid.ItemsSource = items;
            if (userDataGrid.Items.IsEmpty)
            {
                borderNoRecords.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                borderNoRecords.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        private void loadTimeDataGrid(IEnumerable<object> items)
        {
            timeDataGrid.ItemsSource = items;
            if (timeDataGrid.Items.IsEmpty)
            {
                lblRecordCount.Content = "(0)";
                borderNoRecordsTime.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                lblRecordCount.Content = String.Format("({0})", timeDataGrid.Items.Count.ToString());
                borderNoRecordsTime.Visibility = System.Windows.Visibility.Hidden;
            }
        }



        #endregion

        #region Private WS EXAMPLES

        /// <summary>
        /// Use this sample of code to call the UserInfoGetAll web service
        /// </summary>
        private void userInfoGetAll()
        {
            try
            {
                using (var client = new STWS.ServiceRouterClient())
                {
                    STWS.UserInfoGetAllRequest request = new STWS.UserInfoGetAllRequest();
                    STWS.SingleToken sToken = new SingleToken();

                    sToken.AuthToken = Properties.Settings.Default["WSToken"].ToString();
                    request.Token = sToken;
                    STWS.UserInfoGetAllResponse response = client.UserInfoGetAll(request);
                    //Load Grid
                    loadUserDataGrid(response.UserInfoGetAllResult);
                }
            }
            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);
            }
        }


        private void TimeGetPunchesByEmpIdentifier(TimeGetPunchesParamsIn paramIn)
        {
            try
            {

                using (var client = new STWS.ServiceRouterClient())
                {
                    STWS.TimeGetPunchesByEmpIdentifierRequest request = new STWS.TimeGetPunchesByEmpIdentifierRequest();
                    request.param = paramIn;
                    request.param.AuthToken = Properties.Settings.Default["WSToken"].ToString();
                    STWS.TimeGetPunchesByEmpIdentifierResponse response = client.TimeGetPunchesByEmpIdentifier(request);
                    //Load Grid
                    loadTimeDataGrid(response.TimeGetPunchesByEmpIdentifierResult);
                }
            }
            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

        #region Private Event Methods

        private void btnGetAll_Click(object sender, RoutedEventArgs e)
        {
            btnGetAll.Content = "Loading...";
            AppUtil.DoEvents();
            userInfoGetAll();
            btnGetAll.Content = "Show All";
        }

        private void btnGetTime_Click(object sender, RoutedEventArgs e)
        {
            var items = userDataGrid.SelectedItems;

            TimeGetPunchesParamsIn paramIn = new TimeGetPunchesParamsIn();

            paramIn.StartDate = (txtStartDate.SelectedDate.HasValue == true) ? txtStartDate.SelectedDate.Value : DateTime.Now;
            paramIn.EndDate = (txtEndDate.SelectedDate.HasValue == true) ? txtEndDate.SelectedDate.Value : DateTime.Now;
            paramIn.EmpIdentifierList = new List<string>();

            foreach (var item in items)
            {

                
                if (item != null)
                {
                    var id = (item as UserInfoBaseModel).EmpIdentifier;
                    paramIn.EmpIdentifierList.Add(id);
                }
            }

            Stopwatch sw = new Stopwatch();
            sw.Start();
            lblProcessingTime.Content = "Loading...  Please Wait...";
            AppUtil.DoEvents();

            TimeGetPunchesByEmpIdentifier(paramIn);

            sw.Stop();
            lblProcessingTime.Content = String.Format("Procces in: {0} seconds", sw.Elapsed.TotalSeconds);


        }
        
        #endregion

    }


}
