using Aliyun.OSS;
using Elasticsearch.Net;
using JiaZhiQuan.Common.AliOSS;
using JiaZhiQuan.Common.ElasticSearch;
using JiaZhiQuan.Common.ElasticSearch.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Senparc.Weixin.MP.Containers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Wicture.DbRESTFul;
using Wicture.DbRESTFul.Cache;
using Wicture.DbRESTFul.Infrastructure.Repository;

namespace JiaZhiQuan.Common
{
    public static partial class RepositoryExtension
    {

        public static async Task<PostCommentState> GetPostCommentState(this DbRESTFulRepository repository, long postId, long authorId = 0, IDbConnection conn = null)
        {
            var userId = long.Parse(repository.Context.Identity?.Id ?? "0");

            var model = await repository.QuerySingleOrDefaultAsync<PostCommentState>($"select notFocused, notFans, notSelf, `all` from n_post_comment_setting where postId={postId}", null, conn, null, true);
            if (model == null)
            {
                model = new PostCommentState();
            }
            if (userId > 0)
            {
                if (model.all == 0)
                {
                    if (model.notFocused == 0 && model.notFans == 0 && model.notSelf == 0)
                    {
                        model.canComment = 1;
                        return model;
                    }
                    // 检查是否是作者
                    if (authorId == 0)
                    {
                        authorId = await repository.QuerySingleOrDefaultAsync<long>($"select userId from n_post where id={postId}", null, conn, null, false);
                    }
                    var isAuthor = authorId == userId;
                    if (isAuthor) model.canComment = 1;
                    else if (model.notSelf == 0)
                    {
                        // 查询关注状态
                        var conditions = new List<string>();
                        if (model.notFans > 0)
                        {
                            conditions.Add($" (userId={authorId} and fanId={userId}) ");
                        }
                        if (model.notFocused > 0)
                        {
                            conditions.Add($" (userId={userId} and fanId={authorId}) ");
                        }
                        if (conditions.Count > 0)
                        {
                            var condition = string.Join("or", conditions);
                            var records = await repository.QueryAsync<dynamic>($"select userId, fanId from n_user_fan where {condition}", null, conn, null, false);
                            if ((model.notFans > 0 && records.Any(e => e.userId == authorId && e.fanId == userId)) || (model.notFocused > 0 && records.Any(e => e.userId == userId && e.fanId == authorId)))
                            {
                                model.canComment = 1;
                            }
                        }
                    }
                }
            }
            return model;
        }

    }

}
