using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace ZionRobot.WebService
{
    public class Activity
    {
        //private string token = "47497f45ea68b19b2f5bdab2542cd62af8dd4da5";

        internal string ActivityCreate(Models.ZFlow.Activity activity)
        {
            return new ZionVault.WebService.Client(Persistence.WSProperties.UrlApi)._request("PUT",
               Persistence.WSProperties.ActivityCreate,
               Persistence.WSProperties.Token,
               null,
               new JavaScriptSerializer().Serialize(activity),
               null);
        }

        internal string UpdateActivityUrl(Models.ZFlow.Activity activity)
        {
            return new ZionVault.WebService.Client(Persistence.WSProperties.UrlApi)._request("PUT",
               Persistence.WSProperties.UpdateActivityUrl,
               Persistence.WSProperties.Token,
               null,
               new JavaScriptSerializer().Serialize(activity),
               null);
        }
        
        internal string UpdateActivityCaseUrl(Models.ZFlow.Activity activity)
        {
            return new ZionVault.WebService.Client(Persistence.WSProperties.UrlApi)._request("PUT",
               Persistence.WSProperties.UpdateActivityCaseUrl,
               Persistence.WSProperties.Token,
               null,
               new JavaScriptSerializer().Serialize(activity),
               null);
        }

        internal string UpdateActivityFieldValue(Models.ZFlow.Activity activity, string name, string value)
        {
            return new ZionVault.WebService.Client(Persistence.WSProperties.UrlApi)._request("PUT",
               Persistence.WSProperties.UpdateActivityFieldValue,
               Persistence.WSProperties.Token,
               null,
               new JavaScriptSerializer().Serialize(new
               {
                   EntityId = activity.EntityId,
                   ActivityId = activity.Id,
                   ElementName = name,
                   Value = value
               }),
               null);
        }

        internal string UpdateActivitySync(Models.ZFlow.Activity activity, int syncStatus)
        {
            return new ZionVault.WebService.Client(Persistence.WSProperties.UrlApi)._request("PUT",
               Persistence.WSProperties.UpdateActivitySync,
               Persistence.WSProperties.Token,
               null,
               new JavaScriptSerializer().Serialize(new
               {
                   EntityId = activity.EntityId,
                   ActivityId = activity.Id,
                   SyncId = activity.SyncId,
                   SyncStatus = syncStatus
               }),
               null);
        }

        internal string UpdateAttachmentLink(Models.ZFlow.Attachment attachemnt)
        {
            return new ZionVault.WebService.Client(Persistence.WSProperties.UrlApi)._request("PUT",
               Persistence.WSProperties.UpdateAttachmentLink,
               Persistence.WSProperties.Token,
               null,
               new JavaScriptSerializer().Serialize(new
               {
                   EntityId = attachemnt.EntityId,
                   ActivityId = attachemnt.ActivityId,
                   Link = attachemnt.Link.AbsoluteUri
               }),
               null);
        }

        internal List<Models.ZFlow.Activity> ActivityToSync()
        {
            string result = new ZionVault.WebService.Client(Persistence.WSProperties.UrlApi)._request("GET",
                Persistence.WSProperties.ActivitiesToSync,
                Persistence.WSProperties.Token,
                null,
                string.Empty,
                null);

            //System.IO.File.WriteAllText(@"C:\Temp\Result.txt", result);

            var json = JsonConvert.DeserializeObject<List<Models.ZFlow.Activity>>(result);
            //var jsonSerialiser = new JavaScriptSerializer();

            //var json = jsonSerialiser.Deserialize<List<Models.ZFlow.Activity>>(result);


            //var a = System.Text.Json.JsonSerializer.Deserialize<Models.ZFlow.Activity>(result,
            //new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = false });

            //var json = new JavaScriptSerializer().Deserialize<Models.ZFlow.Activity>(result);


            return json;
        }

        internal List<Models.ZFlow.Activity> ActivitiesToCreateDataJuri()
        {
            string result = new ZionVault.WebService.Client(Persistence.WSProperties.UrlApi)._request("GET",
                Persistence.WSProperties.ActivitiesToCreateDataJuri,
                Persistence.WSProperties.Token,
                null,
                string.Empty,
                null);

            var jsonSerialiser = new JavaScriptSerializer();

            var json = jsonSerialiser.Deserialize<List<Models.ZFlow.Activity>>(result);

            return json;
        }
        internal List<Models.ZFlow.Activity> ActivityToCanceling()
        {
            string result = new ZionVault.WebService.Client(Persistence.WSProperties.UrlApi)._request("GET",
                Persistence.WSProperties.ActivitiesToCanceling,
                Persistence.WSProperties.Token,
                null,
                new JavaScriptSerializer().Serialize(new
                {
                    name = @"Wu shang hao"
                }),
                null);


            var jsonSerialiser = new JavaScriptSerializer();

            var json = jsonSerialiser.Deserialize<List<Models.ZFlow.Activity>>(result);

            return json;
        }


        internal List<Models.ZFlow.Activity> ActivityCanceled(Models.ZFlow.Activity activity)
        {
            string result = new ZionVault.WebService.Client(Persistence.WSProperties.UrlApi)._request("PUT",
                Persistence.WSProperties.ActivityCanceled,
                Persistence.WSProperties.Token,
                null,
                new JavaScriptSerializer().Serialize(new
                {
                    entity_id = activity.EntityId,
                    id = activity.Id,
                    activity_type_id = activity.TypeId
                }),
                null);


            var jsonSerialiser = new JavaScriptSerializer();

            var json = jsonSerialiser.Deserialize<List<Models.ZFlow.Activity>>(result);

            return json;
        }

        internal string ActivityDone(Models.ZFlow.Activity activity)
        {
            return new ZionVault.WebService.Client(Persistence.WSProperties.UrlApi)._request("PUT",
               Persistence.WSProperties.ActivityDone,
               Persistence.WSProperties.Token,
               null,
               new JavaScriptSerializer().Serialize(new { 
                    entity_id = activity.EntityId,
                    id = activity.Id,
                    activity_type_id = activity.TypeId
               }),
               null);
        }
    }
}
