﻿using System;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
using Zencoder.HttpHelpers;

namespace Zencoder.Jobs
{
    /// <summary>
    ///     Implements the cancel job request.
    /// </summary>
    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class FinishLiveJobRequest : Request<FinishLiveJobResponse>
    {
        private int _jobId;
        private Uri _url;

        /// <summary>
        ///     Initializes a new instance of the CancelJobRequest class.
        /// </summary>
        /// <param name="zencoder">The <see cref="Zencoder" /> service to create the request with.</param>
        public FinishLiveJobRequest(Zencoder zencoder)
            : base(zencoder)
        {
        }

        /// <summary>
        ///     Initializes a new instance of the CancelJobRequest class.
        /// </summary>
        /// <param name="apiKey">The API key to use when connecting to the service.</param>
        /// <param name="baseUrl">The service base URL.</param>
        public FinishLiveJobRequest(string apiKey, Uri baseUrl)
            : base(apiKey, baseUrl)
        {
        }

        /// <summary>
        ///     Gets or sets the ID of the job to cancel.
        /// </summary>
        public int JobId
        {
            get => _jobId;

            set
            {
                _jobId = value;
                _url = null;
            }
        }

        /// <summary>
        ///     Gets the concrete URL this request will call.
        /// </summary>
        public override Uri Url
        {
            get
            {
                if (_url != null) return _url;
                if (JobId < 1)
                    throw new InvalidOperationException("JobId must be set before generating the request URL.");

                var path = $"jobs/{JobId}/finish";
                _url = BaseUrl.AppendPath(path).WithApiKey(ApiKey);

                return _url;
            }
        }

        /// <summary>
        ///     Gets the HTTP verb to use when making the request.
        /// </summary>
        public override HttpMethod Verb => HttpMethod.Put;
        


    }
}