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

namespace Com.FirmLib.Bll
{
    public class BrandBll:BaseHttpBll
    {
        private BrandBll():base(@"/manufacturer/brand")
        {

        }
        /// <summary>
        /// 增加品牌
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Add(BrandItem item)
        {
            JObject jObject = new JObject();
            jObject.Add("venderId", item.ManufacturerId);
            jObject.Add("name", item.Name??string.Empty);
            jObject.Add("logo", item.LogoKey ?? string.Empty);
            var result = PostJsonAdd(jObject);
            var flag = HandlerResult(result, (jo) => { item.Id = jo["id"].ToString(); }, (jo) => { this.ErrorMessage = jo["resultMsg"].ToString(); });          
            return flag;
        }

        /// <summary>
        /// 获取制定生产厂商的品牌
        /// </summary>
        /// <param name="manufacturerId"></param>
        /// <returns></returns>
        public List<BrandItem> GetAllItems(string manufacturerId)
        {
            List<BrandItem> items = new List<BrandItem>();
            JObject jObject = new JObject {{"venderId", manufacturerId } };
            jObject.Add("orderDesc", false);
            var result = PostJsonQuery(jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<BrandItem>>(listStr);
            });
            return items;
        }
        /// <summary>
        /// 修改生品牌
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Update(BrandItem item)
        {
            JObject jObject = new JObject();           
            jObject.Add("brandId", item.Id);
            jObject.Add("venderId", item.ManufacturerId);
            jObject.Add("name", item.Name);
            jObject.Add("logo", item.LogoKey ?? string.Empty);
            var result = PostJsonUpdate(jObject);
            return HandlerResult(result, null, (jo) => { this.ErrorMessage = jo["resultMsg"].ToString(); }); ;
        }

        /// <summary>
        /// 删除品牌
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Delete(BrandItem item)
        {
            JObject jObject = new JObject();
            jObject.Add("brandId", item.Id);
            jObject.Add("venderId", item.ManufacturerId);
            var result = PostJsonDelete(jObject);
            return HandlerResult(result, null, (jo) => { this.ErrorMessage = jo["resultMsg"].ToString(); });
        }

        /// <summary>
        /// 获取品牌名称
        /// </summary>
        /// <param name="venderId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public BrandItem GetItem(string venderId,string id)
        {
            var brands = this.GetAllItems(venderId);
            return brands.FirstOrDefault(c => c.Id == id);
        }
    }
}
