using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;

namespace CloudAwesome.Xrm.SamplePlugins;

/// <summary>
/// Sample code taken from Microsoft/PowerApps-Samples
/// https://github.com/microsoft/PowerApps-Samples/blob/master/dataverse/orgsvc/C%23/FollowupPlugin/FollowupPlugin/FollowupPlugin.cs
/// --------------------------------------------------
/// The plug-in creates a task activity after a new account is created. The activity reminds the user to
/// follow-up with the new account customer one week after the account was created.
/// </summary>
/// <remarks>Register this plug-in on the Create message, account entity, and asynchronous mode.
/// </remarks>
public sealed class FollowupPlugin : IPlugin  // Implements the IPlugin interface
{
    /// <summary>
    /// Execute method that is required by the IPlugin interface.
    /// </summary>
    /// <param name="serviceProvider">The service provider from which you can obtain the
    /// tracing service, plug-in execution context, organization service, and more.</param>
    public void Execute(IServiceProvider serviceProvider)
    {
        //Extract the tracing service for use in debugging sandboxed plug-ins.
        ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        // Obtain the execution context from the service provider.
        IPluginExecutionContext context = (IPluginExecutionContext)
            serviceProvider.GetService(typeof(IPluginExecutionContext));

        // The InputParameters collection contains all the data passed in the message request.
        if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
        {
            // Obtain the target entity from the input parameters.
            Entity entity = (Entity)context.InputParameters["Target"];

            // Verify that the target entity represents an account.
            // If not, this plug-in was not registered correctly.
            if (entity.LogicalName != "account")
                return;

            try
            {
                // Create a task activity to follow up with the account customer in 7 days. 
                Entity followup = new Entity("task");

                followup["subject"] = "Send e-mail to the new customer.";
                followup["description"] =
                    "Follow up with the customer. Check if there are any new issues that need resolution.";
                followup["scheduledstart"] = DateTime.Now.AddDays(7);
                followup["scheduledend"] = DateTime.Now.AddDays(7);
                followup["category"] = "account"; //context.PrimaryEntityName;

                // Refer to the account in the task activity.
                if (context.OutputParameters.Contains("id"))
                {
                    Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                    string regardingobjectidType = "account";

                    followup["regardingobjectid"] =
                    new EntityReference(regardingobjectidType, regardingobjectid);
                }

                // Obtain the organization service reference.
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                // Create the task in Microsoft Dynamics CRM.
                tracingService.Trace("FollowupPlugin: Successfully created the task activity.");
                service.Create(followup);
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the FollowupPlugin plug-in.", ex);
            }

            catch (Exception ex)
            {
                tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
                throw;
            }
        }
    }
}