using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace HOZProject.Helpers
{
    public class NewFunMethod
    {
        //Xray谱图生成图片
        /// <summary>
        /// Xray谱图生成图片
        /// </summary>
        /// <param name="analysis_xray">Xray数据</param>
        /// <param name="list_showelementinfo">元素列表</param>
        /// <param name="saveImgPathName">保存路径名称</param>
        /// <returns>Image</returns>
        public void GetXrayToImage(uint[] analysis_xray, List<ShowElementInfo> list_showelementinfo, string saveImgPathName)
        {
            try
            {
                Control_XRayTable control_XRayTable1 = new Control_XRayTable();
                //获取数据后，需要对xraytable设置
                control_XRayTable1.SetXRayShowLineValue(analysis_xray, list_showelementinfo);
                //设置图像
                Bitmap bitmap = new Bitmap(control_XRayTable1.Width, control_XRayTable1.Height);
                Rectangle rectImg = new Rectangle();
                rectImg.Location = control_XRayTable1.Location;
                rectImg.Width = control_XRayTable1.Width;
                rectImg.Height = control_XRayTable1.Height;
                control_XRayTable1.DrawToBitmap(bitmap, rectImg);
                //如要保存请添加保存路径名称参数
                bitmap.Save(saveImgPathName, System.Drawing.Imaging.ImageFormat.Png);
                bitmap.Dispose();
            }
            catch (Exception ex)
            {
                //记录日志
            }
        }

        /// <summary>
        /// BSE标注颗粒位置
        /// </summary>
        /// <param name="bseImgPath">BSE图像所在位置路径参数</param>
        /// <param name="points">分析点位置列表</param>
        /// <param name="drawType">绘制类型 0：点 1：线 2：面</param>
        /// <param name="saveImgPathName">添加分析点位置标注后，图像保存路径</param>
        /// <returns></returns>
        public void GetBseAnalyticalSite(string bseImgPath, List<DrawAnalysePoint> darwAPList, string saveImgPathName)
        {
            try
            {
                //1.BSE图像所在位置路径参数
                System.Drawing.Image img = System.Drawing.Image.FromFile(bseImgPath);
                Bitmap bitmap = new Bitmap(img.Width,img.Height);
                string filePath = bseImgPath;
                //全路径  C:\aaa\bbb\ccc.xml
                string fileFullPath = System.IO.Path.GetFullPath(filePath);
                //所在文件夹路径  C:\aaa\bbb
                string fileDirectoryName = System.IO.Path.GetDirectoryName(filePath);
                //文件名称.后缀名  ccc.xml
                string fileName = System.IO.Path.GetFileName(filePath);
                //后缀名 .xml
                string ExtensionName = System.IO.Path.GetExtension(filePath);
                //2.生成图像绘制对象
                Graphics g = Graphics.FromImage(bitmap);
                //绘制BSE原图
                g.DrawImage(img, 0, 0);
                //编辑绘制图形的线条颜色
                Pen drawPan = new Pen(Color.Red);
                //点标注 + 号 宽高长度
                int WH = 20;
                //字体大小 
                float fontSize = 15;
                Font font = new Font("黑体", fontSize, FontStyle.Bold);
                SolidBrush solidBrush = new SolidBrush(Color.Red);
                //绘制标签 Y轴向上偏移位置
                int drawTitleOffUPLocation = 20;
                //3.绘制类型 0：点 1：线 2：面
                for (int i = 0; i < darwAPList.Count; i++)
                {
                    DrawType drawType = darwAPList[i].DrawType;
                    string title = darwAPList[i].Title;
                    switch (drawType)
                    {
                        case DrawType.Point:
                            int startPointX = darwAPList[i].Point.X - WH;
                            int endPointX = darwAPList[i].Point.X + WH;
                            int startPointY = darwAPList[i].Point.Y - WH;
                            int endPointY = darwAPList[i].Point.Y + WH;
                            //绘制点十字
                            g.DrawLine(drawPan, startPointX, darwAPList[i].Point.Y, endPointX, darwAPList[i].Point.Y);
                            g.DrawLine(drawPan, darwAPList[i].Point.X, startPointY, darwAPList[i].Point.X, endPointY);
                            //绘制标签
                            g.DrawString(title, font, solidBrush, darwAPList[i].Point.X, darwAPList[i].Point.Y- drawTitleOffUPLocation);
                            break;
                        case DrawType.Line:
                            List<Point> points = darwAPList[i].LinePoints;
                            System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
                            //将多点生成绘制路径
                            graphicsPath.AddLines(points.ToArray());
                            g.DrawPath(drawPan, graphicsPath);
                            //绘制标签
                            g.DrawString(title, font, solidBrush, darwAPList[i].LinePoints[0].X, darwAPList[i].LinePoints[0].Y - drawTitleOffUPLocation);
                            break;
                        case DrawType.Rectangle:
                            Rectangle rect = new Rectangle();
                            rect.Location = new Point(darwAPList[i].Point.X, darwAPList[i].Point.Y);
                            rect.Size = darwAPList[i].RectSize;
                            g.DrawRectangle(drawPan, rect);
                            //绘制标签
                            g.DrawString(title, font, solidBrush, darwAPList[i].Point.X, darwAPList[i].Point.Y - drawTitleOffUPLocation);
                            break;
                        default:
                            break;
                    }
                }
                //4.添加分析点位置标注后，图像保存路径
                bitmap.Save(saveImgPathName, System.Drawing.Imaging.ImageFormat.Png);
                g.Dispose();
                bitmap.Dispose();
            }
            catch (Exception ex)
            {
                //记录日志
            }
        }
    }
    public enum DrawType
    {
        Point = 0,
        Line = 1,
        Rectangle=2
    }
    //元素包含信息结构
    public class Periodic
    {
        string _xh;         //序号
        string _yzzl;    //原子重量,因为有标记的重量带有()括号，所以这里先用字符串进行存储
        string _fh;      //符号
        string _zwysm;   //中文元素名
        string _ywm;     //英文名 
        string _sx1;     //属性1
        string _sx2;     //属性2
        string _sx3;     //属性3
        string _fl;      //分类

        /// <summary>
        /// 序号
        /// </summary>
        public string XH
        {
            get { return _xh; }
            set { _xh = value; }
        }
        /// <summary>
        /// 原子重量
        /// </summary>
        public string YZZL
        {
            get { return _yzzl; }
            set { _yzzl = value; }
        }
        /// <summary>
        /// 符号
        /// </summary>
        public string FH
        {
            get { return _fh; }
            set { _fh = value; }
        }
        /// <summary>
        /// 中文元素名
        /// </summary>
        public string ZWYSM
        {
            get { return _zwysm; }
            set { _zwysm = value; }
        }
        /// <summary>
        /// 英文名
        /// </summary>
        public string YWM
        {
            get { return _ywm; }
            set { _ywm = value; }
        }
        /// <summary>
        /// 属性1
        /// </summary>
        public string SX1
        {
            get { return _sx1; }
            set { _sx1 = value; }
        }
        /// <summary>
        /// 属性2
        /// </summary>
        public string SX2
        {
            get { return _sx2; }
            set { _sx2 = value; }
        }
        /// <summary>
        /// 属性3
        /// </summary>
        public string SX3
        {
            get { return _sx3; }
            set { _sx3 = value; }
        }
        /// <summary>
        /// 分类
        /// </summary>
        public string FL
        {
            get { return _fl; }
            set { _fl = value; }
        }
    }

    public class DrawAnalysePoint
    {
        /// <summary>
        /// 标签名称
        /// </summary>
        private string title;
        /// <summary>
        /// 分析点位置
        /// </summary>
        private Point point;
        /// <summary>
        /// 绘制类型 面尺寸
        /// </summary>
        private Size rectSize;
        /// <summary>
        /// 绘制类型：线 点位置
        /// </summary>
        private List<Point> linePoints;
        /// <summary>
        /// 绘制类型 Point：点 Line：线 Rectangle：面
        /// </summary>
        private DrawType drawType;

        public string Title { get => title; set => title = value; }
        public Point Point { get => point; set => point = value; }
        public Size RectSize { get => rectSize; set => rectSize = value; }
        public List<Point> LinePoints { get => linePoints; set => linePoints = value; }
        public DrawType DrawType { get => drawType; set => drawType = value; }
    }
}
