using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace WebUI.LIB
{
    public class BadLink
    {
        private readonly RequestDelegate _next;
        private readonly IWebHostEnvironment _webHostEnvironment;

        public BadLink(RequestDelegate next, IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            string url = context.Request.Path.Value;
            if (!url.Contains(".jpg"))
            {
                await _next(context);//走正常流程
                return;
            }
            //  访问来源 其他域名等会记录
            string urlReferrer = context.Request.Headers["Referer"];

            if (string.IsNullOrWhiteSpace(urlReferrer))//直接访问
            {
               await _next(context);//走正常流程

                // await this.SetForbiddenImage(context, _webHostEnvironment);//返回404图片
            }
            else if (!urlReferrer.Contains("090"))//非当前域名
            {
                await this.SetForbiddenImage(context, _webHostEnvironment);//返回404图片
            }
            else
            {
                await _next(context);//走正常流程
            }
        }
        /// <summary>
        /// 设置拒绝图片
        /// </summary>
        /// <param name="context"></param>
        /// <param name="webHostEnvironment"></param>
        /// <returns></returns>
        private async Task SetForbiddenImage(HttpContext context, IWebHostEnvironment webHostEnvironment)
        {
            // 获取访问的图片
            string defaultImagePath = webHostEnvironment.WebRootPath + context.Request.Path.Value;// "wwwroot/Document/Img/Forbidden.jpg";
            string path = Path.Combine(Directory.GetCurrentDirectory(), defaultImagePath);
            // 没有图片
            if (!File.Exists(path))
            {
                await _next(context);//走正常流程
            }
            Bitmap bmp = new Bitmap(path);

            Graphics graph = Graphics.FromImage(bmp);

            for (int i = 0; i < 30; i++)
            {
                var c1 = Color.FromArgb(200, 255, 255, 255);
                var c2 = Color.FromArgb(100, 0, 0, 0);
                int x = new Random().Next(0, bmp.Width);int y =  new Random().Next(0, bmp.Height);
                graph.DrawString("小希", new Font("微软雅黑",25), new SolidBrush(c1), new PointF(x-2,y));
                graph.DrawString("小希", new Font("微软雅黑", 25), new SolidBrush(c1), new PointF(x +2, y));
                graph.DrawString("小希", new Font("微软雅黑", 25), new SolidBrush(c1), new PointF(x , y - 2));
                graph.DrawString("小希", new Font("微软雅黑", 25), new SolidBrush(c1), new PointF(x , y+ 2));
                graph.DrawString("小希", new Font("微软雅黑", 25), new SolidBrush(c2), new PointF(x, y));
            }
            MemoryStream ms = null;
            ms = new MemoryStream();
            bmp.Save(ms, ImageFormat.Jpeg);
            byte[] byteImage = new Byte[ms.Length];
            byteImage = ms.ToArray();
            await context.Response.Body.WriteAsync(byteImage, 0, byteImage.Length);
        }
    }
}
