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.Shapes;

namespace GraphDrawer
{
    /// <summary>
    /// Interaction logic for GraphByMatrix.xaml
    /// </summary>
    public partial class GraphByMatrix : Window
    {
        public GraphByMatrix()
        {
            InitializeComponent();
        }

        private void SubmitBtn_Click(object sender, RoutedEventArgs e)
        {
            ComboBoxItem selectedItem = (ComboBoxItem)ModeSelection.SelectedItem;
            if (selectedItem.Content.ToString() == "Матрица смежности")
            {
                int[,] matrix = CreateMatrix(MatrixTextBox);
                GraphByClick graphByClick = new GraphByClick(matrix, true);
                graphByClick.Show();
                Close();
            }
            if(selectedItem.Content.ToString()=="Матрица инцидентности")
            {
                int[,] matrix = CreateMatrix(MatrixTextBox);
                GraphByClick graphByClick = new GraphByClick(matrix, false);
                graphByClick.Show();
                Close();
            }
        }

        public int[,] CreateMatrix(TextBox MatrixTextBox)
        {
            var line = MatrixTextBox.GetLineText(0);
            line = RemoveEmptyEntries(line);
            var length = line.Length;
            List<string> lines = MatrixTextBox.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
            for (int i = 0; i < lines.Count; i++)
            {
                lines[i] = RemoveEmptyEntries(lines[i]);
            }

            int[,] matrix = new int[lines.Count, length];
            for(int i = 0; i < lines.Count; i++)
            {
                var newLine = lines[i];
                for(int j = 0; j < length; j++)
                {
                    if (newLine[j] == '1')
                        matrix[i, j] = 1;
                    if (newLine[j] == '0')
                        matrix[i, j] = 0;
                }
            }
            

            return matrix;
        }

        public string RemoveEmptyEntries(string line)
        {
            line = line.Replace(",", string.Empty);
            line = line.Replace(", ", string.Empty);
            line = line.Replace(" ", string.Empty);
            line = line.Replace("\0", string.Empty);
            line = line.Replace("\n", string.Empty);
            line = line.Replace("\r", string.Empty);
            return line;
        }
    }
}
