using InABox.Core;
using InABox.Poster.Shared;
using InABox.Scripting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InABox.Poster.Custom;

public class CustomPosterEngine<TPostable> : BasePosterEngine<TPostable, ICustomPoster<TPostable>, CustomPosterSettings>
    where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
{

    public override bool BeforePost(IDataModel<TPostable> model)
    {
        if (GetScriptDocument() is ScriptDocument script)
        {
            return script.Execute(methodname: "BeforePost", parameters: new object[] { model });
        }
        return false;
    }

    protected override IPostResult<TPostable> DoProcess(IDataModel<TPostable> model)
    {
        if (GetScriptDocument() is ScriptDocument script)
        {
            if(!script.Execute(methodname: "Process", parameters: new object[] { model }))
            {
                throw new Exception("Post Failed.");
            }

            var resultsObject = script.GetValue("Results");
            return (resultsObject as IPostResult<TPostable>)
                ?? throw new Exception($"Script 'Results' property expected to be IPostResult<{typeof(TPostable)}>, got {resultsObject}");
        }
        else
        {
            throw new Exception("Post Failed.");
        }
    }
    public override void AfterPost(IDataModel<TPostable> model, IPostResult<TPostable> result)
    {
        if (GetScriptDocument() is ScriptDocument script)
        {
            script.Execute(methodname: "AfterPost", parameters: new object[] { model });
        }
    }
}
