using Contracts.BindingModels;
using Contracts.BusinessLogic;
using Contracts.SearchModel;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Diagnostics;

namespace Forms
{
    public partial class FormPlace : Form
    {
        private readonly ILogger _logger;
        private readonly IPlaceLogic _logic;
        private int? _id;
        public int Id { set { _id = value; } }
        string[] ModelName = { "Vlad", "Dimit", "Mos", "Volg", "Peter" };
        string[] ModelType = { "grad", "sk", "vl", "orod", "burg" };
        string[] ModelCat = { "-severniy", "-sanct", "", "-kamchatskiy", "Yar" };
        Random rndModel = new Random();
        public FormPlace(ILogger<FormCompany> logger,  IPlaceLogic placeLogic)
        {
            InitializeComponent();
            _logger = logger;
            _logic = placeLogic;
        }

        private void FormComponent_Load(object sender, EventArgs e)
        {

            try
            {
                
                    if (_id.HasValue)
                    {
                        _logger.LogInformation("Получение company");

                        var view = _logic.ReadElement(new PlaceSM
                        {
                            Id = _id.Value
                        });

                        if (view != null)
                        {
                            TitleTextBox.Text = view.Title;
                            
                        }
                    }



                
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения company");
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
            }
            
            
        }
       
        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TitleTextBox.Text))
            {
                MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            _logger.LogInformation("Сохранение компонента");

            try
            {
                var model = new PlaceBM
                {
                    Id = _id ?? 0,
                    Title = TitleTextBox.Text,
                  
                };

                var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);

                if (!operationResult)
                {
                    throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
                }

                MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка сохранения компонента");
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

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

        private void PlacecomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void AddTenbutton_Click(object sender, EventArgs e)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 10; i++)
            {
                var model = new PlaceBM
                {
                    Id = _id ?? 0,
                    Title = ModelName[rndModel.Next(0, 4)] + ModelType[rndModel.Next(0, 4)] + ModelCat[rndModel.Next(0, 4)],
                    
                };

                var operationResult = _logic.Create(model);
            }
            stopwatch.Stop();
            var time = stopwatch.ElapsedMilliseconds;
            Timelabel.Text = time.ToString();
        }
    }
}