using Elastic.Transport;
using Elastic.Transport.Diagnostics;
using Elastic.Transport.Extensions;
using Elastic.Transport.Products;

using Elastic.Clients.Elasticsearch;
using Xunit.Abstractions;
using ElasticSearchStudy.Core;

namespace ElasticSearchStudy.UnitTest
{
    public class UseElasticSearchTest
    {
        private ITestOutputHelper _output;

        public UseElasticSearchTest(ITestOutputHelper outputHelper)
        {
            _output = outputHelper;
        }

        [Fact]
        public void Test()
        {
            var elasticSetting = new ElasticsearchClientSettings(new Uri("https://localhost:9200"))
                .CertificateFingerprint("F8:B0:ED:80:2C:1C:6C:76:6E:CC:21:3A:CD:91:C3:C8:C7:77:D4:41:F4:71:50:FB:E7:0E:66:0D:71:8C:F3:1A")
                .Authentication(new BasicAuthentication("elastic", "3JI3QjRtjW8-Tl1q=mVx"));

            ElasticsearchClient client = new ElasticsearchClient(elasticSetting);

            var pingResponse = client.Ping();

            if (pingResponse.IsSuccess())
            {
                _output.WriteLine($"����ɹ�,{pingResponse.DebugInformation}");
            }
            else
            {
                _output.WriteLine("����ʧ��");
            }

            Assert.True(pingResponse.IsSuccess());
        }

        [Fact]
        public void CreateIndex_Test()
        {
            var elasticSetting = new ElasticsearchClientSettings(new Uri("https://localhost:9200"))
                .CertificateFingerprint("F8:B0:ED:80:2C:1C:6C:76:6E:CC:21:3A:CD:91:C3:C8:C7:77:D4:41:F4:71:50:FB:E7:0E:66:0D:71:8C:F3:1A")
                .Authentication(new BasicAuthentication("elastic", "3JI3QjRtjW8-Tl1q=mVx"))
                ;

            ElasticsearchClient client = new ElasticsearchClient(elasticSetting);


            var tweet = new Tweet
            {
                Id = 1,
                User = "stevejgordon",
                PostDate = DateTime.Now,
                Message = "Trying out the client, so far so good?"
            };

            var response =  client.Index(tweet, "tweet-index");

            if (response.IsValidResponse)
            {
                Console.WriteLine($"Index document with ID {response.Id} succeeded.");
            }

        }

        [Fact]
        public void GetDoc_Test()
        {
            var elasticSetting = new ElasticsearchClientSettings(new Uri("https://localhost:9200"))
                .CertificateFingerprint("F8:B0:ED:80:2C:1C:6C:76:6E:CC:21:3A:CD:91:C3:C8:C7:77:D4:41:F4:71:50:FB:E7:0E:66:0D:71:8C:F3:1A")
                .Authentication(new BasicAuthentication("elastic", "3JI3QjRtjW8-Tl1q=mVx"));

            ElasticsearchClient client = new ElasticsearchClient(elasticSetting);

            var tweet = new Tweet
            {
                Id = 2,
                User = "stevejgordon",
                PostDate = new DateTime(2009, 11, 15),
                Message = "Trying out the client, so far so good?"
            };

            var response =  client.Create<Tweet>(tweet, "my-tweet-index",3);

            if (response.IsValidResponse)
            {
                _output.WriteLine($"Index document with ID {response.Id} succeeded.");
            }
        }
    }
}