private void CreateTransformerPartialVertex(DataVertex dv, double m = 0, double n = 0)
        {
            DataVertexTransformerPartial dvtp = new DataVertexTransformerPartial()
            {
                Text       = dv.Text,
                Element_id = dv.Element_id
            };

            VertexControl vc;

            vc = new VertexControl(dvtp);



            dvtp.typeOfVertex = DataVertex.TypeOfVertex.TRANSFORMER_PARTIAL_VERTEX;



            vc = new VertexControl(dvtp);


            if (m == 0)
            {
                dvtp.Text = "Second transfromer ending: " + dvtp.Element_id;
                vc.SetPosition(new Point(x, y));
            }
            else
            {
                vc.SetPosition(m, n);
            }

            mainWindow.GlobalVertices.Add(dvtp.Element_id, dvtp);

            vc.Background = Brushes.GreenYellow;
            mainWindow.graphView.Area.AddVertexAndData(dvtp, vc, true);

            Point point = CreatePointBasedOn(vc);


            if (graphState == GraphState.NORMAL)
            {
                x += 30;
                y += 30;
            }

            this.dvtp = dvtp;
            return;
        }
        public void PrepareComplexVertexForDelete(DataVertex dv)
        {
            VertexControl vc = GetVertexControlWithDataVertex(dv.Element_id);

            Point p = CreatePointBasedOn(vc);

            DeleteVertex(dv);


            if (dv.typeOfVertex == DataVertex.TypeOfVertex.REGULATOR_VERTEX)
            {
                dvr = (DataVertexRegulator)dv;
                if (dvr.Line_Segment_To != null)
                {
                    if (mainWindow.GlobalVertices.Keys.Contains(dvr.Line_Segment_To))
                    {
                        DeleteVertex(mainWindow.GlobalVertices[dvr.Line_Segment_To]);
                        mainWindow.GlobalVertices.Remove(dvr.Line_Segment_To);
                        dvrp = null;
                    }
                }

                dvr = null;
            }
            else if (dv.typeOfVertex == DataVertex.TypeOfVertex.TRANSFORMER_VERTEX)
            {
                dvt = (DataVertexTransformer)dv;
                if (dvt.Line_to != null)
                {
                    if (mainWindow.GlobalVertices.Keys.Contains(dvt.Line_to))
                    {
                        DeleteVertex(mainWindow.GlobalVertices[dvt.Line_to]);
                        mainWindow.GlobalVertices.Remove(dvt.Line_to);
                        dvtp = null;
                    }
                }

                dvt = null;
            }

            mainWindow.enableButtonIfEdgesExists();
            mainWindow.CloseAllWindows();
        }
        public void DeleteVertex(DataVertex dv)
        {
            //Area.RemoveVertex(globalVertices[dv.Text]);
            mainWindow.GlobalVertices.Remove(dv.Element_id);
            Console.WriteLine(mainWindow.GlobalVertices);
            bool is_empty = true;
            bool has_edge = false;


            is_empty = CheckIfZeroEdgesExist();


            //if 0 edges exist we can remove it
            RemoveIfZeroEdgesExist(is_empty, dv.Element_id);

            //if more then 0 edges exist we look for them
            has_edge = RemoveIfMoreThenZeroEdgesExist(dv);

            //if its not empty and its has edges, its third case, we delete it
            if (!is_empty && !has_edge)
            {
                VertexControl vc = GetVertexControlWithDataVertex(dv.Element_id);

                if (vc == null)
                {
                    return;
                }

                mainWindow.graphView.Area.RemoveVertex((vc.Vertex as DataVertex));
            }


            if (dv.typeOfVertex == DataVertex.TypeOfVertex.REGULATOR_VERTEX)
            {
                dvr = null;

                if (dvrp != null)
                {
                    DeleteVertex(dvrp);
                    dvrp = null;
                }
            }


            if (dv.typeOfVertex == DataVertex.TypeOfVertex.TRANSFORMER_VERTEX)
            {
                dvt = null;
                if (dvtp != null)
                {
                    DeleteVertex(dvtp);
                    dvtp = null;
                }
            }


            //now we look for his edges and delete them
            foreach (string key in mainWindow.GlobalEdges.Keys)
            {
                foreach (DataEdge de in mainWindow.GlobalEdges[key])
                {
                    if (de.Target.Element_id == dv.Element_id)
                    {
                        mainWindow.GlobalEdges[key].Remove(de);

                        if (mainWindow.graphView.Area.EdgesList.ContainsKey(de))
                        {
                            mainWindow.graphView.Area.RemoveEdge(de);
                            break;
                        }
                    }
                }
            }


            if (dv.typeOfVertex == DataVertex.TypeOfVertex.REGULATOR_PARTIAL_VERTEX || dv.typeOfVertex == DataVertex.TypeOfVertex.TRANSFORMER_PARTIAL_VERTEX)
            {
                foreach (DataVertex temp in mainWindow.graphView.Area.VertexList.Keys)
                {
                    if (temp.Element_id == dv.Element_id)
                    {
                        mainWindow.graphView.Area.RemoveVertex(temp);
                        break;
                    }
                }
            }

            if (graphState == GraphState.NORMAL)
            {
                undoRedoCommander.addUndoCommand(new Command("DeleteVertex", dv));
            }
        }
        private void dynamicButton_Click(object sender, RoutedEventArgs e)
        {
            if (!allFilled())
            {
                MessageBox.Show("You have to enter all data");
                return;
            }

            if ((_mn.graphView.DataContext as GraphViewModel).dvtp == null)
            {
                if (globalVertices.ContainsKey(lineTo.Text) && second_component != lineTo.Text)
                {
                    MessageBox.Show("Node with that ID already exists, change Line segment to input.");
                    return;
                }
            }



            DataVertexTransformer dataVertexTransformer = setupDataVertexTransformerProperties();

            DataVertexTransformerPartial dvtp;


            if ((_mn.graphView.DataContext as GraphViewModel).dvtp != null)
            {
                dvtp                  = (_mn.graphView.DataContext as GraphViewModel).dvtp;
                dvtp.Text             = "Second transformer ending: " + lineTo.Text;
                dynamicButton.Content = "Update";
            }
            else
            {
                DataVertexTransformerPartial dataVertexTransformerPartial = new DataVertexTransformerPartial()
                {
                    Text         = "Second transformer ending: " + lineTo.Text,
                    typeOfVertex = DataVertex.TypeOfVertex.TRANSFORMER_PARTIAL_VERTEX,
                    Element_id   = dataVertexTransformer.Line_to,
                    Name         = dataVertexTransformer.NameB,
                    _kVA         = dataVertexTransformer._kVA_B,
                    _kV_High     = dataVertexTransformer._kV_HighB,
                    _kV_Low      = dataVertexTransformer._kV_LowB,
                    RPercentage  = dataVertexTransformer.RPercentageB,
                    XPercentage  = dataVertexTransformer.XPercentageB
                };
                (_mn.graphView.DataContext as GraphViewModel).dvtp = dataVertexTransformerPartial;
                dvtp = dataVertexTransformerPartial;
            }

            dvtp.Element_id = lineTo.Text;

            if (dynamicButton.Content.ToString() == "Update")
            {
                (_mn.graphView.DataContext as GraphViewModel).graphState = GraphViewModel.GraphState.UPDATE;
                (_mn.graphView.DataContext as GraphViewModel).UpdateVertices(dataVertexTransformer, dvtp, last_value);
                (_mn.graphView.DataContext as GraphViewModel).graphState = GraphViewModel.GraphState.NORMAL;
                last_value = dataVertexTransformer.Line_to;
            }
            else
            {
                (_mn.graphView.DataContext as GraphViewModel).CreateComplexVertexWithPartialType(dataVertexTransformer, dvtp, dv);
            }

            _mn.CloseAllWindows();
        }