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 Form4 : Form
    {
        public Form4()
        {
            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"));
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                    Form6 fs = this.Owner as Form6;
                    DataGridViewRow g = new DataGridViewRow();
                    DataGridViewCell sl = new DataGridViewTextBoxCell();
                    if(textBox1.Text=="")
                        sl.Value = "Неизвестно";
                    else
                        sl.Value = textBox1.Text;
                    g.Cells.Add(sl);

                    DataGridViewCell sl2 = new DataGridViewTextBoxCell();
                    if(textBox2.Text=="")
                        sl2.Value = "Неизвестно";
                    else
                        sl2.Value = textBox2.Text;
                    g.Cells.Add(sl2);

                    DataGridViewCell sl3 = new DataGridViewTextBoxCell();
                    if(textBox2.Text=="")
                        sl3.Value = "Неизвестно";
                    else
                        sl3.Value = textBox3.Text;
                    g.Cells.Add(sl3);

                    DataGridViewCell sl4 = new DataGridViewTextBoxCell();
                    sl4.Value = ((radioButton1.Checked) ? "Ж" : "М");
                    g.Cells.Add(sl4);

                    DataGridViewCell sl5 = new DataGridViewTextBoxCell();
                    if(textBox4.Text == "")
                        sl5.Value = "79";
                    else
                        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();
                    if(textBox5.Text =="")
                        sl8.Value = "Неизвестно";
                    else
                        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.badklient(g);
                    this.Close();
                }
                catch { }
        }




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