﻿// Copyright 2017 DAIMTO ([Linda Lawton](https://twitter.com/LindaLawtonDK)) :  [www.daimto.com](http://www.daimto.com/)
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by DAIMTO-Google-apis-Sample-generator 1.0.0
//     Template File Name:  methodTemplate.tt
//     Build date: 2017-10-08
//     C# generater version: 1.0.0
//     
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------  
// About 
// 
// Unoffical sample for the Androidenterprise v1 API for C#. 
// This sample is designed to be used with the Google .Net client library. (https://github.com/google/google-api-dotnet-client)
// 
// API Description: Manages the deployment of apps to Android for Work users.
// API Documentation Link https://developers.google.com/android/work/play/emm-api
//
// Discovery Doc  https://www.googleapis.com/discovery/v1/apis/Androidenterprise/v1/rest
//
//------------------------------------------------------------------------------
// Installation
//
// This sample code uses the Google .Net client library (https://github.com/google/google-api-dotnet-client)
//
// NuGet package:
//
// Location: https://www.nuget.org/packages/Google.Apis.Androidenterprise.v1/ 
// Install Command: PM>  Install-Package Google.Apis.Androidenterprise.v1
//
//------------------------------------------------------------------------------  
using Google.Apis.Androidenterprise.v1;
using Google.Apis.Androidenterprise.v1.Data;
using System;

namespace GoogleSamplecSharpSample.Androidenterprisev1.Methods
{
  
    public static class EntitlementsSample
    {


        /// <summary>
        /// Removes an entitlement to an app for a user. 
        /// Documentation https://developers.google.com/androidenterprise/v1/reference/entitlements/delete
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Androidenterprise service.</param>  
        /// <param name="enterpriseId">The ID of the enterprise.</param>
        /// <param name="userId">The ID of the user.</param>
        /// <param name="entitlementId">The ID of the entitlement (a product ID), e.g. "app:com.google.android.gm".</param>
        public static void Delete(AndroidenterpriseService service, string enterpriseId, string userId, string entitlementId)
        {
            try
            {
                // Initial validation.
                if (service == null)
                    throw new ArgumentNullException("service");
                if (enterpriseId == null)
                    throw new ArgumentNullException(enterpriseId);
                if (userId == null)
                    throw new ArgumentNullException(userId);
                if (entitlementId == null)
                    throw new ArgumentNullException(entitlementId);

                // Make the request.
                 service.Entitlements.Delete(enterpriseId, userId, entitlementId).Execute();
            }
            catch (Exception ex)
            {
                throw new Exception("Request Entitlements.Delete failed.", ex);
            }
        }

        /// <summary>
        /// Retrieves details of an entitlement. 
        /// Documentation https://developers.google.com/androidenterprise/v1/reference/entitlements/get
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Androidenterprise service.</param>  
        /// <param name="enterpriseId">The ID of the enterprise.</param>
        /// <param name="userId">The ID of the user.</param>
        /// <param name="entitlementId">The ID of the entitlement (a product ID), e.g. "app:com.google.android.gm".</param>
        /// <returns>EntitlementResponse</returns>
        public static Entitlement Get(AndroidenterpriseService service, string enterpriseId, string userId, string entitlementId)
        {
            try
            {
                // Initial validation.
                if (service == null)
                    throw new ArgumentNullException("service");
                if (enterpriseId == null)
                    throw new ArgumentNullException(enterpriseId);
                if (userId == null)
                    throw new ArgumentNullException(userId);
                if (entitlementId == null)
                    throw new ArgumentNullException(entitlementId);

                // Make the request.
                return service.Entitlements.Get(enterpriseId, userId, entitlementId).Execute();
            }
            catch (Exception ex)
            {
                throw new Exception("Request Entitlements.Get failed.", ex);
            }
        }

        /// <summary>
        /// Lists all entitlements for the specified user. Only the ID is set. 
        /// Documentation https://developers.google.com/androidenterprise/v1/reference/entitlements/list
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Androidenterprise service.</param>  
        /// <param name="enterpriseId">The ID of the enterprise.</param>
        /// <param name="userId">The ID of the user.</param>
        /// <returns>EntitlementsListResponseResponse</returns>
        public static EntitlementsListResponse List(AndroidenterpriseService service, string enterpriseId, string userId)
        {
            try
            {
                // Initial validation.
                if (service == null)
                    throw new ArgumentNullException("service");
                if (enterpriseId == null)
                    throw new ArgumentNullException(enterpriseId);
                if (userId == null)
                    throw new ArgumentNullException(userId);

                // Make the request.
                return service.Entitlements.List(enterpriseId, userId).Execute();
            }
            catch (Exception ex)
            {
                throw new Exception("Request Entitlements.List failed.", ex);
            }
        }
        public class EntitlementsPatchOptionalParms
        {
            /// Set to true to also install the product on all the user's devices where possible. Failure to install on one or more devices will not prevent this operation from returning successfully, as long as the entitlement was successfully assigned to the user.
            public bool? Install { get; set; }  
        
        }
 
        /// <summary>
        /// Adds or updates an entitlement to an app for a user. This method supports patch semantics. 
        /// Documentation https://developers.google.com/androidenterprise/v1/reference/entitlements/patch
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Androidenterprise service.</param>  
        /// <param name="enterpriseId">The ID of the enterprise.</param>
        /// <param name="userId">The ID of the user.</param>
        /// <param name="entitlementId">The ID of the entitlement (a product ID), e.g. "app:com.google.android.gm".</param>
        /// <param name="body">A valid Androidenterprise v1 body.</param>
        /// <param name="optional">Optional paramaters.</param>
        /// <returns>EntitlementResponse</returns>
        public static Entitlement Patch(AndroidenterpriseService service, string enterpriseId, string userId, string entitlementId, Entitlement body, EntitlementsPatchOptionalParms optional = null)
        {
            try
            {
                // Initial validation.
                if (service == null)
                    throw new ArgumentNullException("service");
                if (body == null)
                    throw new ArgumentNullException("body");
                if (enterpriseId == null)
                    throw new ArgumentNullException(enterpriseId);
                if (userId == null)
                    throw new ArgumentNullException(userId);
                if (entitlementId == null)
                    throw new ArgumentNullException(entitlementId);

                // Building the initial request.
                var request = service.Entitlements.Patch(body, enterpriseId, userId, entitlementId);

                // Applying optional parameters to the request.                
                request = (EntitlementsResource.PatchRequest)SampleHelpers.ApplyOptionalParms(request, optional);

                // Requesting data.
                return request.Execute();
            }
            catch (Exception ex)
            {
                throw new Exception("Request Entitlements.Patch failed.", ex);
            }
        }
        public class EntitlementsUpdateOptionalParms
        {
            /// Set to true to also install the product on all the user's devices where possible. Failure to install on one or more devices will not prevent this operation from returning successfully, as long as the entitlement was successfully assigned to the user.
            public bool? Install { get; set; }  
        
        }
 
        /// <summary>
        /// Adds or updates an entitlement to an app for a user. 
        /// Documentation https://developers.google.com/androidenterprise/v1/reference/entitlements/update
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Androidenterprise service.</param>  
        /// <param name="enterpriseId">The ID of the enterprise.</param>
        /// <param name="userId">The ID of the user.</param>
        /// <param name="entitlementId">The ID of the entitlement (a product ID), e.g. "app:com.google.android.gm".</param>
        /// <param name="body">A valid Androidenterprise v1 body.</param>
        /// <param name="optional">Optional paramaters.</param>
        /// <returns>EntitlementResponse</returns>
        public static Entitlement Update(AndroidenterpriseService service, string enterpriseId, string userId, string entitlementId, Entitlement body, EntitlementsUpdateOptionalParms optional = null)
        {
            try
            {
                // Initial validation.
                if (service == null)
                    throw new ArgumentNullException("service");
                if (body == null)
                    throw new ArgumentNullException("body");
                if (enterpriseId == null)
                    throw new ArgumentNullException(enterpriseId);
                if (userId == null)
                    throw new ArgumentNullException(userId);
                if (entitlementId == null)
                    throw new ArgumentNullException(entitlementId);

                // Building the initial request.
                var request = service.Entitlements.Update(body, enterpriseId, userId, entitlementId);

                // Applying optional parameters to the request.                
                request = (EntitlementsResource.UpdateRequest)SampleHelpers.ApplyOptionalParms(request, optional);

                // Requesting data.
                return request.Execute();
            }
            catch (Exception ex)
            {
                throw new Exception("Request Entitlements.Update failed.", ex);
            }
        }
        
        }

        public static class SampleHelpers
        {

        /// <summary>
        /// Using reflection to apply optional parameters to the request.  
        /// 
        /// If the optonal parameters are null then we will just return the request as is.
        /// </summary>
        /// <param name="request">The request. </param>
        /// <param name="optional">The optional parameters. </param>
        /// <returns></returns>
        public static object ApplyOptionalParms(object request, object optional)
        {
            if (optional == null)
                return request;

            System.Reflection.PropertyInfo[] optionalProperties = (optional.GetType()).GetProperties();

            foreach (System.Reflection.PropertyInfo property in optionalProperties)
            {
                // Copy value from optional parms to the request.  They should have the same names and datatypes.
                System.Reflection.PropertyInfo piShared = (request.GetType()).GetProperty(property.Name);
				if (property.GetValue(optional, null) != null) // TODO Test that we do not add values for items that are null
					piShared.SetValue(request, property.GetValue(optional, null), null);
            }

            return request;
        }
    }
}