using Microsoft.VisualBasic.FileIO;
using ProjectItCompany.Entities;
using ProjectItCompany.Entities.Enums;
using ProjectItCompany.Repositories;
using ProjectItCompany.Repositories.Implementations;
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 ProjectItCompany.Forms
{
    public partial class FormProject : Form
    {
        private readonly IProjectRepository _projectRepository;
        private int? _projectId;

        public int Id
        {
            set
            {
                try
                {
                    var project = _projectRepository.ReadProjectById(value);
                    if (project == null)
                    {
                        throw new InvalidDataException(nameof(project));
                    }
                    foreach (CompletedStages elem in Enum.GetValues(typeof(CompletedStages)))
                    {
                        if ((elem & project.CompletedStages) != 0)
                        {
                            checkedListBoxCompletedStage.SetItemChecked(checkedListBoxCompletedStage.Items.IndexOf(elem), true);
                        }
                    }

                    textBoxNameProject.Text = project.NameProject;
                    _projectId = value;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }
        public FormProject(IProjectRepository projectRepository)
        {
            InitializeComponent();
            _projectRepository = projectRepository ?? throw new ArgumentNullException(nameof(projectRepository));
        }

        private void ButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(textBoxNameProject.Text))
                {
                    throw new Exception("Имеются незаполненные поля");
                }
                if (_projectId.HasValue)
                {
                    _projectRepository.UpdateProject(CreateProject(_projectId.Value));
                }
                else
                {
                    _projectRepository.CreateProject(CreateProject(0));
                }
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка при сохранении",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            foreach (var elem in Enum.GetValues(typeof(CompletedStages)))
            {
                checkedListBoxCompletedStage.Items.Add(elem);
            }
        }

        private void ButtonCancel_Click(object sender, EventArgs e) => Close();

        private Project CreateProject(int id) => Project.CreateEntity(id, textBoxNameProject.Text, (int)comboBoxCustomer.SelectedValue, (CompletedStages)checkedListBoxCompletedStage.SelectedItem!);
    }
}
