﻿using System;
using Newtonsoft.Json;

namespace ChaosCMS.Hal.Json
{
    /// <summary>
    ///
    /// </summary>
    public class JsonHalModelConverter : JsonConverter
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="objectType"></param>
        /// <returns></returns>
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(HalResponse);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="objectType"></param>
        /// <param name="existingValue"></param>
        /// <param name="serializer"></param>
        /// <returns></returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="value"></param>
        /// <param name="serializer"></param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var model = value as HalResponse;
            if (model == null)
            {
                return;
            }

            var output = model.ToJObject(serializer);
            output.WriteTo(writer);
        }
    }
}