using System;
using System.Collections.Generic;
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 GraphDrawer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        SolidColorBrush brush = new SolidColorBrush(Color.FromRgb(0, 0, 0));
        Random rand = new Random();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void generateBtn_Click(object sender, RoutedEventArgs e)
        {
            canvas.Children.Clear();
            Random rand = new Random();
            if (!int.TryParse(vertexBox.Text, out int vertexCount) || !int.TryParse(edgeBox.Text, out int edgeCount))
            {
                MessageBox.Show("Values must be integer", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
            else
            {
                //int canvasWidth = Convert.ToInt32(canvas.ActualWidth)-50;
                //int canvasHeight = Convert.ToInt32(canvas.ActualHeight)-50;
                //Ellipse[] ellipse = new Ellipse[vertexCount];
                //Line[] lines = new Line[edgeCount * vertexCount];
                //Vertex[] vertices = new Vertex[vertexCount];
                //Edge[] edges = new Edge[vertexCount * edgeCount];
                
                //for(int i = 0; i< vertexCount; i++)
                //{
                //    int x = rand.Next(50, canvasWidth);
                //    int y = rand.Next(50, canvasHeight);
                //    vertices[i] = new Vertex(x, y, 0);
                //    DrawVertex(ellipse[i], canvas, vertices[i]);
                //}

                //for (int i = 0; i < vertexCount; i++)
                //{
                //    if (vertices[i].edgeCount < edgeCount)
                //    {
                //        for (int j = 0; j < vertexCount; j++)
                //        {
                //            if (vertices[j].edgeCount < edgeCount)
                //            {
                //                edges[i] = new Edge(lines[i], vertices[j], vertices[i]);
                //                vertices[i].edgeCount++;
                //                vertices[j].edgeCount++;
                //                DrawEdge(canvas, lines[i], edges[i]);
                //            }
                //        }
                //    }
                //}

                //for (int i = 0; i < vertexCount; i++)
                //{
                //    for (int j = i; j < vertexCount; j++ )
                //    {
                //        if (vertices[j].edgeCount <= edgeCount && vertices[i].edgeCount<=edgeCount)
                //        {
                //            edges[i] = new Edge(lines[i], vertices[j], vertices[i]);
                //            vertices[j].edgeCount++;
                //            vertices[i].edgeCount++;
                //            DrawEdge(canvas, lines[i], edges[i]);
                //        }
                //    }
                //}
            }
        }

        public void DrawEdge(Canvas canvas, Line line, Edge edge)
        {
            line = new Line();
            line.Stroke = GetBrush();
            line.StrokeThickness = 2;
            line.X1 = edge.startVertex.X+5;
            line.Y1 = edge.startVertex.Y+5;
            line.X2 = edge.finishVertex.X+5;
            line.Y2 = edge.finishVertex.Y+5;
            canvas.Children.Add(line);
        }

        public void DrawVertex(Ellipse ellipse, Canvas canvas, Vertex vertex)
        {
            ellipse = new Ellipse();
            ellipse.Height = 10;
            ellipse.Width = 10;
            ellipse.StrokeThickness = 2;
            ellipse.Stroke = brush;
            ellipse.Fill = brush;
            Canvas.SetLeft(ellipse, vertex.X);
            Canvas.SetTop(ellipse, vertex.Y);
            canvas.Children.Add(ellipse);
        }

        public SolidColorBrush GetBrush()
        {
            SolidColorBrush[] brush = new SolidColorBrush[10];
            brush[0] = new SolidColorBrush(Color.FromRgb(235, 64, 52));
            brush[1] = new SolidColorBrush(Color.FromRgb(235, 235, 52));
            brush[2] = new SolidColorBrush(Color.FromRgb(64, 235, 52));
            brush[3] = new SolidColorBrush(Color.FromRgb(52, 235, 229));
            brush[4] = new SolidColorBrush(Color.FromRgb(0, 13, 255));
            brush[5] = new SolidColorBrush(Color.FromRgb(140, 0, 255));
            brush[6] = new SolidColorBrush(Color.FromRgb(255, 0, 230));
            brush[7] = new SolidColorBrush(Color.FromRgb(255, 162, 0));
            brush[8] = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            return brush[rand.Next(0, 8)];
        }

        private void choiceBtn_Click(object sender, RoutedEventArgs e)
        {
            ChoiceMode regime = new ChoiceMode();
            regime.Show();
            Close();
        }
    }
}