using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

using BS.Shared.Core;
using BS.Shared.DataContracts.Compact;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;

using BeWo.Core;
using BeWo.Core.Service;
using BeWo.ServiceProxy;
using BeWo.ViewModel;

namespace BeWo.View.Controls
{
    public partial class SingleSupportConceptSelectionControl
    {
        private FlatSupportConceptTreeNodeDC mSelectedSupportConcept;

        public SingleSupportConceptSelectionControl()
        {
            InitializeComponent();
            SearchView.ControlInitialized += SearchView_ControlInitialized;
        }

        void SearchView_ControlInitialized(object sender, EventArgs e)
        {
            if (ControlInitialized != null)
            {
                ControlInitialized(this, new EventArgs());
            }
        }

        public event EventHandler<EventArgs<FlatSupportConceptTreeNodeDC>> ItemSelected;

        public event EventHandler ControlInitialized;

        public List<CompactSupportConceptDC> SupportConceptList
        {
            get { return SearchView.SupportConcepts; }
            set { SearchView.SupportConcepts = value; }
        }

        public FlatSupportConceptTreeNodeDC SelectedItem
        {
            get
            {
                return mSelectedSupportConcept;
            }

            set
            {
                mSelectedSupportConcept = value;
                if (value.SupportConceptTreeNodeDC != null)
                {
                    NameAsLink.Cursor = System.Windows.Input.Cursors.Hand;
                    customerText.TextDecorations = TextDecorations.Underline;
                }

                UpdateControls();
            }
        }

        public string SearchText 
        {
            get
            {
                return SearchView.SearchText;
            }

            set
            {
                SearchView.SearchText = value;
            }
        }

        private void SupportConceptSearchView_ItemSelected(object sender, EventArgs<CompactSupportConceptDC> e)
        {
            popupSupportConcepts.IsOpen = false;
            SetSelectedSupportConcept(e.Data);
        }

        private void SetSelectedSupportConcept(CompactSupportConceptDC dc)
        {
            mSelectedSupportConcept      = SupportConceptService.CreateFlatSupportConceptItem(dc, null);
            NameAsLink.Cursor            = System.Windows.Input.Cursors.Hand;
            customerText.TextDecorations = TextDecorations.Underline;

            UpdateControls();

            if (ItemSelected != null)
            {
                ItemSelected(this, new EventArgs<FlatSupportConceptTreeNodeDC>(mSelectedSupportConcept));
            }
        }

        internal void UpdateControls()
        {
            if (mSelectedSupportConcept == null || mSelectedSupportConcept.SupportConceptTreeNodeDC == null)
            {
                if (img != null) img.Visibility = Visibility.Collapsed;
            }
            else
            {
                img.Visibility = Visibility.Visible;
            }

            if (ItemContent != null)
            {
                ItemContent.DataContext = mSelectedSupportConcept;
            }

            customerText.Text = mSelectedSupportConcept.CustomerName;
        }

        private void btnSelectSupportConcept_Click(object sender, RoutedEventArgs e)
        {
            popupSupportConcepts.IsOpen = true;
        }

        internal void SetSelectedSupportConceptOid(long pOid)
        {
            var items = SearchView.SupportConcepts;

            if (items != null)
            {
                CompactSupportConceptDC found = null;

                foreach (var sc in items.Where(sc => found == null).Where(sc => sc != null && sc.SupportConceptOid == pOid))
                {
                    found = sc;
                }

                if (found != null)
                {
                    SetSelectedSupportConcept(found);
                }
            }
        }

        private void Hyperlink_OnClick(object sender, RoutedEventArgs e)
        {
            if (SelectedItem.SupportConceptTreeNodeDC == null)
            {
                return;
            }

            var selectedCustomer = SelectedItem.SupportConceptTreeNodeDC.Customer;

            VMFactory.CreateCustomerVMAsync(selectedCustomer.CustomerOid,
                cb => this.Dispatch(
                    () =>
                        BeWoUtils.OpenModalViewWindow<CustomerVM, CustomerDC, BeWoView>(cb, null, () => this.Dispatch(
                            delegate
                            {
                                var updatedCustomer = new CompactCustomerDC();

                                ServiceFacade.DoCustomerServiceSync(s => updatedCustomer = s.GetCompactCustomerDC(selectedCustomer.CustomerOid));
                                selectedCustomer = new CompactCustomerDC();
                                selectedCustomer = updatedCustomer;
                                UpdateControls();
                                customerText.Text = updatedCustomer.ToString();

                                BeWoUtils.UpdateAllViews(selectedCustomer);
                            }))
                    ));
        }
    }
}