using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
 
namespace proekt1
{
    public partial class FRM_GENERAL : Form
    {
        // Объявляем переменные
        public int[] ptk_id; // id марки
        public string[] ptk_name; // имя марки
        public double[] ptk_h; // высота маркиs
        public long[] ptk_objid;
 
        public int[] pt1_id; // id марки
        public string[] pt1_name; // имя марки
        public double[] pt1_h; // высота марки
        public long[] pt1_long;
 
        public int[] pt2_id; // id марки
        public string[] pt2_name; // имя марки
        public double[] pt2_h; // высота марки
        public Color n_color;
 
        public Boolean[] flag_edit;
        
        public FRM_GENERAL()
        {
            InitializeComponent();
        }
 
        private void FRM_GENERAL_Load(object sender, EventArgs e)
        {
            textBox1.Text = "Строка для откладки программы разработчиком";
            textBox2.Text = "70";
            textBox3.Text = "70";
            button1.Tag = "button1";
            button2.Tag = "button2";
            comboBox1.Items.Add("Желтый");
            comboBox1.Items.Add("Красный");
            comboBox1.Items.Add("Синий");
            comboBox1.Items.Add("Зеленый");
            comboBox1.Items.Add("Голубой");
            comboBox2.Items.Add("Желтый");
            comboBox2.Items.Add("Красный");
            comboBox2.Items.Add("Синий");
            comboBox2.Items.Add("Зеленый");
            comboBox2.Items.Add("Голубой");
            button1.Click += new EventHandler(fnc_btn1);
            button2.Click += new EventHandler(fnc_btn1);
 
            n_color = Color.FromArgb(5, 255, 10);
            pictureBox1.BackColor = n_color;
            pictureBox2.BackColor = n_color;
            
 
        }
        public void fnc_select_color(Object sender, EventArgs e)
        {
           
        }
        public void fnc_btn1(object sender, EventArgs e)
        {
            var vr_butt = (Button)sender;
            if(vr_butt.Tag == "button1")
            {
                // проверка введенных даных пользователем!!! НЕ ЗАБЫТЬ
 
                // Код для кнопки нулевого цикла
                // открываем диалоговое окно выбора текстового файла с данными
                openFileDialog1.Filter = "txt files (*.txt) |*.txt";
                openFileDialog1.ShowDialog();
                if (openFileDialog1.FileName == null)
                {
                    return;
                }
                // считываем данные из файла
                FileStream fl1_pth = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                StreamReader fl1_reader = new StreamReader(fl1_pth);
                String txt_read_f1 = fl1_reader.ReadToEnd();
                fl1_reader.Close();
                String[] txt_mas_f1 = txt_read_f1.Split(new Char[]{ '\t', '\n' });
                // определяем размерность массива
                int i = 1;
                int ii = 0;
                int length_mas = txt_mas_f1.Length / 2;
                // инициализируем главные массивы данных
                ptk_id = new int[length_mas];
                ptk_name = new string[length_mas];
                ptk_h = new double[length_mas];
               // ptk_objid = new long[length_mas];
                ptk_objid = new long[length_mas];
                // определяем десятичный разделитель системы
                Char separator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator[0];
                
                Hide(); // скрываем главную форму работаем в автокаде
 
                // Делаем активный слой осадочные марки, если его нет то создаем его
               
                
                foreach (String vr in txt_mas_f1)
                {
                    if (i % 2 == 1)
                    {
                        if (vr == "" || vr == " " || vr == null)
                        {
 
                        }
                        else
                        { 
                        Class1.func_rename_new("осадочные марки", comboBox1.SelectedIndex);
                        ptk_id[ii] = ii;
                        ptk_name[ii] = vr;
                        // создаем окружность осадочной марки на чертеже
                        Class1.func_new_circle(vr, Convert.ToInt16(textBox2.Text));
                        }
                        
                    }else{
                        Class1.func_rename_new("текст осадочной марки", comboBox2.SelectedIndex);
                        string aaa = vr.Replace(',', separator);
                        ptk_h[ii] = Convert.ToDouble(aaa);
                        textBox1.Text += Environment.NewLine;
                        textBox1.Text += "id : " + ptk_id[ii] + " name : " + ptk_name[ii] + " h : " + ptk_h[ii];
                        
                        // создаем текст высотной отметки и получаем данные
                        long vr_data = Class1.func_new_text(ptk_name[ii], vr, Convert.ToInt16(textBox3.Text));
                        // long vr_data = Class1.func_new_text(ptk_name[ii], vr, Convert.ToInt16(textBox3.Text));
                        ptk_objid[ii] = vr_data;
                        ii++;
                        textBox1.Text += Environment.NewLine;
                        textBox1.Text += "vr data : " + vr_data;
                    }
                    i++;
                }
                    // открываем форму
                    Show();
                    // записываем готовую массив
                    saveFileDialog1.Filter = "текстовый файл (*.txt) | *.txt";
                    saveFileDialog1.ShowDialog();
                    if (saveFileDialog1.FileName == null)
                    {
                        return;
                    }
                    FileStream fstream1;
                    if (checkBox1.Checked == true)
                    {
                        fstream1 = new FileStream(saveFileDialog1.FileName, FileMode.Append);
                    }
                    else { 
                        fstream1 = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write);
                    }
                    StreamWriter sw1 = new StreamWriter(fstream1);
                    string vr_for = null;
                    for (int for_i = 0; for_i < length_mas; for_i++)
                    {
                        vr_for = null;
                        
                      //  vr_for = Convert.ToString(ptk_id[for_i]) + "\t" + Convert.ToString(ptk_name[for_i]) + "\t" + Convert.ToString(ptk_h[for_i]) + "\t" + ptk_objid[for_i];
                        vr_for = String.Format("{0}\t{1}\t{2}\t{3}", ptk_id[for_i], ptk_name[for_i], ptk_h[for_i], ptk_objid[for_i]);
                        sw1.WriteLine(vr_for);
                    }
                    sw1.Close();
            }
            if (vr_butt.Tag == "button2")
            {
                openFileDialog1.Filter = "txt files (*.txt) |*.txt";
                openFileDialog1.ShowDialog();
                if (openFileDialog1.FileName == null)
                {
                    return;
                }
                // считываем данные из файла
                FileStream fl1_pth = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                StreamReader fl1_reader = new StreamReader(fl1_pth);
                String txt_read_f1 = fl1_reader.ReadToEnd();
                fl1_reader.Close();
 
                String[] txt_mas_f1 = txt_read_f1.Split(new Char[] { '\t', '\n' });
 
                int i = 1;
                int ii = 0;
 
                pt1_id = new int[txt_mas_f1.Length / 4];
                pt1_h = new double[txt_mas_f1.Length / 4];
                pt1_name = new string[txt_mas_f1.Length / 4];
                pt1_long = new long[txt_mas_f1.Length / 4];
 
                foreach(String vr_data1 in txt_mas_f1)
                {
                    switch (i)
                    {
                        case 1:
                            if (vr_data1 == null || vr_data1=="" || vr_data1==" ")
                            {
 
                            }
                            else { 
                            if (vr_data1 != "") { 
                            pt1_id[ii] = Convert.ToInt16(vr_data1);
                            textBox1.Text += Environment.NewLine + "ok1 = " + vr_data1 + " - " + pt1_id[ii];
                            }
                            }
                            break;
                        case 2:
                            
                            pt1_name[ii] = vr_data1;
                            textBox1.Text += Environment.NewLine + "ok2 = " + vr_data1 + " - " + pt1_name[ii];
                            break;
                        case 3:
                            
                            pt1_h[ii] = Convert.ToDouble(vr_data1);
                            textBox1.Text += Environment.NewLine + "ok3 = " + vr_data1 + " - " + pt1_h[ii];
                            break;
                        case 4:
                            pt1_long[ii] = Int64.Parse(vr_data1);
                                                       
                            textBox1.Text += Environment.NewLine + "ok4 = " + vr_data1 + " - " + pt1_long[ii];
                            
                            i = 0;
 
                            textBox1.Text += Environment.NewLine;
                            ii++;
                            break;
                    }
                    i++;
                }
                openFileDialog1.Filter = "txt files (*.txt) |*.txt";
                openFileDialog1.ShowDialog();
                if (openFileDialog1.FileName == null)
                {
                    return;
                }
                // считываем данные из файла
                FileStream fl2_pth = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                StreamReader fl2_reader = new StreamReader(fl2_pth);
                String txt_read_f2 = fl2_reader.ReadToEnd();
                fl2_reader.Close();
 
                String[] txt_mas_f2 = txt_read_f2.Split(new Char[] { '\t', '\n' });
 
                i = 1;
                ii = 0;
 
                pt2_id = new int[txt_mas_f2.Length / 2];
                pt2_h = new double[txt_mas_f2.Length / 2];
                pt2_name = new string[txt_mas_f2.Length / 2];
                Char separator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator[0];
                string aaa;
                foreach (String vr_data2 in txt_mas_f2)
                {
                    switch (i)
                    {
                        case 1:
                            if (vr_data2 == null || vr_data2=="" || vr_data2==" ")
                            {
 
                            }
                            else
                            {
 
                            textBox1.Text += "case1 ii = " + ii + " i = " + i;
                            pt2_name[ii] = vr_data2;
                            
                            }
                            break;
                        case 2:
                            aaa = vr_data2.Replace(',', separator);
                            pt2_h[ii] = Convert.ToDouble(aaa);
                            // pt2_h[ii] = Convert.ToDouble(vr_data2);
                            textBox1.Text += "case2 ii = " + ii + " i = " + i;
                            i = 0;
                            ii++;
                            break;
                    }
                    i++;
                }
                Double razn = new double();
                flag_edit = new Boolean[pt1_id.Length];
                Hide();
                for (i = 0; i < pt2_name.Length; i++)
                {
                   
                    for (ii = 0; ii < pt1_name.Length; ii++)
                    {
                       
                        if (pt1_name[ii] == pt2_name[i])
                        {
                            flag_edit[ii] = true;
                            textBox1.Text += Environment.NewLine + "yes : " + pt1_name[ii];
                            textBox1.Text += Environment.NewLine + " | " + pt2_h[i] + " | " + pt1_h[ii];
                            razn = Math.Round((pt2_h[i] - pt1_h[ii]) * 1000,2);
                            textBox1.Text += Environment.NewLine + " - text edit - " + Class1.func_edit_text(razn, pt2_name[i], pt1_long[ii]);
                        }
                    }
                }
                for (i = 0; i < pt1_name.Length; i++)
                {
                    if (flag_edit[i] != true & checkBox2.Checked==false)
                    {
                        Class1.func_edit_text(pt1_name[i], pt1_long[i]);
                    }
                }
                Show();
            }
        }
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Color n_color;
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    n_color = Color.FromArgb(255,255,1);
                    break;
                case 1:
                    n_color = Color.FromArgb(255, 1, 1);
                    break;
                case 2:
                    n_color = Color.FromArgb(1, 2, 255);
                    break;
                case 3:
                    n_color = Color.FromArgb(1, 255, 1);
                    break;
                case 4:
                    n_color = Color.FromArgb(1, 255, 255);
                    break;
                default:
                    n_color = Color.FromArgb(5, 255, 10);
                    break;
            }
            ComboBox cmb_vr = (ComboBox)sender;
 
            if (cmb_vr.Tag == "1")
            {
                switch (comboBox1.SelectedIndex)
                {
                    case 0:
                        n_color = Color.FromArgb(255, 255, 1);
                        break;
                    case 1:
                        n_color = Color.FromArgb(255, 1, 1);
                        break;
                    case 2:
                        n_color = Color.FromArgb(1, 2, 255);
                        break;
                    case 3:
                        n_color = Color.FromArgb(1, 255, 1);
                        break;
                    case 4:
                        n_color = Color.FromArgb(1, 255, 255);
                        break;
                    default:
                        n_color = Color.FromArgb(5, 255, 10);
                        break;
                }
                pictureBox1.BackColor = n_color;
            }
            else
            {
                switch (comboBox2.SelectedIndex)
                {
                    case 0:
                        n_color = Color.FromArgb(255, 255, 1);
                        break;
                    case 1:
                        n_color = Color.FromArgb(255, 1, 1);
                        break;
                    case 2:
                        n_color = Color.FromArgb(1, 2, 255);
                        break;
                    case 3:
                        n_color = Color.FromArgb(1, 255, 1);
                        break;
                    case 4:
                        n_color = Color.FromArgb(1, 255, 255);
                        break;
                    default:
                        n_color = Color.FromArgb(5, 255, 10);
                        break;
                }
                pictureBox2.BackColor = n_color;
            }
           
            
 
        }
 
        private void label4_Click(object sender, EventArgs e)
        {
            label4.ForeColor = Color.FromArgb(255, 5, 5);
        }
    }
}