using System;
using Microsoft.SharePoint;


namespace SharePointProducts.CopyMove.Samples.EventReceiver
{
    /// <summary>
    /// This sample list item event receiver demonstrates how to detect CopyMove 
    /// operations on items being added and updated in a library or list.
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);

            HandleCopyMoveEvent(properties.AfterProperties);
        }


        /// <summary>
        /// An item is being updated.
        /// </summary>
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);

            HandleCopyMoveEvent(properties.AfterProperties);
        }


        private void HandleCopyMoveEvent(SPItemEventDataCollection properties)
        {
            //
            // CopyMove injects a property named SPPCopyMoveEvent with an integer
            // value between 0 and 4.
            //
            object sppCopyMoveEventObj = properties["SPPCopyMoveEvent"];
            if (sppCopyMoveEventObj != null)
            {
                int copyMoveEventId;
                if (Int32.TryParse(sppCopyMoveEventObj.ToString(), out copyMoveEventId))
                {
                    HandleCopyMoveEvent(copyMoveEventId);
                }
            }
        }


        private void HandleCopyMoveEvent(int copyMoveEventId)
        {
            switch (copyMoveEventId)
            {
                case 0:   // Copying last item version
                    break;

                case 1:   // Moving last item version
                    break;

                case 2:   // Move in progress - more versions to come.
                    break;

                case 3:   // Copy in progress - more versions to come.
                    break;

                case 4:   // Copy complete.
                    break;

                case 5:   // Move complete.
                    break;

            }
        }
    }
}
