﻿using PresentationLayer.Views;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PresentationLayer.Forms
{
    public partial class GroupPropertiesForm : Form, IGroupPropertiesView
    {
        #region View properties
        public string ViewTitle
        {
            set
            {
                lblViewTitle.Text = value;
                this.Name = value;
            }
        }
        public bool ButtonSaveEnabled { set => btnSave.Enabled = value; }
        public string CreatedAt { set => lblCreatedAt.Text = value; }
        public string UpdatedAt { set => lblUpdatedAt.Text = value; }
        #endregion

        #region View events
        public event Action NotifySaveButtonClicked;
        public event Action NotifyViewLoad;
        public event Action NotifyViewShow;
        public event Action NotifyViewClosing;
        public event Action NotifyViewClosed;
        #endregion


        public GroupPropertiesForm()
        {
            InitializeComponent();
        }

        #region View methods
        public void ShowView() => this.ShowDialog();
        public void CloseView() => this.Close();
        public void SetupBindings(Dictionary<string, Binding> viewModelBindingDictionary)
        {
            tbGroupName.DataBindings.Clear();
            tbGroupName.DataBindings.Add(viewModelBindingDictionary["Name"]);
            tbDescription.DataBindings.Clear();
            tbDescription.DataBindings.Add(viewModelBindingDictionary["Description"]);
        }
        public void SetValidatorStatus(IBindableComponent bindableComponent, bool result, string message)
        {
            if (bindableComponent is null) return;
            Control control = bindableComponent as Control;
            if (control is null) return;

            if (result)
            {
                CorrectErrorProvicer.SetError(control, message);
                IncorrectErrorProvider.SetError(control, string.Empty);
            }
            else
            {
                CorrectErrorProvicer.SetError(control, string.Empty);
                IncorrectErrorProvider.SetError(control, message);
            }
        }
        #endregion

        #region Form events
        private void btnSave_Click(object sender, EventArgs e) => NotifySaveButtonClicked?.Invoke();
        private void btnCancel_Click(object sender, EventArgs e) => this.Close();
        private void GroupPropertiesForm_Load(object sender, EventArgs e) => NotifyViewLoad?.Invoke();
        private void GroupPropertiesForm_FormClosed(object sender, FormClosedEventArgs e) => NotifyViewClosed?.Invoke();
        private void GroupPropertiesForm_FormClosing(object sender, FormClosingEventArgs e) => NotifyViewClosing?.Invoke();
        private void GroupPropertiesForm_Shown(object sender, EventArgs e) => NotifyViewShow?.Invoke();
        #endregion
    }
}
