/// This code was generated by
/// \ / _    _  _|   _  _
///  | (_)\/(_)(_|\/| |(/_  v1.0.0
///       /       /
/// <summary>
/// PLEASE NOTE that this class contains preview products that are subject to change. Use them with caution. If you
/// currently do not have developer preview access, please contact help@twilio.com.
///
/// DocumentPermissionResource
/// </summary>

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using Twilio.Base;
using Twilio.Clients;
using Twilio.Converters;
using Twilio.Exceptions;
using Twilio.Http;

namespace Twilio.Rest.Preview.Sync.Service.Document
{

    public class DocumentPermissionResource : Resource
    {
        private static Request BuildFetchRequest(FetchDocumentPermissionOptions options, ITwilioRestClient client)
        {
            return new Request(
                HttpMethod.Get,
                Rest.Domain.Preview,
                "/Sync/Services/" + options.PathServiceSid + "/Documents/" + options.PathDocumentSid + "/Permissions/" + options.PathIdentity + "",
                queryParams: options.GetParams(),
                headerParams: null
            );
        }

        /// <summary>
        /// Fetch a specific Sync Document Permission.
        /// </summary>
        /// <param name="options"> Fetch DocumentPermission parameters </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> A single instance of DocumentPermission </returns>
        public static DocumentPermissionResource Fetch(FetchDocumentPermissionOptions options,
                                                       ITwilioRestClient client = null)
        {
            client = client ?? TwilioClient.GetRestClient();
            var response = client.Request(BuildFetchRequest(options, client));
            return FromJson(response.Content);
        }

        #if !NET35
        /// <summary>
        /// Fetch a specific Sync Document Permission.
        /// </summary>
        /// <param name="options"> Fetch DocumentPermission parameters </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> Task that resolves to A single instance of DocumentPermission </returns>
        public static async System.Threading.Tasks.Task<DocumentPermissionResource> FetchAsync(FetchDocumentPermissionOptions options,
                                                                                               ITwilioRestClient client = null)
        {
            client = client ?? TwilioClient.GetRestClient();
            var response = await client.RequestAsync(BuildFetchRequest(options, client));
            return FromJson(response.Content);
        }
        #endif

        /// <summary>
        /// Fetch a specific Sync Document Permission.
        /// </summary>
        /// <param name="pathServiceSid"> The service_sid </param>
        /// <param name="pathDocumentSid"> Sync Document SID or unique name. </param>
        /// <param name="pathIdentity"> Identity of the user to whom the Sync Document Permission applies. </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> A single instance of DocumentPermission </returns>
        public static DocumentPermissionResource Fetch(string pathServiceSid,
                                                       string pathDocumentSid,
                                                       string pathIdentity,
                                                       ITwilioRestClient client = null)
        {
            var options = new FetchDocumentPermissionOptions(pathServiceSid, pathDocumentSid, pathIdentity);
            return Fetch(options, client);
        }

        #if !NET35
        /// <summary>
        /// Fetch a specific Sync Document Permission.
        /// </summary>
        /// <param name="pathServiceSid"> The service_sid </param>
        /// <param name="pathDocumentSid"> Sync Document SID or unique name. </param>
        /// <param name="pathIdentity"> Identity of the user to whom the Sync Document Permission applies. </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> Task that resolves to A single instance of DocumentPermission </returns>
        public static async System.Threading.Tasks.Task<DocumentPermissionResource> FetchAsync(string pathServiceSid,
                                                                                               string pathDocumentSid,
                                                                                               string pathIdentity,
                                                                                               ITwilioRestClient client = null)
        {
            var options = new FetchDocumentPermissionOptions(pathServiceSid, pathDocumentSid, pathIdentity);
            return await FetchAsync(options, client);
        }
        #endif

        private static Request BuildDeleteRequest(DeleteDocumentPermissionOptions options, ITwilioRestClient client)
        {
            return new Request(
                HttpMethod.Delete,
                Rest.Domain.Preview,
                "/Sync/Services/" + options.PathServiceSid + "/Documents/" + options.PathDocumentSid + "/Permissions/" + options.PathIdentity + "",
                queryParams: options.GetParams(),
                headerParams: null
            );
        }

        /// <summary>
        /// Delete a specific Sync Document Permission.
        /// </summary>
        /// <param name="options"> Delete DocumentPermission parameters </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> A single instance of DocumentPermission </returns>
        public static bool Delete(DeleteDocumentPermissionOptions options, ITwilioRestClient client = null)
        {
            client = client ?? TwilioClient.GetRestClient();
            var response = client.Request(BuildDeleteRequest(options, client));
            return response.StatusCode == System.Net.HttpStatusCode.NoContent;
        }

        #if !NET35
        /// <summary>
        /// Delete a specific Sync Document Permission.
        /// </summary>
        /// <param name="options"> Delete DocumentPermission parameters </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> Task that resolves to A single instance of DocumentPermission </returns>
        public static async System.Threading.Tasks.Task<bool> DeleteAsync(DeleteDocumentPermissionOptions options,
                                                                          ITwilioRestClient client = null)
        {
            client = client ?? TwilioClient.GetRestClient();
            var response = await client.RequestAsync(BuildDeleteRequest(options, client));
            return response.StatusCode == System.Net.HttpStatusCode.NoContent;
        }
        #endif

        /// <summary>
        /// Delete a specific Sync Document Permission.
        /// </summary>
        /// <param name="pathServiceSid"> The service_sid </param>
        /// <param name="pathDocumentSid"> Sync Document SID or unique name. </param>
        /// <param name="pathIdentity"> Identity of the user to whom the Sync Document Permission applies. </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> A single instance of DocumentPermission </returns>
        public static bool Delete(string pathServiceSid,
                                  string pathDocumentSid,
                                  string pathIdentity,
                                  ITwilioRestClient client = null)
        {
            var options = new DeleteDocumentPermissionOptions(pathServiceSid, pathDocumentSid, pathIdentity);
            return Delete(options, client);
        }

        #if !NET35
        /// <summary>
        /// Delete a specific Sync Document Permission.
        /// </summary>
        /// <param name="pathServiceSid"> The service_sid </param>
        /// <param name="pathDocumentSid"> Sync Document SID or unique name. </param>
        /// <param name="pathIdentity"> Identity of the user to whom the Sync Document Permission applies. </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> Task that resolves to A single instance of DocumentPermission </returns>
        public static async System.Threading.Tasks.Task<bool> DeleteAsync(string pathServiceSid,
                                                                          string pathDocumentSid,
                                                                          string pathIdentity,
                                                                          ITwilioRestClient client = null)
        {
            var options = new DeleteDocumentPermissionOptions(pathServiceSid, pathDocumentSid, pathIdentity);
            return await DeleteAsync(options, client);
        }
        #endif

        private static Request BuildReadRequest(ReadDocumentPermissionOptions options, ITwilioRestClient client)
        {
            return new Request(
                HttpMethod.Get,
                Rest.Domain.Preview,
                "/Sync/Services/" + options.PathServiceSid + "/Documents/" + options.PathDocumentSid + "/Permissions",
                queryParams: options.GetParams(),
                headerParams: null
            );
        }

        /// <summary>
        /// Retrieve a list of all Permissions applying to a Sync Document.
        /// </summary>
        /// <param name="options"> Read DocumentPermission parameters </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> A single instance of DocumentPermission </returns>
        public static ResourceSet<DocumentPermissionResource> Read(ReadDocumentPermissionOptions options,
                                                                   ITwilioRestClient client = null)
        {
            client = client ?? TwilioClient.GetRestClient();
            var response = client.Request(BuildReadRequest(options, client));

            var page = Page<DocumentPermissionResource>.FromJson("permissions", response.Content);
            return new ResourceSet<DocumentPermissionResource>(page, options, client);
        }

        #if !NET35
        /// <summary>
        /// Retrieve a list of all Permissions applying to a Sync Document.
        /// </summary>
        /// <param name="options"> Read DocumentPermission parameters </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> Task that resolves to A single instance of DocumentPermission </returns>
        public static async System.Threading.Tasks.Task<ResourceSet<DocumentPermissionResource>> ReadAsync(ReadDocumentPermissionOptions options,
                                                                                                           ITwilioRestClient client = null)
        {
            client = client ?? TwilioClient.GetRestClient();
            var response = await client.RequestAsync(BuildReadRequest(options, client));

            var page = Page<DocumentPermissionResource>.FromJson("permissions", response.Content);
            return new ResourceSet<DocumentPermissionResource>(page, options, client);
        }
        #endif

        /// <summary>
        /// Retrieve a list of all Permissions applying to a Sync Document.
        /// </summary>
        /// <param name="pathServiceSid"> The service_sid </param>
        /// <param name="pathDocumentSid"> Sync Document SID or unique name. </param>
        /// <param name="pageSize"> Page size </param>
        /// <param name="limit"> Record limit </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> A single instance of DocumentPermission </returns>
        public static ResourceSet<DocumentPermissionResource> Read(string pathServiceSid,
                                                                   string pathDocumentSid,
                                                                   int? pageSize = null,
                                                                   long? limit = null,
                                                                   ITwilioRestClient client = null)
        {
            var options = new ReadDocumentPermissionOptions(pathServiceSid, pathDocumentSid){PageSize = pageSize, Limit = limit};
            return Read(options, client);
        }

        #if !NET35
        /// <summary>
        /// Retrieve a list of all Permissions applying to a Sync Document.
        /// </summary>
        /// <param name="pathServiceSid"> The service_sid </param>
        /// <param name="pathDocumentSid"> Sync Document SID or unique name. </param>
        /// <param name="pageSize"> Page size </param>
        /// <param name="limit"> Record limit </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> Task that resolves to A single instance of DocumentPermission </returns>
        public static async System.Threading.Tasks.Task<ResourceSet<DocumentPermissionResource>> ReadAsync(string pathServiceSid,
                                                                                                           string pathDocumentSid,
                                                                                                           int? pageSize = null,
                                                                                                           long? limit = null,
                                                                                                           ITwilioRestClient client = null)
        {
            var options = new ReadDocumentPermissionOptions(pathServiceSid, pathDocumentSid){PageSize = pageSize, Limit = limit};
            return await ReadAsync(options, client);
        }
        #endif

        /// <summary>
        /// Fetch the target page of records
        /// </summary>
        /// <param name="targetUrl"> API-generated URL for the requested results page </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> The target page of records </returns>
        public static Page<DocumentPermissionResource> GetPage(string targetUrl, ITwilioRestClient client)
        {
            client = client ?? TwilioClient.GetRestClient();

            var request = new Request(
                HttpMethod.Get,
                targetUrl
            );

            var response = client.Request(request);
            return Page<DocumentPermissionResource>.FromJson("permissions", response.Content);
        }

        /// <summary>
        /// Fetch the next page of records
        /// </summary>
        /// <param name="page"> current page of records </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> The next page of records </returns>
        public static Page<DocumentPermissionResource> NextPage(Page<DocumentPermissionResource> page,
                                                                ITwilioRestClient client)
        {
            var request = new Request(
                HttpMethod.Get,
                page.GetNextPageUrl(Rest.Domain.Preview)
            );

            var response = client.Request(request);
            return Page<DocumentPermissionResource>.FromJson("permissions", response.Content);
        }

        /// <summary>
        /// Fetch the previous page of records
        /// </summary>
        /// <param name="page"> current page of records </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> The previous page of records </returns>
        public static Page<DocumentPermissionResource> PreviousPage(Page<DocumentPermissionResource> page,
                                                                    ITwilioRestClient client)
        {
            var request = new Request(
                HttpMethod.Get,
                page.GetPreviousPageUrl(Rest.Domain.Preview)
            );

            var response = client.Request(request);
            return Page<DocumentPermissionResource>.FromJson("permissions", response.Content);
        }

        private static Request BuildUpdateRequest(UpdateDocumentPermissionOptions options, ITwilioRestClient client)
        {
            return new Request(
                HttpMethod.Post,
                Rest.Domain.Preview,
                "/Sync/Services/" + options.PathServiceSid + "/Documents/" + options.PathDocumentSid + "/Permissions/" + options.PathIdentity + "",
                postParams: options.GetParams(),
                headerParams: null
            );
        }

        /// <summary>
        /// Update an identity's access to a specific Sync Document.
        /// </summary>
        /// <param name="options"> Update DocumentPermission parameters </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> A single instance of DocumentPermission </returns>
        public static DocumentPermissionResource Update(UpdateDocumentPermissionOptions options,
                                                        ITwilioRestClient client = null)
        {
            client = client ?? TwilioClient.GetRestClient();
            var response = client.Request(BuildUpdateRequest(options, client));
            return FromJson(response.Content);
        }

        #if !NET35
        /// <summary>
        /// Update an identity's access to a specific Sync Document.
        /// </summary>
        /// <param name="options"> Update DocumentPermission parameters </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> Task that resolves to A single instance of DocumentPermission </returns>
        public static async System.Threading.Tasks.Task<DocumentPermissionResource> UpdateAsync(UpdateDocumentPermissionOptions options,
                                                                                                ITwilioRestClient client = null)
        {
            client = client ?? TwilioClient.GetRestClient();
            var response = await client.RequestAsync(BuildUpdateRequest(options, client));
            return FromJson(response.Content);
        }
        #endif

        /// <summary>
        /// Update an identity's access to a specific Sync Document.
        /// </summary>
        /// <param name="pathServiceSid"> Sync Service Instance SID. </param>
        /// <param name="pathDocumentSid"> Sync Document SID or unique name. </param>
        /// <param name="pathIdentity"> Identity of the user to whom the Sync Document Permission applies. </param>
        /// <param name="read"> Read access. </param>
        /// <param name="write"> Write access. </param>
        /// <param name="manage"> Manage access. </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> A single instance of DocumentPermission </returns>
        public static DocumentPermissionResource Update(string pathServiceSid,
                                                        string pathDocumentSid,
                                                        string pathIdentity,
                                                        bool? read,
                                                        bool? write,
                                                        bool? manage,
                                                        ITwilioRestClient client = null)
        {
            var options = new UpdateDocumentPermissionOptions(pathServiceSid, pathDocumentSid, pathIdentity, read, write, manage);
            return Update(options, client);
        }

        #if !NET35
        /// <summary>
        /// Update an identity's access to a specific Sync Document.
        /// </summary>
        /// <param name="pathServiceSid"> Sync Service Instance SID. </param>
        /// <param name="pathDocumentSid"> Sync Document SID or unique name. </param>
        /// <param name="pathIdentity"> Identity of the user to whom the Sync Document Permission applies. </param>
        /// <param name="read"> Read access. </param>
        /// <param name="write"> Write access. </param>
        /// <param name="manage"> Manage access. </param>
        /// <param name="client"> Client to make requests to Twilio </param>
        /// <returns> Task that resolves to A single instance of DocumentPermission </returns>
        public static async System.Threading.Tasks.Task<DocumentPermissionResource> UpdateAsync(string pathServiceSid,
                                                                                                string pathDocumentSid,
                                                                                                string pathIdentity,
                                                                                                bool? read,
                                                                                                bool? write,
                                                                                                bool? manage,
                                                                                                ITwilioRestClient client = null)
        {
            var options = new UpdateDocumentPermissionOptions(pathServiceSid, pathDocumentSid, pathIdentity, read, write, manage);
            return await UpdateAsync(options, client);
        }
        #endif

        /// <summary>
        /// Converts a JSON string into a DocumentPermissionResource object
        /// </summary>
        /// <param name="json"> Raw JSON string </param>
        /// <returns> DocumentPermissionResource object represented by the provided JSON </returns>
        public static DocumentPermissionResource FromJson(string json)
        {
            // Convert all checked exceptions to Runtime
            try
            {
                return JsonConvert.DeserializeObject<DocumentPermissionResource>(json);
            }
            catch (JsonException e)
            {
                throw new ApiException(e.Message, e);
            }
        }

        /// <summary>
        /// Twilio Account SID.
        /// </summary>
        [JsonProperty("account_sid")]
        public string AccountSid { get; private set; }
        /// <summary>
        /// Sync Service Instance SID.
        /// </summary>
        [JsonProperty("service_sid")]
        public string ServiceSid { get; private set; }
        /// <summary>
        /// Sync Document SID.
        /// </summary>
        [JsonProperty("document_sid")]
        public string DocumentSid { get; private set; }
        /// <summary>
        /// Identity of the user to whom the Sync Document Permission applies.
        /// </summary>
        [JsonProperty("identity")]
        public string Identity { get; private set; }
        /// <summary>
        /// Read access.
        /// </summary>
        [JsonProperty("read")]
        public bool? _Read { get; private set; }
        /// <summary>
        /// Write access.
        /// </summary>
        [JsonProperty("write")]
        public bool? Write { get; private set; }
        /// <summary>
        /// Manage access.
        /// </summary>
        [JsonProperty("manage")]
        public bool? Manage { get; private set; }
        /// <summary>
        /// URL of this Sync Document Permission.
        /// </summary>
        [JsonProperty("url")]
        public Uri Url { get; private set; }

        private DocumentPermissionResource()
        {

        }
    }

}