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

namespace StekloMaster
{
    public partial class Form7 : Form
    {
        public Form7()
        {
            InitializeComponent();
        }

        private void Form7_Shown(object sender, EventArgs e)
        {
            StreamReader Sr = new StreamReader(Application.StartupPath + "\\manager.akv", Encoding.UTF8);
            string f = Sr.ReadToEnd();
            Sr.Close();
            Sr = null;
            textBox1.Text = f.Split('|')[0];
            textBox2.Text = f.Split('|')[1];
        }

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

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

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

        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"));
        }


        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 button1_Click(object sender, EventArgs e)
        {
            File.WriteAllText(Application.StartupPath + "\\manager.akv", textBox1.Text.Replace("|","/")+"|"+textBox2.Text, Encoding.UTF8);
            Form1 fs = this.Owner as Form1;
            fs.load_manager();
            this.Close();
        }

    }
}
