using NEIntelligentControl2.Models;
using NEIntelligentControl2.Models.Pages;
using NEIntelligentControl2.Models.User;
using NEIntelligentControl2.Service.User;
using System;
using System.Collections.Generic;
using System.Drawing;
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.Pages.User
{
    /// <summary>
    /// 用户编辑页面
    /// </summary>
    public partial class PageUserEdit : Page, IPageMessage
    {
        private UserInfo userInfo;
        /// <summary>
        /// 用户信息
        /// </summary>
        public UserInfo UserInfo { get => userInfo ?? (userInfo = new UserInfo()); set => userInfo = value; }
        /// <summary>
        /// 是否是编辑模式
        /// </summary>
        private bool _IsEdit;
        private UserManager _UserManager;
        private int _FingerIndex = 0;
        public PageUserEdit(UserManager um)
        {
            InitializeComponent();
            _UserManager = um;

            Init();
        }

        private void Init()
        {
            _CBMain.ItemsSource = new string[] { "选择指纹", "指纹1", "指纹2", "指纹3", "指纹4", "指纹5", "指纹6", "指纹7", "指纹8", "指纹9", "指纹10" };
            _CBMain.SelectedIndex = 0;

            _gd.DataContext = this.UserInfo;
        }

        public Action<object> OnMessage { get; set; }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            switch (((Control)sender).Tag)
            {
                case "submit":// 提交
                    Submit();
                    break;
                default:return;
            }
        }

        /// <summary>
        /// 提交
        /// </summary>
        private void Submit()
        {
            _TBTitle.Text = "";
            if (string.IsNullOrWhiteSpace(_TBPassword.Password) || string.IsNullOrWhiteSpace(_TBUserName.Text))
            {
                _TBTitle.Text = "用户名或密码不能为空！";
                return;
            }
            this.UserInfo.Password = _TBPassword.Password;
            string message = "";
            if (_IsEdit)
            {
                message = _UserManager.UpdateUser(this.UserInfo);
            }
            else
            {
                message = _UserManager.RegisterUser(this.UserInfo);
            }
            if (message=="success")
            {
                OnMessage?.Invoke(message);
            }
            else
            {
                _TBTitle.Text = message;
            }
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        internal void EditUser(UserInfo user)
        {
            UserInfo = user;
            _IsEdit = true;
            _TBUserName.Text = user.UserName;
            _TBUserName.IsReadOnly = true;
            _TBName.Text = user.Name;
            _Phone.Text = user.Telephone;
            _TBPageTitle.Text = "用户编辑";

            _gd.DataContext = this.UserInfo;
        }

        private void _CBMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_CBMain.SelectedIndex <= 0) return;
            _fi1.IsActive = true;
            _FingerIndex = 0;
        }


        private void InitFingerPoint(byte[] fs)
        {
            int index = _CBMain.SelectedIndex;
            if (index <= 0) return;
            var pt = UserInfo.GetType().GetProperty($"Finger{index}");
            if (pt == null) return;
            pt.SetValue(this.UserInfo, Convert.ToBase64String(fs));
        }

        private void CapturedPicture(Bitmap obj)
        {
            Dispatcher.Invoke(() =>
            {
                switch (_FingerIndex)
                {
                    case 0:
                        _fi1.ShowImage(obj.GetBitmapImage());
                        _fi2.IsActive = true;
                        break;
                    case 1:
                        _fi2.ShowImage(obj.GetBitmapImage());
                        _fi3.IsActive = true;
                        break;
                    case 2:
                        _fi3.ShowImage(obj.GetBitmapImage());
                        break;
                    default:
                        _FingerIndex = 0;
                        return;
                }
                ++_FingerIndex;
            });
        }
    }
}
