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.Navigation;
using System.Windows.Shapes;
using BeWo.ServiceProxy;
using BeWo.Core;
using BeWo.Converter;
using BeWo.View;
using BS.Shared.Core;
using BS.Shared.Extensions;

namespace BeWo.View.Search
{
    /// <summary>
    /// Interaction logic for PersonSearchView.xaml
    /// </summary>
    public partial class SimplePersonSearchView : BeWoView
    {
        public event EventHandler<EventArgs<CompactPersonDC>> PersonSelected;
        private PersonType? _DefaultType = null;
        public SimplePersonSearchView()
        {
            InitializeComponent();
        }

        public SimplePersonSearchView(PersonType pDefaultType)
            : this()
        {
            _DefaultType = pDefaultType;
        }

        //private void OnDataRowMouseDoubleClick(object sender, MouseButtonEventArgs e)
        //{
        //    CompactPersonDC lPerson = ResultGrid.CurrentItem as CompactPersonDC;

        //    if (lPerson != null && PersonSelected != null)
        //        PersonSelected(this, new EventArgs<CompactPersonDC>(lPerson));
        //}

        private void SearchPersons()
        {
            //listPerson.ItemsSource = ServiceFacade.DoCustomerService(s => s.FindPerson(textbox_search.Text, textbox_search.Text, null, null));
            MainContentGrid.DataContext = ServiceFacade.DoCustomerService(s => s.FindPerson(textbox_search.Text, textbox_search.Text, null, null));
        }

        private void button_showAll_Click(object sender, RoutedEventArgs e)
        {
            ShowAll();
        }

        private void ShowAll()
        {
            //listPerson.ItemsSource = ServiceFacade.DoCustomerService(s => s.FindPerson("", "", null, _DefaultType));
            MainContentGrid.DataContext = ServiceFacade.DoCustomerService(s => s.FindPerson("", "", null, _DefaultType));
        }

        public override string Title
        {
            get
            {
                return "Personen-Suche";
            }
        }

        #region Commands

        private void ExcelCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //bool lRes = lClient.PrepareExcelFile((string)e.Parameter, listPerson.GetHeader(), listPerson.GetContent());
            //ProxyFactory.CloseDownloadServiceClient.Close();
        }

        private void ExcelCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = listPerson.Items.Count > 0;
        }

        private void OpenCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            CompactPersonDC lPerson = listPerson.SelectedItem as CompactPersonDC;

            if (lPerson != null && PersonSelected != null)
                PersonSelected(this, new EventArgs<CompactPersonDC>(lPerson));
        }

        private void OpenCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = listPerson.SelectedItem != null;
        }

        #endregion

        private void textbox_search_TextChanged(object sender, TextChangedEventArgs e)
        {
            SearchPersons();
        }
    }
}
