using IRaCIS.Core.Domain.Share;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;

namespace IRaCIS.Core.API
{
    public class JSONCustomDateConverter : DateTimeConverterBase
    {
        private TimeZoneInfo _timeZoneInfo;
        private string _dateFormat;

        private IUserInfo _userInfo;
        public JSONCustomDateConverter(string dateFormat, TimeZoneInfo timeZoneInfo, IUserInfo userInfo)
        {
            _dateFormat = dateFormat;
            _timeZoneInfo = timeZoneInfo;

            _userInfo = userInfo;
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var timeZoneId = _userInfo.TimeZoneId;

            var needConvertUtcDateTime = Convert.ToDateTime(value);


            var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);


            var dateTimeOffset = new DateTimeOffset(needConvertUtcDateTime);

            var time = TimeZoneInfo.ConvertTimeFromUtc(needConvertUtcDateTime, tz).ToString(_dateFormat);

            writer.WriteValue(time);
            writer.Flush();
        }

    }
}
