

using System;

using Google.Apis.Dfareporting.v1_3;
using Google.Apis.Dfareporting.v1_3.Data;

namespace DfaReporting.Sample
{
    /// <summary>
    /// This example gets the first page of a particular type of dimension available for reporting
    /// in the given date range. You can use a similar workflow to retrieve the values for any
    /// dimension.
    /// 
    /// The available dimension value names can be found here:
    /// https://developers.google.com/doubleclick-advertisers/reporting/v1.1/dimensions
    /// </summary>
    internal class GetDimensionValuesHelper
    {
        private readonly DfareportingService service;

        /// <summary>
        /// Instantiate a helper for retrieving dimension values.
        /// </summary>
        /// <param name="service">DfaReporting service object used to run the requests.</param>
        public GetDimensionValuesHelper(DfareportingService service)
        {
            this.service = service;
        }

        /// <summary>
        /// Lists the first page of results for a dimension value.
        /// </summary>
        /// <param name="dimensionName">The name of the dimension to retrieve values for.</param>
        /// <param name="userProfileId">
        /// The ID number of the DFA user profile to run this request as.
        /// </param>
        /// <param name="startDate">
        /// Values which existed after this start date will be returned.
        /// </param>
        /// <param name="endDate">
        /// Values which existed before this end date will be returned.
        /// </param>
        /// <param name="maxPageSize">The maximum page size to retrieve.</param>
        /// <returns>The first page of dimension values received.</returns>
        public DimensionValueList Query(string dimensionName, long userProfileId,
            DateTime startDate, DateTime endDate, int maxPageSize)
        {
            Console.WriteLine("=================================================================");
            Console.WriteLine("Listing available {0} values", dimensionName);
            Console.WriteLine("=================================================================");

            // Create a dimension value query which selects available dimension values.
            var request = new DimensionValueRequest();
            request.DimensionName = dimensionName;
            request.StartDate = DfaReportingDateConverterUtil.convert(startDate);
            request.EndDate = DfaReportingDateConverterUtil.convert(endDate);
            var dimensionQuery = service.DimensionValues.Query(request, userProfileId);
            dimensionQuery.MaxResults = maxPageSize;

            // Retrieve values and display them.
            var values = dimensionQuery.Execute();

            if (values.Items.Count > 0)
            {
                foreach (var dimensionValue in values.Items)
                {
                    Console.WriteLine("{0} with value \"{1}\" was found.", dimensionName,
                        dimensionValue.Value);
                }
            }
            else
            {
                Console.WriteLine("No values found.");
            }
            Console.WriteLine();
            return values;
        }
    }
}
