using System;
using System.Drawing;
using System.Windows.Forms;
using АвтотранспортноеПредприятие.Database;
using АвтотранспортноеПредприятие.Models;
using АвтотранспортноеПредприятие.Models.АвтотранспортноеПредприятие.Models;

namespace АвтотранспортноеПредприятие
{
    public partial class FormLogin : Form
    {
        private DatabaseManager db;
        [System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
        public Customer CurrentUser { get; set; }

        private Label lblTitle;
        private Label lblPhone;
        private Label lblPassword;
        private TextBox txtPhone;
        private TextBox txtPassword;
        private Button btnLogin;
        private Button btnCancel;

        public FormLogin()
        {
            db = new DatabaseManager();
            InitializeForm();
            SetupEvents();
        }

        private void InitializeForm()
        {
            this.Text = "Авторизация";
            this.Size = new Size(400, 320);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.BackColor = Color.White;

            lblTitle = new Label();
            lblTitle.Text = "Вход в систему";
            lblTitle.Font = new Font("Microsoft Sans Serif", 16, FontStyle.Bold);
            lblTitle.ForeColor = Color.DarkBlue;
            lblTitle.Size = new Size(200, 30);
            lblTitle.Location = new Point(100, 20);
            lblTitle.TextAlign = ContentAlignment.MiddleCenter;

            lblPhone = new Label();
            lblPhone.Text = "Телефон:";
            lblPhone.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);
            lblPhone.Size = new Size(80, 25);
            lblPhone.Location = new Point(50, 80);

            txtPhone = new TextBox();
            txtPhone.Size = new Size(200, 25);
            txtPhone.Location = new Point(140, 80);
            txtPhone.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);
            txtPhone.BackColor = Color.WhiteSmoke;

            lblPassword = new Label();
            lblPassword.Text = "Пароль:";
            lblPassword.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);
            lblPassword.Size = new Size(80, 25);
            lblPassword.Location = new Point(50, 130);

            txtPassword = new TextBox();
            txtPassword.Size = new Size(200, 25);
            txtPassword.Location = new Point(140, 130);
            txtPassword.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);
            txtPassword.PasswordChar = '*';
            txtPassword.BackColor = Color.WhiteSmoke;

            btnLogin = new Button();
            btnLogin.Text = "Вход";
            btnLogin.Size = new Size(100, 35);
            btnLogin.Location = new Point(80, 200);
            btnLogin.BackColor = Color.LightGreen;
            btnLogin.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
            btnLogin.ForeColor = Color.DarkGreen;
            btnLogin.FlatStyle = FlatStyle.Flat;

            btnCancel = new Button();
            btnCancel.Text = "Отмена";
            btnCancel.Size = new Size(100, 35);
            btnCancel.Location = new Point(200, 200);
            btnCancel.BackColor = Color.LightCoral;
            btnCancel.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
            btnCancel.ForeColor = Color.DarkRed;
            btnCancel.FlatStyle = FlatStyle.Flat;

            this.Controls.Add(lblTitle);
            this.Controls.Add(lblPhone);
            this.Controls.Add(txtPhone);
            this.Controls.Add(lblPassword);
            this.Controls.Add(txtPassword);
            this.Controls.Add(btnLogin);
            this.Controls.Add(btnCancel);
        }

        private void SetupEvents()
        {
            btnLogin.Click += BtnLogin_Click;
            btnCancel.Click += BtnCancel_Click;
            this.Load += FormLogin_Load;
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            txtPhone.Clear();
            txtPassword.Clear();
            txtPhone.Focus();
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            string phone = txtPhone.Text.Trim();
            string password = txtPassword.Text.Trim();

            if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(password))
            {
                MessageBox.Show("Введите телефон и пароль!", "Ошибка",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                var user = db.Authenticate(phone, password);

                if (user != null)
                {
                    CurrentUser = user;
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    MessageBox.Show("Неверный телефон или пароль!", "Ошибка авторизации",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPassword.Clear();
                    txtPassword.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Ошибка подключения к базе данных: {ex.Message}", "Ошибка",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}