using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Com.FirmLib.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Com.FirmLib.Entity
{
    /// <summary>
    /// 商品型号
    /// </summary>
    public class ProductTypeItem
    {
        #region 附加信息
        //// <summary>
        /// 生产厂商Id
        /// </summary>
        [JsonProperty("venderId")]
        public string ManufacturerId { get; set; }
        /// <summary>
        /// 品牌Id
        /// </summary>
        [JsonProperty("brandId")]
        public string BrandId { get; set; }
        /// <summary>
        /// 产品Id
        /// </summary>
        [JsonProperty("productId")]
        public string ProductId { get; set; } 
        #endregion
      
        [JsonProperty("specificationId")]
        public string Id { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
       
        [JsonProperty("infos")]
        [JsonConverter(typeof(ParameterItemsConverter))]
        public List<ParameterItem> Parameters { get; set; }
 
    }

    public class ParameterItemsConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }
            List<ParameterItem> parameters = value as  List<ParameterItem>;
            if (parameters!=null)
            {
                JToken t = parameters.ConvertToJObject();
                t.WriteTo(writer);
            }
            else
            {
                JToken t = JToken.FromObject(value);
                t.WriteTo(writer);
            }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
            JsonSerializer serializer)
        {
            try
            {
                JObject jObject = (JObject) serializer.Deserialize(reader, typeof(JObject));


                return jObject.ConvertToParameterItems();
            }
            catch (Exception)
            {

                return new List<ParameterItem>();
            }


        }

        public override bool CanConvert(Type objectType)
        {
            return objectType==typeof(List<ParameterItem>);
        }
    }

    //public class ProductTypeExtendInfo
    //{
    //      //"specificationId":"",
    //      //  "specificationName":"",
    //      //  "venderId":"",
    //      //  "venderName":"",
    //      //  "brandId":"",
    //      //  "brandName":"",
    //      //  "productId":"",
    //      //  "productName":""
    //}
}
