using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StekloMaster
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }
        string obid = "";
        string putin = "";
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (((TextBox)sender).Text == "" || ((TextBox)sender).Text == null) return;
            InputLanguage myCurrentLanguage = InputLanguage.CurrentInputLanguage;
            if (myCurrentLanguage.Culture.Name != "ru-RU")
            {
                string s = RUSSIAN(((TextBox)sender).Text);
                if (s != ((TextBox)sender).Text)
                {
                    ((TextBox)sender).Text = s;
                    ((TextBox)sender).SelectionStart = s.Length;
                }
            }
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new System.Globalization.CultureInfo("ru-RU"));
            if (textBox1.Text.Length > 4 && ((TextBox)sender) == textBox2)
            {
                string b2 = textBox1.Text.Substring(textBox1.Text.Length - 2);
                if (b2 == "ОВ" || b2 == "ЕВ" || b2 == "ИН" || b2 == "ЫН" || b2 == "ОЙ")
                {
                    if (!radioButton2.Checked)
                        radioButton2.Checked = true;
                }
                else if (b2 == "АЯ" || b2 == "ЯЯ")
                {
                    if (!radioButton1.Checked)
                        radioButton1.Checked = true;
                }
                else
                {
                    string b3 = textBox1.Text.Substring(textBox1.Text.Length - 3);
                    if (b3 == "ОВА" || b3 == "ЕВА" || b3 == "ИНА")
                    {
                        if (!radioButton1.Checked)
                            radioButton1.Checked = true;
                    }
                    else
                    {
                        string b4 = textBox1.Text.Substring(textBox1.Text.Length - 4);
                        if (b4 == "ЦКИЙ" || b4 == "СКИЙ")
                        {
                            if (!radioButton2.Checked)
                                radioButton2.Checked = true;
                        }
                    }
                }
            }
        }

        private string RUSSIAN(string s)
        {
            s = s.Replace('q', 'й');
            s = s.Replace('w', 'ц');
            s = s.Replace('e', 'у');
            s = s.Replace('r', 'к');
            s = s.Replace('t', 'е');
            s = s.Replace('y', 'н');
            s = s.Replace('u', 'г');
            s = s.Replace('i', 'ш');
            s = s.Replace('o', 'щ');
            s = s.Replace('p', 'з');
            s = s.Replace('[', 'х');
            s = s.Replace(']', 'ъ');
            s = s.Replace('a', 'ф');
            s = s.Replace('s', 'ы');
            s = s.Replace('d', 'в');
            s = s.Replace('f', 'а');
            s = s.Replace('g', 'п');
            s = s.Replace('h', 'р');
            s = s.Replace('j', 'о');
            s = s.Replace('k', 'л');
            s = s.Replace('l', 'д');
            s = s.Replace(';', 'ж');
            s = s.Replace('"', 'э');
            s = s.Replace('z', 'я');
            s = s.Replace('x', 'ч');
            s = s.Replace('c', 'с');
            s = s.Replace('v', 'м');
            s = s.Replace('b', 'и');
            s = s.Replace('n', 'т');
            s = s.Replace('m', 'ь');
            s = s.Replace(',', 'б');
            s = s.Replace('.', 'ю');
            s = s.Replace('\'', 'э');


            s = s.Replace('Q', 'й');
            s = s.Replace('W', 'ц');
            s = s.Replace('E', 'у');
            s = s.Replace('R', 'к');
            s = s.Replace('T', 'е');
            s = s.Replace('Y', 'н');
            s = s.Replace('U', 'г');
            s = s.Replace('I', 'ш');
            s = s.Replace('O', 'щ');
            s = s.Replace('P', 'з');
            s = s.Replace('[', 'х');
            s = s.Replace(']', 'ъ');
            s = s.Replace('A', 'ф');
            s = s.Replace('S', 'ы');
            s = s.Replace('D', 'в');
            s = s.Replace('F', 'а');
            s = s.Replace('G', 'п');
            s = s.Replace('H', 'р');
            s = s.Replace('J', 'о');
            s = s.Replace('K', 'л');
            s = s.Replace('L', 'д');
            s = s.Replace(';', 'ж');
            s = s.Replace('"', 'э');
            s = s.Replace('Z', 'я');
            s = s.Replace('X', 'ч');
            s = s.Replace('C', 'с');
            s = s.Replace('V', 'м');
            s = s.Replace('B', 'и');
            s = s.Replace('N', 'т');
            s = s.Replace('M', 'ь');

            s = s.Replace("|", "/");
            s = s.Replace("\n", " ");
            s = s.Replace("\r", "");
            s = s.ToUpper();
            return s;
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (textBox4.Text.Length == 11)
            {
                textBox4.ForeColor = Color.Red;
            }
            else
            {
                textBox4.ForeColor = Color.Black;
            }
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsControl(e.KeyChar)) return;
            if (e.KeyChar < '0' || e.KeyChar > '9' || textBox4.Text.Length >= 11)
            {
                e.Handled = true;
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (richTextBox1.Text == "" || richTextBox1.Text == null) return;
            InputLanguage myCurrentLanguage = InputLanguage.CurrentInputLanguage;
            if (myCurrentLanguage.Culture.Name != "ru-RU")
            {
                string s = RUSSIAN(richTextBox1.Text);
                if (s != richTextBox1.Text)
                {
                    richTextBox1.Text = s;
                    richTextBox1.SelectionStart = s.Length;

                }
            }
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new System.Globalization.CultureInfo("ru-RU"));
        }

        public void zag(string text)
        {
            button1.Text = text;
            if (Application.StartupPath.IndexOf("comp") != -1)
            {
                putin = Application.StartupPath.Substring(0, Application.StartupPath.IndexOf("\\comp"));
            }
            else
            {
                putin = Application.StartupPath;
            }
        }

        public void zag2(string text, DataGridViewRow dt)
        {
            button1.Text = text;
            obid = dt.Cells[9].Value.ToString();
            textBox1.Text = dt.Cells[0].Value.ToString().ToUpper();
            textBox2.Text = dt.Cells[1].Value.ToString().ToUpper();
            textBox3.Text = dt.Cells[2].Value.ToString().ToUpper();
            if (dt.Cells[3].Value.ToString() == "Ж")
            {
                radioButton1.Checked = true;
                radioButton2.Checked = false;
            }
            else
            {
                radioButton1.Checked = false;
                radioButton2.Checked = true;
            }
            textBox4.Text = dt.Cells[4].Value.ToString().ToUpper();
            numericUpDown1.Value = Convert.ToInt32(dt.Cells[5].Value.ToString());
            numericUpDown2.Value = Convert.ToInt32(dt.Cells[6].Value.ToString());
            textBox5.Text = dt.Cells[7].Value.ToString();
            richTextBox1.Text = dt.Cells[8].Value.ToString().Replace("<r>", "\r").Replace("<n>", "\n");

            if (Application.StartupPath.IndexOf("comp") != -1)
            {
                putin = Application.StartupPath.Substring(0, Application.StartupPath.IndexOf("\\comp"));
            }
            else
            {
                putin = Application.StartupPath;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "79")
            {
                MessageBox.Show("Данные клиента не заполнены!");
                return;
            }
            /*  проверка правописания    try
                  {
                      TextBox tmp = new TextBox();
                      tmp.Text = textBox1.Text.Substring(0, 1) + textBox1.Text.Substring(1).ToLower();
                      string tmps = tmp.Text;
                      fSpellCheck(tmp);
                      if (tmps != tmp.Text)
                          textBox1.Text = tmp.Text.ToUpper();

                      tmp.Text = textBox2.Text.Substring(0, 1) + textBox2.Text.Substring(1).ToLower();
                      tmps = tmp.Text;
                      fSpellCheck(tmp);
                      if (tmps != tmp.Text)
                          textBox2.Text = tmp.Text.ToUpper();

                      tmp.Text = textBox3.Text.Substring(0, 1) + textBox3.Text.Substring(1).ToLower();
                      tmps = tmp.Text;
                      fSpellCheck(tmp);
                      if (tmps != tmp.Text)
                          textBox3.Text = tmp.Text.ToUpper();
                  }
                  catch { }*/

            if (button1.Text == "Создать клиента и выбрать")
            {
                //создание клиента
                string f = "";
                if (File.Exists(putin + "\\users.akv"))
                {
                    StreamReader Sr = new StreamReader(putin + "\\users.akv", Encoding.UTF8);
                    f = Sr.ReadToEnd();
                    Sr.Close();
                    Sr = null;
                }
                string zak = textBox1.Text + "|" + textBox2.Text + "|" + textBox3.Text + "|" + ((radioButton1.Checked) ? "1" : "2") + "|" + textBox4.Text + "|"
                    + numericUpDown1.Value + "|" + numericUpDown2.Value + "|" + textBox5.Text + "|" + richTextBox1.Text.Replace("\r", "<r>").Replace("\n", "<n>") + "|";

                if (f.IndexOf(zak) != -1)
                {
                    MessageBox.Show("Клиент с такими данными уже существует!");
                    return;
                }
                f = zak + "\r\n" + f;
                File.WriteAllText(putin + "\\users.akv", f, Encoding.UTF8);
                Form6 fs = this.Owner as Form6;
                fs.load_and_vibor();
                this.Close();

            }
            else
            {
                //редактирование клиента
                int c = 0;
                string str33 = "";
                string s = "";
                try
                {
                    string nom = obid;
                    using (StreamReader sr = new StreamReader(putin + "\\users.akv"))
                    {
                        while ((str33 = sr.ReadLine()) != null)
                        {
                            if (c.ToString() != nom)
                            {
                                s += str33 + "\r\n";
                            }
                            else
                            {
                                string zak = textBox1.Text + "|" + textBox2.Text + "|" + textBox3.Text + "|" + ((radioButton1.Checked) ? "1" : "2") + "|" + textBox4.Text + "|"
                     + numericUpDown1.Value + "|" + numericUpDown2.Value + "|" + textBox5.Text + "|" + richTextBox1.Text.Replace("\r", "<r>").Replace("\n", "<n>") + "|";
                                s += zak + "\r\n";
                            }
                            c++;
                        }
                        sr.Close();
                        File.WriteAllText(putin + "\\users.akv", s, Encoding.UTF8);
                    }
                    Form6 fs = this.Owner as Form6;
                    DataGridViewRow g = new DataGridViewRow();
                    DataGridViewCell sl=new DataGridViewTextBoxCell();
                    sl.Value = textBox1.Text;
                    g.Cells.Add(sl);
                    DataGridViewCell sl2 = new DataGridViewTextBoxCell();
                    sl2.Value = textBox2.Text;
                    g.Cells.Add(sl2);
                    DataGridViewCell sl3 = new DataGridViewTextBoxCell();
                    sl3.Value = textBox3.Text;
                    g.Cells.Add(sl3);
                    DataGridViewCell sl4 = new DataGridViewTextBoxCell();
                    sl4.Value = ((radioButton1.Checked) ? "Ж" : "М");
                    g.Cells.Add(sl4);
                    DataGridViewCell sl5 = new DataGridViewTextBoxCell();
                    sl5.Value = textBox4.Text;
                    g.Cells.Add(sl5);
                    DataGridViewCell sl6 = new DataGridViewTextBoxCell();
                    sl6.Value = numericUpDown1.Value.ToString();
                    g.Cells.Add(sl6);
                    DataGridViewCell sl7 = new DataGridViewTextBoxCell();
                    sl7.Value = numericUpDown2.Value.ToString();
                    g.Cells.Add(sl7);
                    DataGridViewCell sl8 = new DataGridViewTextBoxCell();
                    sl8.Value = textBox5.Text;
                    g.Cells.Add(sl8);
                    DataGridViewCell sl9 = new DataGridViewTextBoxCell();
                    sl9.Value = richTextBox1.Text.Replace("\r", "<r>").Replace("\n", "<n>");
                    g.Cells.Add(sl9);
                    fs.reload(obid, g);
                    this.Close();
                }
                catch (Exception fs)

                { MessageBox.Show(fs.Message); }
                c = 0;
                str33 = null;
                s = null;
            }
        }




        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar = char.ToUpper(e.KeyChar);
        }
    }
}
