using NEIntelligentControl2.Models.Pages;
using NEIntelligentControl2.Models.Station;
using NEIntelligentControl2.Models.User;
using NEIntelligentControl2.Service.User;
using NEIntelligentControl2.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace NEIntelligentControl2.Views.MainWindow
{
    /// <summary>
    /// 标题栏
    /// </summary>
    public partial class TitleBar : UserControl
    {
        private List<StationInfo> itemsSource;
        /// <summary>
        /// 标题数据
        /// </summary>
        public List<StationInfo> ItemsSource { get=> itemsSource; set { itemsSource = value; _LBMain.ItemsSource = value; _LBMain.SelectedIndex = 0; } }

        private UserManager _UserManager;
        private TagManager _TagManager;
        public TitleBar()
        {
            InitializeComponent();
            _UserManager = App.ServiceProvider.GetService(typeof(UserManager)) as UserManager;
            _TagManager = App.ServiceProvider.GetService(typeof(TagManager)) as TagManager;
            _TagManager.TagChanged = TagChanged;
            _TagManager.ChangeTag = ChangeTag;
        }

        private void ChangeTag(string obj)
        {
            var v = ItemsSource.Where(i => i.Id == obj).FirstOrDefault();
            if (v == null) return;
            _LBMain.SelectedItem = v;
            _TagManager.TagSwitched?.Invoke(v);
        }

        /// <summary>
        /// 标签内容改变
        /// </summary>
        /// <param name="obj"></param>
        private void TagChanged(List<StationInfo> obj)
        {
            this.ItemsSource = obj;
        }

        private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _PUser.IsOpen = false;
            switch (((FrameworkElement)sender).Tag)
            {
                case "close":// 关闭应用程序
                    CloseApp();
                    break;
                case "user":
                    UserClick();
                    break;
                case "useradd":
                    _UserManager.UserAdd();
                    break;
                case "useredit":
                    _UserManager.UserEdit();
                    break;
                case "logout":
                    Logout();
                    break;
                default: return;
            }
        }

        /// <summary>
        /// 注销用户
        /// </summary>
        private void Logout()
        {
            _UserManager.Logout();
            _TBUser.Text = "未登录";
        }

        private void UserClick()
        {
            if (_UserManager.IsLogined)
            {
                _PUser.IsOpen = !_PUser.IsOpen;
            }
            else
            {
                UserWindow uw = App.ServiceProvider.GetService(typeof(UserWindow)) as UserWindow;
                bool b = uw.Show("User.PageFingerprint");
                if (b)
                {
                    _TBUser.Text = _UserManager.User.Name;
                }
            }
        }

        /// <summary>
        /// 关闭程序
        /// </summary>
        private void CloseApp()
        {
            var v = MessageWindow.ShowMessage("是否退出系统？");
            if (!v) return;
            Application.Current.Shutdown();
        }

        private void Grid_MouseLeave(object sender, MouseEventArgs e)
        {
            _PUser.IsOpen = false;
        }

        private void _LBMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count <= 0) return;
            var si = e.AddedItems[0] as StationInfo;
            if (si == null) return;
            _TagManager.TagSwitched?.Invoke(si);
        }
    }
}
