using NEIntelligentControl2.Models.Messages;
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 PageLogin : Page, IPageMessage
    {

        private string _VerifyCode;          // 验证码

        public Action<object> OnMessage { get; set; }
        private UserManager _UserManager;
        public PageLogin(UserManager um)
        {
            InitializeComponent();
            _UserManager = um;
            Init();
        }

        private void Init()
        {
            InitVerifyCode();
        }

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            OnMessage?.Invoke("User.PageFingerprint");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            switch (((Control)sender).Tag)
            {
                case "login":// 登录
                    Login();
                    break;
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        private void Login()
        {
            _TBTitle.Text = "";
            if (string.IsNullOrWhiteSpace(_TBPassword.Password) || string.IsNullOrWhiteSpace(_TBUserName.Text))
            {
                _TBTitle.Text = "用户名或密码不能为空！";
                return;
            }
            if (_TBVerifyCode.Text?.ToLower() != _VerifyCode.ToLower())
            {
                _TBTitle.Text = "验证码错误！";
                _TBVerifyCode.Text = "";
                InitVerifyCode();
                return;
            }

            try
            {
                _UserManager.Login(_TBUserName.Text?.Trim(), _TBPassword.Password?.Trim());

                OnMessage?.Invoke("success");
            }
            catch (Exception ex)
            {
                _TBTitle.Text = "登录失败，用户名或密码错误！";
            }
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            InitVerifyCode();
        }

        private void InitVerifyCode()
        {
            //建立Bitmap对象，绘图
            Bitmap bitmap = new Bitmap(178, 44);
            Graphics graph = Graphics.FromImage(bitmap);
            graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, 178, 44);
            Font font = new Font(System.Drawing.FontFamily.GenericSerif, 36, System.Drawing.FontStyle.Bold, GraphicsUnit.Pixel);
            Random r = new Random();
            string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

            StringBuilder sb = new StringBuilder();

            //添加随机的五个字母
            for (int x = 0; x < 4; x++)
            {
                string letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
                sb.Append(letter);
                graph.DrawString(letter, font, new SolidBrush(System.Drawing.Color.Black), 25 + x * 30, r.Next(0, 9));
            }

            //混淆背景
            System.Drawing.Pen linePen = new System.Drawing.Pen(new SolidBrush(System.Drawing.Color.Black), 2);
            for (int x = 0; x < 6; x++)
                graph.DrawLine(linePen, new System.Drawing.Point(r.Next(25, 155), r.Next(0, 43)), new System.Drawing.Point(r.Next(25, 155), r.Next(0, 43)));

            BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            _Image.Source = bs;

            _VerifyCode = sb.ToString();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            _TBUserName.Focus();
        }
    }

    class User
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
}
