using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using 试验版.socket;

namespace 试验版
{
    public partial class Form2 : Form
    {
        public bool isSelectedpoint = false;
        public dian currentpoint;
        public dian prepoint;
        public Graph graph;
        public Graph graphll;
        public List<dian> connectionpoints;
        public List<dian> results;
        public List<liness> connectlines;

        int choose = 12;

        public List<element> sblist;
        int picknumber = -1;
        int picknumber2 = 0;
        PointF[] points = new PointF[1000];
        PointF[] points2 = new PointF[1000];
        PointF[] points3 = new PointF[1000];
   
        float bb = 0;
        float tt = 0;
        float ttt = 0;
        float scale = 1;
        float scale2 = 1;

        bool pp = false;

        bool ss = false;

        int t = 0;

        Point lastMousePos; // 定义最后的鼠标位置

        
        private List<element> elements = new List<element>();

        Pen Mypen1 = new Pen(Color.Black, 2f);
       
        

        public Form2()
        {
            connectlines = new List<liness>();
            connectionpoints = new List<dian>();
            lastMousePos = Point.Empty; // 初始化最后的鼠标位置
            InitializeComponent();
            sblist = new List<element>(0);
            graph = new Graph();
            graphll = new Graph();
            List<dian> results = new List<dian>();
            List<dian> bfsResult = new List<dian>();

        }

        public List<dian> removedEdges = new List<dian>();
        public List<dian> removedEdges2 = new List<dian>();
        

        private void 发电机ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            //创建Graphics对象
            Graphics GPS = this.CreateGraphics();

            GPS.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            GPS.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;

            //创建黑色pen对象
            Pen Mypen = new Pen(Color.Black, 2f);
            

            element el = new element(GPS);
            el.draw(GPS,Mypen,scale2);
            

            sblist.Add(el);
            connectionpoints.Add(el.dian1);
            el.dian1.sbname = "eldd1";
            graph.AddVertex(el.dian1);
            graphll.AddVertex(el.dian1);

            connectlines.Add(el.dian1.line1);
            
        }

        private void 开关ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //创建Graphics对象
            Graphics GPS = this.CreateGraphics();
            //创建黑色pen对象
            Pen Mypen = new Pen(Color.Black, 2f);

            Break br = new Break(GPS);
            br.draw(GPS, Mypen, scale);

            sblist.Add(br);
            connectionpoints.Add(br.dian1);
            br.dian1.sbname = "brdd1";
            graph.AddVertex(br.dian1);
            graphll.AddVertex(br.dian1);
            connectionpoints.Add(br.dian2);
            br.dian2.sbname = "brdd2";
            graph.AddVertex(br.dian2);
            graphll.AddVertex(br.dian2);

            graph.AddEdge(br.dian1, br.dian2);
            graphll.AddEdge(br.dian1, br.dian2);
        }

        private void 刀闸开关ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //创建Graphics对象
            Graphics GPS = this.CreateGraphics();
            //创建黑色pen对象
            Pen Mypen = new Pen(Color.Black, 2f);

            dbreak bbr = new dbreak(GPS);
            bbr.draw(GPS, Mypen, scale);
            sblist.Add(bbr);
            connectionpoints.Add(bbr.dian1);
            bbr.dian1.sbname = "bbrdd1";
            graph.AddVertex(bbr.dian1);
            graphll.AddVertex(bbr.dian1);
            connectionpoints.Add(bbr.dian2);
            bbr.dian2.sbname = "bbrdd2";
            graph.AddVertex(bbr.dian2);
            graphll.AddVertex(bbr.dian2);

            graph.AddEdge(bbr.dian1, bbr.dian2);
            graphll.AddEdge(bbr.dian1, bbr.dian2);

        }

        private void 母线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //创建Graphics对象
            Graphics GPS = this.CreateGraphics();
            //创建黑色pen对象
            Pen Mypen = new Pen(Color.Black, 2f);

            muxian mx = new muxian(GPS);
            mx.draw(GPS, Mypen, scale);
            sblist.Add(mx);
            connectionpoints.Add(mx.dian1);
            mx.dian1.sbname = "mxdd1";
            graph.AddVertex(mx.dian1);
            graphll.AddVertex(mx.dian1);
            connectionpoints.Add(mx.dian2);
            mx.dian2.sbname = "mxdd2";
            graph.AddVertex(mx.dian2);
            graphll.AddVertex(mx.dian2);

            graph.AddEdge(mx.dian1, mx.dian2);
            graphll.AddEdge(mx.dian1, mx.dian2);

        }


        private void 二绕组变压器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //创建Graphics对象
            Graphics GPS = this.CreateGraphics();
            //创建黑色pen对象
            Pen Mypen = new Pen(Color.Black, 2f);

            erby er = new erby(GPS);
            er.draw(GPS, Mypen, scale);

            sblist.Add(er);
            connectionpoints.Add(er.dian1);
            er.dian1.sbname = "erdd1";
            graph.AddVertex(er.dian1);
            graphll.AddVertex(er.dian1);
            connectionpoints.Add(er.dian2);
            er.dian2.sbname = "erdd2";
            graph.AddVertex(er.dian2);
            graphll.AddVertex(er.dian2);

            graph.AddEdge(er.dian1, er.dian2);
            graphll.AddEdge(er.dian1, er.dian2);
        }

        private void 三绕组变压器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //创建Graphics对象
            Graphics GPS = this.CreateGraphics();
            //创建黑色pen对象
            Pen Mypen = new Pen(Color.Black, 2f);

            triby tri = new triby(GPS);
            tri.draw(GPS, Mypen, scale);
            sblist.Add(tri);
            connectionpoints.Add(tri.dian1);
            tri.dian1.sbname = "tridd1";
            graph.AddVertex(tri.dian1);
            graphll.AddVertex(tri.dian1);
            connectionpoints.Add(tri.dian2);
            tri.dian2.sbname = "tridd2";
            graph.AddVertex(tri.dian2);
            graphll.AddVertex(tri.dian2);
            connectionpoints.Add(tri.dian3);
            tri.dian3.sbname = "tridd3";
            graph.AddVertex(tri.dian3);
            graphll.AddVertex(tri.dian3);

            graph.AddEdge(tri.dian1, tri.dian2);
            graph.AddEdge(tri.dian1, tri.dian3);
            graph.AddEdge(tri.dian2, tri.dian3);

            graphll.AddEdge(tri.dian1, tri.dian2);
            graphll.AddEdge(tri.dian1, tri.dian3);
            graphll.AddEdge(tri.dian2, tri.dian3);
        }


        
        //画图
        private void drawall(Graphics g, Pen Mypen)
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;

            foreach(element i in sblist)
            {
                Mypen.Color = Color.Black;
                if (i.isPowered == true)
                    Mypen.Color = Color.Red;

                i.draw(g, Mypen, 1);
            }

            //画线
            if (connectionpoints.Count != 0)
            {
                List<dian> pp = new List<dian>();
                foreach (var s in connectionpoints)
                {
                    pp.AddRange(graphll.adjList[s]);

                    if (pp.Count != 0)
                    {
                        foreach (var ss in pp)
                        {
                            if((s.fatherelement != ss.fatherelement && s.ispower == true && ss.ispower == true) || 
                                (s.fatherelement != ss.fatherelement && (s.fatherelement.isPowered == true || ss.fatherelement.isPowered == true)))
                            {
                                Pen linepen = new Pen(Color.Red, 1f);
                                s.DrawLine(ss, g, linepen);
                            }
                            else if(s.fatherelement != ss.fatherelement && (s.fatherelement.isPowered == false && ss.fatherelement.isPowered == false))
                            {
                                Pen linepen = new Pen(Color.Black, 1f);
                                s.DrawLine(ss, g, linepen);
                            }
                            
                            
                        }
                        pp.Clear();
                    }

                }

            }

        }

        private void Form2_Paint(object sender, PaintEventArgs e)
        {
            drawall(e.Graphics, Mypen1);
        }


        
        private void Form2_MouseWheel(object sender, MouseEventArgs e)
        {
            float zoomDelta = e.Delta; // 鼠标滚动的增量值，正值表示向上滚动，负值表示向下滚动
            float zoomFactor = 0.1f; // 缩放比例
            

            // 根据鼠标滚动的增量值，确定缩放
            if (zoomDelta > 0)
            {
                scale = 1 + zoomFactor; // 放大
            }
            else
            {
                scale = 1 - zoomFactor; // 缩小
            }

            scale2 = scale;

            // 获取鼠标当前位置
            float mouseX = e.X;
            float mouseY = e.Y;

            foreach (element i in sblist)
            {
                // 计算缩放前的图形中心
                float centerX1 = i.rect.X + i.rect.Width / 2;
                float centerY1 = i.rect.Y + i.rect.Height / 2;
                float centerX2 = i.rect2.X + i.rect2.Width / 2;
                float centerY2 = i.rect2.Y + i.rect2.Height / 2;
                float centerX3 = i.rect5.X + i.rect5.Width / 2;
                float centerY3 = i.rect5.Y + i.rect5.Height / 2;


                // 计算缩放后的图形中心相对于鼠标位置的偏移量
                float offsetX1 = (centerX1 - mouseX) * scale;
                float offsetY1 = (centerY1 - mouseY) * scale;
                float offsetX2 = (centerX2 - mouseX) * scale;
                float offsetY2 = (centerY2 - mouseY) * scale;
                float offsetX3 = (centerX3 - mouseX) * scale;
                float offsetY3 = (centerY3 - mouseY) * scale;

                // 更新图形的实际坐标和大小
                i.rect = new RectangleF
                (
                    mouseX + offsetX1 - i.rect.Width * scale / 2,
                    mouseY + offsetY1 - i.rect.Height * scale / 2,
                    i.rect.Width * scale,
                    i.rect.Height * scale
                );

                i.rect2 = new RectangleF
                (
                    mouseX + offsetX2 - i.rect2.Width * scale / 2,
                    mouseY + offsetY2 - i.rect2.Height * scale / 2,
                    i.rect2.Width * scale,
                    i.rect2.Height * scale
                );

                i.rect5 = new RectangleF
                (
                    mouseX + offsetX3 - i.rect5.Width * scale / 2,
                    mouseY + offsetY3 - i.rect5.Height * scale / 2,
                    i.rect5.Width * scale,
                    i.rect5.Height * scale
                );

            }

            this.Invalidate();
        }
        

        







        private void Form2_MouseDown(object sender, MouseEventArgs e)
        {
            
                int j = 0;
                int m = 0;
                foreach (element i in sblist)
                {
                    if (i.ispick(e.Location))
                    {
                        picknumber = j;

                        float x1 = e.X - sblist[picknumber].rect.X;
                        float y1 = e.Y - sblist[picknumber].rect.Y;
                        float x2 = e.X - sblist[picknumber].rect2.X;
                        float y2 = e.Y - sblist[picknumber].rect2.Y;
                        float x3 = e.X - sblist[picknumber].rect5.X;
                        float y3 = e.Y - sblist[picknumber].rect5.Y;


                        points2[picknumber] = new PointF(x2, y2);
                        points3[picknumber] = new PointF(x3, y3);
                        points[picknumber] = new PointF(x1, y1);
                    }
                    j++;
                }


                foreach (element i in sblist)
                {
                    if (i.ispick(e.Location) && pp)
                    {
                        picknumber2 = m;

                        float x1 = e.X - sblist[picknumber2].rect.X;
                        float y1 = e.Y - sblist[picknumber2].rect.Y;
                        float x2 = e.X - sblist[picknumber2].rect2.X;
                        float y2 = e.Y - sblist[picknumber2].rect2.Y;
                        float x3 = e.X - sblist[picknumber2].rect5.X;
                        float y3 = e.Y - sblist[picknumber2].rect5.Y;


                        points2[picknumber2] = new PointF(x2, y2);
                        points3[picknumber2] = new PointF(x3, y3);
                        points[picknumber2] = new PointF(x1, y1);
                    }
                    m++;
                }

        }

        private void Form2_MouseMove(object sender, MouseEventArgs e)
        {
            
            
                
            
                if (picknumber >= 0)
                {
                    this.Refresh();
                    // 更新图形的位置
                    sblist[picknumber].rect.X = e.X - points[picknumber].X;
                    sblist[picknumber].rect.Y = e.Y - points[picknumber].Y;
                    sblist[picknumber].rect2.X = e.X - points2[picknumber].X;
                    sblist[picknumber].rect2.Y = e.Y - points2[picknumber].Y;
                    sblist[picknumber].rect5.X = e.X - points3[picknumber].X;
                    sblist[picknumber].rect5.Y = e.Y - points3[picknumber].Y;
                    
                    
                }


                if (picknumber2 >= 0 && pp)
                {
                    this.Refresh();
                    
                    foreach (element i2 in sblist)
                    {
                        sblist[picknumber2].rect.X = e.X - points[picknumber2].X;
                        sblist[picknumber2].rect.Y = e.Y - points[picknumber2].Y;
                        sblist[picknumber2].rect2.X = e.X - points2[picknumber2].X;
                        sblist[picknumber2].rect2.Y = e.Y - points2[picknumber2].Y;
                        sblist[picknumber2].rect5.X = e.X - points3[picknumber2].X;
                        sblist[picknumber2].rect5.Y = e.Y - points3[picknumber2].Y;
                    }
                }
        }

        private void Form2_MouseUp(object sender, MouseEventArgs e)
        {

            this.Refresh();
                if (picknumber >= 0)
                {
                    picknumber = -1;
                }

                if (picknumber2 >= 0 && pp)
                { picknumber2 = -1; }
            
            
        }

        private void Form2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int j = 0;
            foreach (element i in sblist)
            {
                if (i.ispick(e.Location))
                {
                    picknumber = j;
                    sblist[picknumber].direction ++;
                    bb = sblist[picknumber].rect.Width;
                    sblist[picknumber].rect.Width = sblist[picknumber].rect.Height;
                    sblist[picknumber].rect.Height = bb;

                    tt = sblist[picknumber].rect2.Width;
                    sblist[picknumber].rect2.Width = sblist[picknumber].rect2.Height;
                    sblist[picknumber].rect2.Height = tt;

                    ttt = sblist[picknumber].rect5.Width;
                    sblist[picknumber].rect5.Width = sblist[picknumber].rect5.Height;
                    sblist[picknumber].rect5.Height = ttt;

                }
                j++;
            }
        }


        private string Datasavepace = Application.StartupPath + "\\sblist.txt";

        public static bool savefiledata(string filepath, List<element> _data, List<dian> _data2, List<liness> _data3)//, bool is overwrite = true)
        {
            if (string.IsNullOrEmpty(filepath))
                return false;
            List<element> _s = new List<element>();
            List<dian> _s2 = new List<dian>();
            List<liness> _s3 = new List<liness>();

            //if(_s == null || _s.Count == 0)
            


            foreach (element _o in _data)
                _s.Add(_o);
            foreach (dian _o in _data2)
                _s2.Add(_o);
           

            if (!Directory.Exists(Path.GetDirectoryName(filepath)))
                Directory.CreateDirectory(Path.GetDirectoryName(filepath));
            if ((!File.Exists(filepath)))
            {
                FileStream _fs = File.Create(filepath);
                _fs.Close();
            }
            try
            {
                FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter write = new StreamWriter(fs);
                foreach (element item in _s)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append(item.sbname + "," + item.rect.X + "," + item.rect.Y + "," + item.rect2.X + "," + item.rect2.Y + "," + item.rect5.X + "," + item.rect5.Y + "," + item.direction + "," + item.rect.Width + "," + item.rect.Height + "," + item.rect2.Width + "," + item.rect2.Height + "," + item.rect5.Width + "," + item.rect5.Height + "," + item.dian1.pos.X + "," + item.dian1.pos.Y + "," + item.dian2.pos.X + "," + item.dian2.pos.Y + "," + item.dian3.pos.X + "," + item.dian3.pos.Y + "," + item.dian1.line1.pos.X + "," + item.dian1.line1.pos.Y + "," + item.dian1.line1.pos2.X + "," + item.dian1.line1.pos2.Y + "," + item.dian2.line1.pos.X + "," + item.dian2.line1.pos.Y + "," + item.dian2.line1.pos2.X + "," + item.dian2.line1.pos2.Y + "," + item.dian3.line1.pos.X + "," + item.dian3.line1.pos.Y + "," + item.dian3.line1.pos2.X + "," + item.dian3.line1.pos2.Y);

                    int jj = 0, rr = 0, oo = 0;

                    // 添加 linkpoints
                    foreach (dian linkpoint in item.dian1.linkpoints)
                    {
                        int j = 0;
                        foreach(dian i in _s2)
                        {
                            if(i == linkpoint)
                            {
                                break;
                            }
                            j++;
                        }
                        sb.Append("," + j);
                        jj++;
                    }
                    foreach (dian linkpoint in item.dian2.linkpoints)
                    {
                        int j = 0;
                        foreach (dian i in _s2)
                        {
                            if (i == linkpoint)
                            {
                                break;
                            }
                            j++;
                        }
                        sb.Append("," + j);
                        rr++;
                    }
                    foreach (dian linkpoint in item.dian3.linkpoints)
                    {
                        int j = 0;
                        foreach (dian i in _s2)
                        {
                            if (i == linkpoint)
                            {
                                break;
                            }
                            j++;
                        }
                        sb.Append("," + j);
                        oo++;
                    }

                    sb.Append("," + jj + "," + rr + "," + oo);

                    write.WriteLine(sb.ToString());


                }


                write.Close();
                fs.Close();

                return true;
            }
            catch { return false; }
        }



        private void 保存文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //keep(sblist);
            savefiledata(Datasavepace, sblist, connectionpoints, connectlines);
            MessageBox.Show("保存成功");
        }


        public class FileDataResult
        {
            public List<element> Elements { get; set; }
            public List<dian> DList { get; set; }
            public List<liness> lines { get; set; }
            public Graph graph { get; set; }
            public Graph graphll { get; set; }
        }


        private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filepath = Application.StartupPath + "\\sblist.txt";
            试验版.Openfile.FileDataResult result = 试验版.Openfile.openfiledata(filepath);

            sblist = result.Elements;
            connectionpoints = result.DList;
            connectlines = result.lines;
            graph = result.graph;
            graphll = result.graphll;

            this.Invalidate();

            //drawall(Mypen1);
            

        }


        
        
        private void 连线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            choose = 12; label1.Text = "操作：连线";
        }

        private void Form2_MouseClick(object sender, MouseEventArgs e)
        {
            switch (choose)
            {
                case 12:
                    {
                        Selectedpoints(e.Location);
                        if (isSelectedpoint)
                        {
                            prepoint = currentpoint;
                            choose = 122;
                            
                        }//选中第一个点，如果触发第一个点，跳转至122
                        break;
                    }
                case 122:
                    {
                        Selectedpoints(e.Location);
                        if (isSelectedpoint)
                        {
                            
                            if (prepoint != currentpoint && prepoint.fatherelement != currentpoint.fatherelement)//判断是否是同一个点以及是否同个元件
                            {
                                
                                //创建Graphics对象
                                 Graphics GPS = this.CreateGraphics();
                                //创建黑色pen对象
                                Pen Mypen = new Pen(Color.Black, 2f);

                                //prepoint.line1 = new liness(prepoint.pos, currentpoint.pos, prepoint, GPS);
                               //currentpoint.line1 = new liness(prepoint.pos, currentpoint.pos, currentpoint, GPS);

                                graph.AddEdge(prepoint, currentpoint);//变色
                                graphll.AddEdge(prepoint, currentpoint);//连线

                               // prepoint.linkpoints.Add(currentpoint);
                                //currentpoint.linkpoints.Add(prepoint);

                                currentpoint = null;
                                prepoint = null;
                                choose = 12;

                                
                                t++;
                                
                            }
                            else if (prepoint.fatherelement == currentpoint.fatherelement)
                            {
                                choose = 12;
                                
                            }
                            
                        }
                        else
                        {
                            choose = 12;
                            
                        }
                        break;
                    }
                case 6:
                    {
                        if(ss)
                        {
                            foreach (var i in sblist)
                            {
                                if (i.ispick(e.Location))
                                {
                                
                                    sblist.Remove(i);

                                    foreach (var ii in i.dian1.linkpoints)
                                    {
                                        ii.linkpoints.Remove(i.dian1);
                                        graphll.RemoveEdge(ii, i.dian1);
                                        graph.RemoveEdge(ii, i.dian1);
                                    }
                                    foreach (var ii in i.dian2.linkpoints)
                                    {
                                        ii.linkpoints.Remove(i.dian2);
                                        graphll.RemoveEdge(ii, i.dian2);
                                        graph.RemoveEdge(ii, i.dian2);
                                    }
                                    foreach (var ii in i.dian3.linkpoints)
                                    {
                                        ii.linkpoints.Remove(i.dian3);
                                        graphll.RemoveEdge(ii, i.dian3);
                                        graph.RemoveEdge(ii, i.dian3);
                                    }

                                    connectionpoints.Remove(i.dian1);
                                    connectionpoints.Remove(i.dian2);
                                    connectionpoints.Remove(i.dian3);
                                    graphll.RemoveVertex(i.dian1);
                                    graphll.RemoveVertex(i.dian2);
                                    graphll.RemoveVertex(i.dian3);

                                    graph.RemoveVertex(i.dian1);
                                    graph.RemoveVertex(i.dian2);
                                    graph.RemoveVertex(i.dian3);

                                    connectlines.Remove(i.dian1.line1);
                                    connectlines.Remove(i.dian2.line1);
                                    connectlines.Remove(i.dian3.line1);

                                    this.Invalidate();
                                    
                                    break;
                                }
                            }
                        }
                        
                        break;
                    }
                case 8:
                    {
                        
                        
                        foreach (element i in sblist)
                        {
                            if (i.sbname == "br")
                            {
                                if (i.ispick(e.Location))
                                {
                                        
                                if (!i.edgesRemoved)//和break类有关
                                {
                                    i.removedEdges.AddRange(graph.adjList[i.dian1]);
                                    i.removedEdges2.AddRange(graph.adjList[i.dian2]);

                                    foreach (var neighbor in i.removedEdges)
                                    {
                                        graph.RemoveEdge(i.dian1, neighbor);
                                    }
                                    foreach (var neighbor in i.removedEdges2)
                                    {
                                        graph.RemoveEdge(i.dian2, neighbor);
                                    }
                                    i.dian1.ispower = false;
                                    i.dian2.ispower = false;
                                    i.edgesRemoved = true;
                                            
                                }
                                else
                                {
                                    foreach (var edge in i.removedEdges)
                                    {
                                        graph.AddEdge(i.dian1, edge);
                                    }

                                    foreach (var edge in i.removedEdges2)
                                    {
                                        graph.AddEdge(i.dian2, edge);
                                    }
                                    i.dian1.ispower = true;
                                    i.dian2.ispower = true;

                                    i.removedEdges.Clear(); // 清空已恢复的边列表
                                    i.removedEdges2.Clear();
                                    i.edgesRemoved = false; // 重置标记，准备下一次移除
                                }
                            }

                        }
                        else if (i.sbname == "bbr")
                        {
                            if (i.ispick(e.Location))
                            {
                                if (!i.edgesRemoved)
                                {
                                    i.removedEdges.AddRange(graph.adjList[i.dian1]);
                                    i.removedEdges2.AddRange(graph.adjList[i.dian2]);

                                    foreach (var neighbor in i.removedEdges)
                                    {
                                        graph.RemoveEdge(i.dian1, neighbor);
                                    }
                                    foreach (var neighbor in i.removedEdges2)
                                    {
                                        graph.RemoveEdge(i.dian2, neighbor);
                                    }
                                    i.edgesRemoved = true;
                                    i.dian1.ispower = false;
                                    i.dian2.ispower = false;

                                }
                                else
                                {
                                    foreach (var edge in i.removedEdges)
                                    {
                                        graph.AddEdge(i.dian1, edge);
                                    }

                                    foreach (var edge in i.removedEdges2)
                                    {
                                        graph.AddEdge(i.dian2, edge);
                                    }

                                    i.dian1.ispower = true;
                                    i.dian2.ispower = true;

                                    i.removedEdges.Clear(); // 清空已恢复的边列表
                                    i.removedEdges2.Clear();
                                    i.edgesRemoved = false; // 重置标记，准备下一次移除
                                }
                            }
                        }

                    }

                    if (power_chooose)
                    {
                        power();
                    }
                    this.Invalidate();


                        
                        
                        
                        break;
                    }
                case 9:
                    {

                        var epp = graph.adjList.ToList();

                        var eppll = graphll.adjList.ToList();

                        for (int i = 0; i < epp.Count; i++)
                        {
                            var kpv = epp[i];
                            dian startPoint = kpv.Key;
                            List<dian> connectedPoints = kpv.Value;

                            for (int j = 0; j < connectedPoints.Count; j++)
                            {
                                if (IsPointNearLine(e.Location, startPoint, connectedPoints[j]))
                                {
                                    if (startPoint.fatherelement != connectedPoints[j].fatherelement)
                                    {
                                        graph.RemoveEdge(startPoint, connectedPoints[j]);
                                    }
                                }
                            }
                        }

                        for (int i = 0; i < eppll.Count; i++)
                        {
                            var kpv = eppll[i];
                            dian startPoint = kpv.Key;
                            List<dian> connectedPoints = kpv.Value;

                            for (int j = 0; j < connectedPoints.Count; j++)
                            {
                                if (IsPointNearLine(e.Location, startPoint, connectedPoints[j]))
                                {
                                    if (startPoint.fatherelement != connectedPoints[j].fatherelement)
                                    {
                                        graphll.RemoveEdge(startPoint, connectedPoints[j]);
                                    }
                                }
                            }
                        }

                        this.Invalidate();



                        break;


                        //break;
                    }
            }
            
        }



        public void Selectedpoints(Point e)
        {
            foreach (dian n in connectionpoints)
            {
                if (n.IsSelect(e))
                {
                    currentpoint = n;
                    isSelectedpoint = true;
                    return;
                }
            }
            isSelectedpoint = false;
  
        }


        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.W)
            {

                foreach (element i in sblist)
                {

                    i.rect.Y = i.rect.Y - 4;
                    i.rect2.Y = i.rect2.Y - 4;
                    i.rect5.Y = i.rect5.Y - 4;
                }
            }
            else if (e.KeyCode == Keys.A)
            {

                foreach (element i in sblist)
                {

                    i.rect.X = i.rect.X - 4;
                    i.rect2.X = i.rect2.X - 4;
                    i.rect5.X = i.rect5.X - 4;
                }
            }
            else if (e.KeyCode == Keys.D)
            {

                foreach (element i in sblist)
                {

                    i.rect.X = i.rect.X + 4;
                    i.rect2.X = i.rect2.X + 4;
                    i.rect5.X = i.rect5.X + 4;
                }
            }
            else if (e.KeyCode == Keys.S)
            {

                foreach (element i in sblist)
                {

                    i.rect.Y = i.rect.Y + 4;
                    i.rect2.Y = i.rect2.Y + 4;
                    i.rect5.Y = i.rect5.Y + 4;
                }
            }
            this.Refresh();
            this.Invalidate();
        }

        private void 清屏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            choose = 6; label1.Text = "操作：删除（再点一下变为移动）";
            if (ss)
            {
                ss = false; label1.Text = "操作：";
            }
            else
                ss = true;
        }


        private void power()
        {
            foreach (element i in sblist)
            {
                i.isPowered = false;
                i.dian1.ispower = false; 
                i.dian2.ispower = false;
                i.dian3.ispower = false;

            }

            List<dian> results = new List<dian>();
            foreach (element i in sblist)
            {
                if (i.sbname == "el")//从发电机开始遍例
                {
                    List<dian> bfsResult = graph.BFS(i.dian1); // BFS广度优先搜索结果
                    if (bfsResult != null && bfsResult.Count > 0)
                    {
                        results.AddRange(bfsResult); // 将符合条件的结果追加到 results 中
                    }

                }
            }

            foreach (dian i in results)
            {
                i.ispower = true;
                i.fatherelement.isPowered = true;
            }

        }




        public bool power_chooose = false;
        private void 通电ToolStripMenuItem_Click(object sender, EventArgs e)
        {  
            label1.Text = "操作：通电";
            
            if (!power_chooose)
            {
                power_chooose = true; 
            }
            
            power();
            this.Invalidate();

        }
        

        private void 断电ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            label1.Text = "操作：断电";
            //Mypen1.Color = Color.Black;
            choose = 6;
            foreach (element i in sblist)
            {
                i.isPowered = false;
            }

            foreach(dian j in connectionpoints)
            {
                j.ispower = false;
            }
            if (power_chooose)
            {
                power_chooose = false;
            }

            this.Invalidate();
        }


        public bool sss = false;
        private void 开关闭合ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            label1.Text = "操作：开关";
            choose = 8;
            

        }

        private void 删除线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            choose = 9;
        }

       
        private float PointToLineDistance(PointF p, PointF lineStart, PointF lineEnd)
        {
            float A = p.X - lineStart.X;
            float B = p.Y - lineStart.Y;
            float C = lineEnd.X - lineStart.X;
            float D = lineEnd.Y - lineStart.Y;

            float dot = A * C + B * D;
            float lenSq = C * C + D * D;
            float param = (lenSq != 0) ? dot / lenSq : -1;

            float xx, yy;
            if (param < 0)
            {
                xx = lineStart.X;
                yy = lineStart.Y;
            }
            else if (param > 1)
            {
                xx = lineEnd.X;
                yy = lineEnd.Y;
            }
            else
            {
                xx = lineStart.X + param * C;
                yy = lineStart.Y + param * D;
            }

            float dx = p.X - xx;
            float dy = p.Y - yy;
            return (float)Math.Sqrt(dx * dx + dy * dy);
        }


        public bool IsPointNearLine(PointF clickPos, dian p1, dian p2)
        {
            float tolerance = 5.0f;
            // 计算鼠标点击到线的距离是否小于容差
            PointF middlePos = new PointF((p1.pos.X + p2.pos.X) / 2, (p1.pos.Y + p2.pos.Y) / 2);

            if (PointToLineDistance(clickPos, p1.pos, middlePos) <= tolerance)
            {
                return true;
            }
            else if (PointToLineDistance(clickPos, middlePos, new PointF(middlePos.X, p2.pos.Y)) <= tolerance)
            {
                return true;
            }
            else if (PointToLineDistance(clickPos, new PointF(middlePos.X, p2.pos.Y), p2.pos) <= tolerance)
            {
                return true;
            }
            else
                return false;

        }

        private void socketToolStripMenuItem_Click(object sender, EventArgs e)
        {
            serverform socketForm = new serverform();
            clientform clientForm = new clientform(this);
            socketForm.Show();
            clientForm.Show();
        }

        private void 移动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            choose = 100;
        }
    }
}
