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.Threading.Tasks;
using System.Windows.Forms;

namespace StekloMaster
{
    public partial class СТАТУСЫ_настройки : Form
    {
        public СТАТУСЫ_настройки()
        {
            InitializeComponent();
        }

        private void StatusZakazSetting_Shown(object sender, EventArgs e)
        {
            if (Directory.Exists(Application.StartupPath + "\\scan_setting"))
            {
                if(File.Exists(Application.StartupPath + "\\scan_setting\\kod.akv"))
                {
                    StreamReader sr = new StreamReader(Application.StartupPath + "\\scan_setting\\kod.akv");
                    textBox1.Text = sr.ReadToEnd();
                    sr.Close();
                }
                if (File.Exists(Application.StartupPath + "\\scan_setting\\text.akv"))
                {
                    StreamReader sr = new StreamReader(Application.StartupPath + "\\scan_setting\\text.akv");
                    textBox2.Text = sr.ReadToEnd();
                    sr.Close();
                }
                if (File.Exists(Application.StartupPath + "\\scan_setting\\sms.akv"))
                {
                    checkBox1.Checked = true;
                    StreamReader sr = new StreamReader(Application.StartupPath + "\\scan_setting\\sms.akv");
                    string tmp = sr.ReadToEnd();
                    textBox3.Text = tmp.Split('|')[0];
                    textBox4.Text = tmp.Split('|')[1];
                    sr.Close();
                }
                if (File.Exists(Application.StartupPath + "\\scan_setting\\textsms.akv"))
                {
                    StreamReader sr = new StreamReader(Application.StartupPath + "\\scan_setting\\textsms.akv");
                    textBox5.Text = sr.ReadToEnd();
                    sr.Close();
                }
                if (File.Exists(Application.StartupPath + "\\scan_setting\\textdopsms.akv"))
                {
                    StreamReader sr = new StreamReader(Application.StartupPath + "\\scan_setting\\textdopsms.akv");
                    textBox15.Text = sr.ReadToEnd();
                    sr.Close();
                    checkBox4.Checked = true;
                }
                if (File.Exists(Application.StartupPath + "\\scan_setting\\mail.akv"))
                {
                    checkBox2.Checked = true;
                    StreamReader sr = new StreamReader(Application.StartupPath + "\\scan_setting\\mail.akv");
                    string tmp = sr.ReadToEnd();
                    string[] s = tmp.Split('|');
                    textBox8.Text = s[0];
                    textBox9.Text = s[1];
                    textBox6.Text = s[2];
                    textBox7.Text = s[3];
                    sr.Close();
                }
            }
            
        }
        

        private void button4_Click(object sender, EventArgs e)
        {
            if(textBox1.Text == "")
            {
                MessageBox.Show("Не указан код статуса!");
                return;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("Не указан текст статуса!");
                return;
            }
            if(checkBox1.Checked)
            {
                if (textBox3.Text == "")
                {
                    MessageBox.Show("Не указан логин СМС провайдера!");
                    return;
                }
                if (textBox4.Text == "")
                {
                    MessageBox.Show("Не указан пароль СМС провайдера!");
                    return;
                }
                if (textBox3.Text.IndexOf("|") != -1)
                {
                    MessageBox.Show("Символ | запрещен в логине СМС провайдера!");
                    return;
                }
                if (textBox4.Text.IndexOf("|") != -1)
                {
                    MessageBox.Show("Символ | запрещен в пароле СМС провайдера!");
                    return;
                }
                if (textBox5.Text == "")
                {
                    MessageBox.Show("Не указан текст СМС!");
                    return;
                }
            }
            if (checkBox2.Checked)
            {
                if (textBox8.Text.IndexOf("@") == -1)
                {
                    MessageBox.Show("Не указан логин почты для отправки!");
                    return;
                }
                if (textBox9.Text == "")
                {
                    MessageBox.Show("Не указан пароль почты!");
                    return;
                }
                if (textBox6.Text.IndexOf(".") == -1)
                {
                    MessageBox.Show("Не указан smtp сервер почты для отправки!");
                    return;
                }
                if (textBox7.Text.IndexOf("@") == -1)
                {
                    MessageBox.Show("Не указана почта получателя!");
                    return;
                }
                if (textBox8.Text.IndexOf("|") != -1)
                {
                    MessageBox.Show("Символ | запрещен в логине почты!");
                    return;
                }
                if (textBox9.Text.IndexOf("|") != -1)
                {
                    MessageBox.Show("Символ | запрещен в пароле почты!");
                    return;
                }
                if (textBox6.Text.IndexOf("|") != -1)
                {
                    MessageBox.Show("Символ | запрещен в smtp сервере почты!");
                    return;
                }
                if (textBox7.Text.IndexOf("|") != -1)
                {
                    MessageBox.Show("Символ | запрещен в почте получателя!");
                    return;
                }
            }
            if (checkBox4.Checked)
            {
                if (textBox15.Text == "")
                {
                    MessageBox.Show("Не указан текст заменяющий СМС!");
                    return;
                }
            }
            if (!Directory.Exists(Application.StartupPath+"\\scan_setting"))
            {
                Directory.CreateDirectory(Application.StartupPath + "\\scan_setting");
            }
            File.WriteAllText(Application.StartupPath + "\\scan_setting\\kod.akv", textBox1.Text, Encoding.UTF8);
            File.WriteAllText(Application.StartupPath + "\\scan_setting\\text.akv", textBox2.Text, Encoding.UTF8);
            if (checkBox1.Checked)
            {
                File.WriteAllText(Application.StartupPath + "\\scan_setting\\sms.akv", textBox3.Text + "|" + textBox4.Text, Encoding.UTF8);
                File.WriteAllText(Application.StartupPath + "\\scan_setting\\textsms.akv", textBox5.Text, Encoding.UTF8);
            }
            else
            {
                if (File.Exists(Application.StartupPath + "\\scan_setting\\sms.akv"))
                {
                    File.Delete(Application.StartupPath + "\\scan_setting\\sms.akv");
                    checkBox1.Checked = false;
                    textBox3.Text = textBox4.Text = "";
                }
                if (File.Exists(Application.StartupPath + "\\scan_setting\\textsms.akv"))
                {
                    File.Delete(Application.StartupPath + "\\scan_setting\\textsms.akv");
                }
            }
            if (checkBox2.Checked)
            {
                File.WriteAllText(Application.StartupPath + "\\scan_setting\\mail.akv", textBox8.Text + "|" + textBox9.Text + "|" + textBox6.Text + "|" + textBox7.Text, Encoding.UTF8);
            }
            else
            {
                if (File.Exists(Application.StartupPath + "\\scan_setting\\mail.akv"))
                {
                    File.Delete(Application.StartupPath + "\\scan_setting\\mail.akv");
                    checkBox1.Checked = false;
                    textBox6.Text = textBox7.Text = textBox8.Text = textBox9.Text = "";
                }
            }
            if (checkBox4.Checked)
            {
                File.WriteAllText(Application.StartupPath + "\\scan_setting\\textdopsms.akv", textBox15.Text, Encoding.UTF8);
            }
            else
            {
                if (File.Exists(Application.StartupPath + "\\scan_setting\\textdopsms.akv"))
                {
                    File.Delete(Application.StartupPath + "\\scan_setting\\textdopsms.akv");
                }
            }
            MessageBox.Show("Для применения настроек перезапустите программу!");
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsControl(e.KeyChar)) return;
            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                if (e.KeyChar != ',')
                    e.Handled = true;
            }
        }
        
    }
}
