using System.Collections.Generic;
using System.Linq;
using GxPress.EnumConst;
using GxPress.Result;
using GxPress.Service.Interface.ContentJson;
using Newtonsoft.Json;

namespace GxPress.Service.Implement.ContentJson
{
    public class ContentJsonService : IContentJsonService
    {
        public string GetTitile(string title, string content)
        {
            if (string.IsNullOrWhiteSpace(title))
            {
                if (string.IsNullOrEmpty(content))
                    content = "[]";
                var data = JsonConvert.DeserializeObject<List<ContentJsonData>>(content);
                if (data.Count > 0)
                {
                    var first = data.First();
                    if (first.TypeValue == AllTypeConst.Text.GetHashCode() && !string.IsNullOrWhiteSpace(first.Text))
                        title = first.Text;
                    else
                    {
                        foreach (var contentJsonData in data)
                        {
                            //是否是文本
                            if (contentJsonData.TypeValue == AllTypeConst.Text.GetHashCode())
                                continue;
                            else if (contentJsonData.TypeValue == AllTypeConst.Image.GetHashCode())
                            {
                                title = "[图片]";
                                break;
                            }
                            else if (contentJsonData.TypeValue == AllTypeConst.File.GetHashCode())
                            {
                                title = "[附件]";
                                break;
                            }
                            else
                            {
                                title = "[附件]";
                                break;
                            }
                        }
                    }
                }
            }
            if (string.IsNullOrEmpty(title))
                title = string.Empty;
            if (title.Length > 50)
                title = title.Remove(50, title.Length - 50);
            return title;
        }
    }
}