using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using InABox.Core;
using Newtonsoft.Json;

namespace InABox.Serialization
{
    public class JsonSerializer : ISerializer<string>
    {
        public TType Deserialize<TType>(string data) // where TType : BaseObject, new()
        {
            var serializerSettings = new JsonSerializerSettings
            {
                DateParseHandling = DateParseHandling.DateTime,
                DateFormatHandling = DateFormatHandling.IsoDateFormat,
                DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
            };

            serializerSettings.Converters.Add(new CoreTableJsonConverter());
            //serializerSettings.Converters.Add(new DateTimeJsonConverter());

            serializerSettings.Converters.Add(new FilterJsonConverter());
            serializerSettings.Converters.Add(new ColumnJsonConverter());
            serializerSettings.Converters.Add(new SortOrderJsonConverter());
            serializerSettings.Converters.Add(new UserPropertiesJsonConverter());

            var ret = default(TType);
            try
            {
                ret = JsonConvert.DeserializeObject<TType>(data, serializerSettings);
            }
            catch
            {
                if (typeof(TType).IsArray)
                {
                    ret = JsonConvert.DeserializeObject<TType>(data, serializerSettings);
                    // JsonConvert.DeserializeObject<JArray>(data,)
                    // var listtype = typeof(List<>).MakeGenericType(typeof(TType).GetElementType());
                    // var list = JsonConvert.DeserializeObject(data, listtype, serializerSettings) as IList;
                    // new ArrayList
                    // ret = (TType)(list.To
                    //object o = Array.CreateInstance(typeof(TType).GetElementType(), 0);
                    //ret = (TType)o;
                }
                else
                {
                    Activator.CreateInstance<TType>();
                }
            }

            return ret;
        }


        public string Serialize<TType>(TType o) // where TType : BaseObject, new()
        {
            var serializerSettings = new JsonSerializerSettings
            {
                DateParseHandling = DateParseHandling.DateTime,
                DateFormatHandling = DateFormatHandling.IsoDateFormat,
                DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
            };

            serializerSettings.Converters.Add(new CoreTableJsonConverter());
            //serializerSettings.Converters.Add(new DateTimeJsonConverter());

            serializerSettings.Converters.Add(new FilterJsonConverter());
            serializerSettings.Converters.Add(new ColumnJsonConverter());
            serializerSettings.Converters.Add(new SortOrderJsonConverter());
            serializerSettings.Converters.Add(new UserPropertiesJsonConverter());

            var json = JsonConvert.SerializeObject(o, Formatting.None, serializerSettings);
            return json;
        }
    }
}