﻿/***************************************************************************************
 * This code was generated by a tool.                                                  *
 * Please do not modify this file directly - modify VersionClient.Generated.tt instead *
 ***************************************************************************************/
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using PrtgAPI.Parameters;

namespace PrtgAPI.Request
{
    internal partial class VersionClient
    {
        //######################################
        // SetChannelProperty
        //######################################

        internal void SetChannelProperty(int[] sensorIds, int channelId, ChannelParameter[] @params, CancellationToken token)
        {
            var parameters = new SetChannelPropertyParameters(sensorIds, channelId, @params);

            if (NeedChannels(parameters, @params))
            {
                var channels = GetChannels(null, sensorIds, channelId, token);

                SetChannelProperty(channels, @params, token);
            }
            else
                client.SetObjectProperty(parameters, parameters.SensorIds.Length, token);
        }

        internal void SetChannelProperty(ICollection<Channel> channels, ChannelParameter[] @params, CancellationToken token)
        {
            var grouping = GroupChannels(channels, @params);

            foreach (var group in grouping)
                client.SetObjectProperty(group.Parameters, group.Parameters.SensorIds.Length, token);
        }

        internal async Task SetChannelPropertyAsync(int[] sensorIds, int channelId, ChannelParameter[] @params, CancellationToken token)
        {
            var parameters = new SetChannelPropertyParameters(sensorIds, channelId, @params);

            if (NeedChannels(parameters, @params))
            {
                var channels = await GetChannelsAsync(null, sensorIds, channelId, token).ConfigureAwait(false);

                await SetChannelPropertyAsync(channels, @params, token).ConfigureAwait(false);
            }
            else
                await client.SetObjectPropertyAsync(parameters, parameters.SensorIds.Length, token).ConfigureAwait(false);
        }

        internal async Task SetChannelPropertyAsync(ICollection<Channel> channels, ChannelParameter[] @params, CancellationToken token)
        {
            var grouping = GroupChannels(channels, @params);

            foreach (var group in grouping)
                await client.SetObjectPropertyAsync(group.Parameters, group.Parameters.SensorIds.Length, token).ConfigureAwait(false);
        }

        //######################################
        // GetTmpId
        //######################################

        protected int? GetTmpId(int deviceId, ICommandParameters internalParams, CancellationToken token)
        {
            //Purposely not validating the QueryTarget for addsensor2; when adding the sensor for real we don't really need it, and
            //current evidence suggestly we can simply just ignore it
            var tmpIdParameters = new BeginAddSensorQueryParameters(deviceId, internalParams[Parameter.SensorType].ToString(), SynthesizeParameters(internalParams));
            var tmpId = client.GetAddSensorTmpId(tmpIdParameters, token);

            if (tmpId == null)
                throw new PrtgRequestException($"Failed to add sensor for sensor type '{internalParams[Parameter.SensorType]}': type was not valid or you do not have sufficient permissions on the specified object.");

            return tmpId;
        }

        protected async Task<int?> GetTmpIdAsync(int deviceId, ICommandParameters internalParams, CancellationToken token)
        {
            //Purposely not validating the QueryTarget for addsensor2; when adding the sensor for real we don't really need it, and
            //current evidence suggestly we can simply just ignore it
            var tmpIdParameters = new BeginAddSensorQueryParameters(deviceId, internalParams[Parameter.SensorType].ToString(), SynthesizeParameters(internalParams));
            var tmpId = await client.GetAddSensorTmpIdAsync(tmpIdParameters, token).ConfigureAwait(false);

            if (tmpId == null)
                throw new PrtgRequestException($"Failed to add sensor for sensor type '{internalParams[Parameter.SensorType]}': type was not valid or you do not have sufficient permissions on the specified object.");

            return tmpId;
        }
    }
}