using BlogContracts.BindingModels;
using BlogContracts.BusinessLogicContracts;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Blog
{
    public partial class FormAddTopic : Form
    {
        private readonly ITopicLogic _topicLogic;
        private readonly ICategoryLogic _categoryLogic;

        private int? _id;
        public int Id { set { _id = value; } }
        public FormAddTopic(ITopicLogic topicLogic, ICategoryLogic categoryLogic)
        {
            InitializeComponent();
            _topicLogic = topicLogic;
            _categoryLogic = categoryLogic;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxTopic.Text))
            {
                MessageBox.Show("Название темы не может быть пустой", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                var model = new TopicBindingModel
                {
                    Name = textBoxTopic.Text,
                    CategoryId = _id ?? 0,
            };
                var operationResult = _topicLogic.Create(model);

                if (!operationResult)
                {
                    throw new Exception("Ошибка при сохранеии.");
                }

                MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;

                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

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