using BBWY.Client.Models;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace BBWY.Client.Views.SelectShop
{
    /// <summary>
    /// SelectShopDialog.xaml 的交互逻辑
    /// </summary>
    public partial class SelectShopDialog : UserControl
    {

        public Shop Shop { get; set; }

        public static readonly DependencyProperty DepartmentListProperty = DependencyProperty.Register("DepartmentList", typeof(IList<Department>), typeof(SelectShopDialog));
        public static readonly RoutedEvent OnShopChangedEvent = EventManager.RegisterRoutedEvent("OnShopChanged", RoutingStrategy.Bubble, typeof(EventHandler<OnShopChangedEventArgs>), typeof(SelectShopDialog));

        public IList<Department> DepartmentList
        {
            get { return GetValue(DepartmentListProperty) as IList<Department>; }
            set { SetValue(DepartmentListProperty, value); }
        }

        public event RoutedEventHandler OnShopChanged
        {
            add { this.AddHandler(OnShopChangedEvent, value); }
            remove { this.RemoveHandler(OnShopChangedEvent, value); }
        }

        public SelectShopDialog()
        {
            InitializeComponent();
        }

        private void cbx_department_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var d = cbx_department.SelectedItem as Department;
            cbx_shop.ItemsSource = d.ShopList;
            cbx_shop.SelectedIndex = 0;
        }

        private void btn_ok_Click(object sender, RoutedEventArgs e)
        {
            popup.IsOpen = false;
            Shop = cbx_shop.SelectedItem as Shop;
            txtShopName.Text = Shop.ShopName;
            var args = new OnShopChangedEventArgs(OnShopChangedEvent, this);
            args.SelectedShop = Shop;
            this.RaiseEvent(args);
        }

        private void bd_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            popup.IsOpen = true;
        }
    }

    public class OnShopChangedEventArgs : RoutedEventArgs
    {
        public OnShopChangedEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { }

        public Shop SelectedShop { get; set; }
    }
}