using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Windows.Forms;

using static System.Net.Mime.MediaTypeNames;

namespace _6
{
    public partial class Form1 : Form
    {
        private List<StandartForPDV> standardsPDV = new List<StandartForPDV>();
        private List<StandartForPVV> standardsPVV = new List<StandartForPVV>();
        private List<Segment> segments = new List<Segment>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            standardsPDV.Add(new StandartForPDV("10BASE5", 11.8, 46.5, 169.5, 0.0866, 500));
            standardsPDV.Add(new StandartForPDV("10BASE2", 11.8, 46.5, 169.5, 0.1026, 185));
            standardsPDV.Add(new StandartForPDV("10BASE-T", 15.3, 42.0, 165.0, 0.113, 100));
            standardsPDV.Add(new StandartForPDV("10BASE-FB", 0, 24.0, 0, 0.1, 2000));
            standardsPDV.Add(new StandartForPDV("10BASE-FL", 12.3, 33.5, 156.5, 0.1, 2000));
            standardsPDV.Add(new StandartForPDV("FOIRL", 7.8, 29.0, 152.0, 0.1, 1000));
            standardsPDV.Add(new StandartForPDV("AUI", 0, 0, 0, 0.1026, 50));

            DataGridViewColumn column = new DataGridViewColumn();
            column.HeaderText = "Тип сегмента";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewPDV.Columns.Add(column);
            column = new DataGridViewColumn();
            column.HeaderText = "База левого сегмента, bt";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewPDV.Columns.Add(column);
            column = new DataGridViewColumn();
            column.HeaderText = "База промежуточного сегмента, bt";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewPDV.Columns.Add(column);
            column = new DataGridViewColumn();
            column.HeaderText = "База правого сегмента, bt";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewPDV.Columns.Add(column);
            column = new DataGridViewColumn();
            column.HeaderText = "Задержка среды на 1 м, bt";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewPDV.Columns.Add(column);
            column = new DataGridViewColumn();
            column.HeaderText = "Максимальная длина сегмента, м";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewPDV.Columns.Add(column);

            foreach (StandartForPDV s in standardsPDV)
            {
                if (s.Type != "10BASE-FB")
                    dataGridViewPDV.Rows.Add(s.Type, s.LeftBase, s.IntermediateBase, s.RightBase, s.Delay, s.Length);
                else
                    dataGridViewPDV.Rows.Add(s.Type, "-", s.IntermediateBase, "-", s.Delay, s.Length);
            }


            standardsPVV.Add(new StandartForPVV("10BASE5", 16, 11));
            standardsPVV.Add(new StandartForPVV("10BASE2", 16, 11));
            standardsPVV.Add(new StandartForPVV("10BASE-FB", 0, 2));
            standardsPVV.Add(new StandartForPVV("10BASE-FL", 10.5, 8));
            standardsPVV.Add(new StandartForPVV("10BASE-T", 10.5, 8));
            standardsPVV.Add(new StandartForPVV("FOIRL", 0, 0));
            standardsPVV.Add(new StandartForPVV("AUI", 0, 0));

            column = new DataGridViewColumn();
            column.HeaderText = "Тип сегмента";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewPVV.Columns.Add(column);
            column = new DataGridViewColumn();
            column.HeaderText = "Передающий сегмент, bt";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewPVV.Columns.Add(column);
            column = new DataGridViewColumn();
            column.HeaderText = "Промежуточный сегмент, bt";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewPVV.Columns.Add(column);

            foreach (StandartForPVV s in standardsPVV)
            {
                if (s.Type == "10BASE-FB")
                    dataGridViewPVV.Rows.Add(s.Type, "-", s.Intermediate);
                else if (s.Type != "FOIRL" && s.Type != "AUI")
                    dataGridViewPVV.Rows.Add(s.Type, s.Transmit, s.Intermediate);
            }
            foreach (StandartForPDV s in standardsPDV)
            {
                comboBoxType.Items.Add(s.Type);
            }
            comboBoxType.SelectedIndex = 0;
            comboBoxType_SelectedIndexChanged(comboBoxType, e);

            column = new DataGridViewColumn();
            column.HeaderText = "Сегмент";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewNetStructure.Columns.Add(column);
            column = new DataGridViewColumn();
            column.HeaderText = "Тип сегмента";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewNetStructure.Columns.Add(column);
            column = new DataGridViewColumn();
            column.HeaderText = "Длина сегмента";
            column.CellTemplate = new DataGridViewTextBoxCell();
            dataGridViewNetStructure.Columns.Add(column);

        }

        public StandartForPDV GetStandardPDV(string type)
        {
            return standardsPDV.First(s => type == s.Type);
        }

        public StandartForPVV GetStandardPVV(string type)
        {
            return standardsPVV.First(s => type == s.Type);
        }

        private void comboBoxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            labelMaxLength.Text = "Максимальная длина сегмента: " + GetStandardPDV(comboBoxType.SelectedItem.ToString()).Length;

            numericUpDown1.Maximum = GetStandardPDV(comboBoxType.SelectedItem.ToString()).Length;
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (comboBoxType.SelectedItem == null) return;
            try
            {
                if (numericUpDown1.Value > GetStandardPDV(comboBoxType.SelectedItem.ToString()).Length)
                {
                    MessageBox.Show("Длина сегмента не должна превышать максимально допустимую");
                    return;
                }
                if (numericUpDown1.Value == 0)
                {
                    MessageBox.Show("Проверьте длину сегмента");
                    return;
                }

                Segment segment = new Segment((string)comboBoxType.SelectedItem, Convert.ToDouble(numericUpDown1.Text));
                segments.Add(segment);

                if (segments.Count == 1)
                {
                    dataGridViewNetStructure.Rows.Add("Начальный сегмент", segment.Type, segment.Length);
                }
                else
                {
                    if (segments.Count > 2)
                        dataGridViewNetStructure[0, segments.Count - 2].Value = "Промежуточный сегмент";

                    dataGridViewNetStructure.Rows.Add("Конечный сегмент", segment.Type, segment.Length);
                }
            }
            catch
            {
                MessageBox.Show("Неверно введены данные");
            }

        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            segments.Clear();
            dataGridViewNetStructure.Rows.Clear();
            labelMaxLength.Text = "";
            richTextBoxResult.Text = "Реузльтат:";
        }

        private void buttonResult_Click(object sender, EventArgs e)
        {
            double pdv = 0;
            double pvv = 0;
            foreach (Segment segment in segments)
            {
                if (segments.IndexOf(segment) == 0)
                {
                    pdv += GetStandardPDV(segment.Type).LeftBase;
                    pvv += GetStandardPVV(segment.Type).Transmit;
                }
                else if (segments.IndexOf(segment) == segments.Count - 1)
                {
                    pdv += GetStandardPDV(segment.Type).RightBase;
                }
                else
                {
                    pdv += GetStandardPDV(segment.Type).IntermediateBase;
                    pvv += GetStandardPVV(segment.Type).Intermediate;
                }
                pdv += GetStandardPDV(segment.Type).Delay * segment.Length;
            }

            richTextBoxResult.Text = "PDV = " + pdv + "\nPVV = " + pvv + '\n';

            if (pdv <= 575)
            {
                richTextBoxResult.SelectionStart = richTextBoxResult.TextLength;
                richTextBoxResult.SelectionLength = 0;
                richTextBoxResult.SelectionColor = Color.Green;
                richTextBoxResult.AppendText("PDV < 575.\n");
            }
            else
            {
                richTextBoxResult.SelectionStart = richTextBoxResult.TextLength;
                richTextBoxResult.SelectionLength = 0;
                richTextBoxResult.SelectionColor = Color.Red;
                richTextBoxResult.AppendText("PDV > 575.\n");
            }

            if (pvv <= 49)
            {
                richTextBoxResult.SelectionStart = richTextBoxResult.TextLength;
                richTextBoxResult.SelectionLength = 0;
                richTextBoxResult.SelectionColor = Color.Green;
                richTextBoxResult.AppendText("PVV не превышает 49 bt.\n");
            }
            else
            {
                richTextBoxResult.SelectionStart = richTextBoxResult.TextLength;
                richTextBoxResult.SelectionLength = 0;
                richTextBoxResult.SelectionColor = Color.Red;
                richTextBoxResult.AppendText("PDV превышает 49 bt.\n");
            }

            if (pdv <= 575 && pvv <= 49)
            {
                richTextBoxResult.SelectionStart = richTextBoxResult.TextLength;
                richTextBoxResult.SelectionLength = 0;
                richTextBoxResult.SelectionColor = Color.Green;
                richTextBoxResult.AppendText("///////Сеть работоспособна///////");
                richTextBoxResult.SelectionColor = richTextBoxResult.ForeColor;
            }
            else
            {
                richTextBoxResult.SelectionStart = richTextBoxResult.TextLength;
                richTextBoxResult.SelectionLength = 0;
                richTextBoxResult.SelectionColor = Color.Red;
                richTextBoxResult.AppendText("///////Сеть неработоспособна///////");
                richTextBoxResult.SelectionColor = richTextBoxResult.ForeColor;

            }
        }
    }
}
