﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EditMetaIDForm.cs" company="">
//   
// </copyright>
// <summary>
//   The edit meta id form.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace MineAPI.Editor.Item
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    using EditorBase.Item;

    using EditorBase.Mod;

    /// <summary>
    ///     The edit meta id form.
    /// </summary>
    public partial class EditMetaIDForm : Form
    {
        #region Fields

        /// <summary>
        ///     The _default meta id.
        /// </summary>
        private int _defaultMetaID;

        /// <summary>
        ///     The _sub item.
        /// </summary>
        private SubItemsArchive _subItem;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="EditMetaIDForm" /> class.
        /// </summary>
        public EditMetaIDForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets or sets the base item.
        /// </summary>
        /// <value>The base item.</value>
        /// <returns>UnregisteredItem.</returns>
        public UnregisteredItem BaseItem { private get; set; }

        /// <summary>
        ///     Gets or sets the sub item.
        /// </summary>
        /// <value>The sub item.</value>
        /// <returns>SubItemsArchive.</returns>
        public SubItemsArchive SubItem
        {
            private get
            {
                return _subItem;
            }

            set
            {
                _subItem = value;
                _defaultMetaID = _subItem.MetaID;
                RefreshMetaID();
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The edit meta id form_ form closed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void EditMetaIDForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            EditorItems.EditMetaID(false, null, null);
            ModLoader.Instance.SaveMod();
        }

        /// <summary>
        ///     Check if the inputted meta ID is valid and within range.
        /// </summary>
        /// <returns>bool.</returns>
        private bool IsMetaIDValid()
        {
            var invalidMetaIDs = new List<int>();
            foreach (SubItemsArchive item in BaseItem.SubItemsArchive)
            {
                if (!invalidMetaIDs.Contains(item.MetaID))
                {
                    invalidMetaIDs.Add(item.MetaID);
                }
            }

            foreach (int metaID in invalidMetaIDs)
            {
                if (metaID == txtMetaID.Value)
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        ///     The refresh meta id.
        /// </summary>
        private void RefreshMetaID()
        {
            // MetaID
            txtMetaID.DataBindings.Clear();
            txtMetaID.DataBindings.Add("Value", SubItem, "MetaID", false, DataSourceUpdateMode.OnPropertyChanged);
        }

        /// <summary>
        /// The btn cancel_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            SubItem.MetaID = _defaultMetaID;
            Close();
        }

        /// <summary>
        /// The btn o k_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!IsMetaIDValid())
            {
                MessageBox.Show(
                    this, 
                    "Unable to use the meta ID of " + txtMetaID.Value
                    + " since it is currently being used by another sub-item.", 
                    Application.ProductName, 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Hand);
                SubItem.MetaID = _defaultMetaID;
                return;
            }

            Close();
        }

        #endregion
    }
}