using System;
using System.Activities;
using System.Activities.Tracking;
using System.Activities.XamlIntegration;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using SignalRWorkflowTracking;

namespace VisualTrackingWeb.Controllers
{
    public class FlowExampleController : ApiController
    {
        // GET api/default1
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/default1/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/default1
        public IEnumerable<KeyValuePair<string, object>> Post([FromBody]string flowInput)
        {
            Uri url = Request.RequestUri;
            string apiUrl = string.Format("{0}://{1}:{2}/registration", url.Scheme, url.Host, url.Port);
            WorkflowInvoker invoker = new WorkflowInvoker(ActivityXamlServices.Load(@"D:\projects\VisualTrackingTalk\SignalRVisualTracking\Workflow.xaml"));
            invoker.Extensions.Add(new SignalRTrackingParticipant(apiUrl)
                {
                    TrackingProfile = new TrackingProfile
                        {
                            Name = "SignalR profile",
                            Queries = {new ActivityStateQuery
                            {
                                Variables = {"*"},
                                Arguments = {"*"},
                                States =
                                {
                                    "Executing"
                                }}}
                        }
                });
            
            Dictionary<string, object> inputs = new Dictionary<string, object>();
            inputs.Add("whoRocks", flowInput);
            IDictionary<string, object> outputs = invoker.Invoke(inputs);

            return outputs.Select( pair => pair);
        }

        // PUT api/default1/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/default1/5
        public void Delete(int id)
        {
        }
    }
}
