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))
            {
                var data = JsonConvert.DeserializeObject<List<ContentJsonData>>(content);
                if (data.Count > 0)
                {
                    var first = data.First();
                    if (first.TypeValue == OldTextEditorTypeConst.Text.GetHashCode() && !string.IsNullOrWhiteSpace(first.Text))
                        title = first.Text;
                    else
                    {
                        foreach (var contentJsonData in data)
                        {
                            //是否是文本
                            if (contentJsonData.TypeValue == OldTextEditorTypeConst.Text.GetHashCode())
                                continue;
                            else if (contentJsonData.TypeValue == OldTextEditorTypeConst.Image.GetHashCode())
                            {
                                title = "[图片]";
                                break;
                            }
                            else if (contentJsonData.TypeValue == OldTextEditorTypeConst.File.GetHashCode())
                            {
                                title = "[附件]";
                                break;
                            }
                            else
                            {
                                title = "[附件]";
                                break;
                            }
                        }
                    }
                }
            }
            if (title.Length > 50)
                title = title.Remove(50, title.Length - 50);
            return title;
        }
    }
}