<commit_before>﻿using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Serilog;
using SO115App.Models.Classi.Matrix;
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace SO115App.SignalR.Utility
{
    public class CallMatrix
    {
        private static Random random = new Random();
        private readonly HttpClient _client;
        private readonly IConfiguration _config;

        public CallMatrix(HttpClient client, IConfiguration config)
        {
            _client = client;
            _config = config;
        }

        public async Task<ChatRoom> GetChatRoomID(string codSede)
        {
            try
            {
                Log.Information("MATRIX - Inizio chiamata GET");

                Log.Information($"MATRIX - URL CHIAMATA GET - {_config.GetSection("UrlMatrix").Value}/directory/room/%23comando.{codSede.ToLower()}:vvf-test.cloud");

                //_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("test");
                var response = await _client.GetAsync($"{_config.GetSection("UrlMatrix").Value}/directory/room/%23comando.{codSede.ToLower()}:vvf-test.cloud").ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    using HttpContent content = response.Content;
                    var data = await content.ReadAsStringAsync().ConfigureAwait(false);
                    var RispostaCas = JsonConvert.DeserializeObject<ChatRoom>(data);

                    Log.Information($"MATRIX - RISULTATO CHIAMATA GET OK");

                    Log.Information($"MATRIX - FINE CHIAMATA GET");

                    return RispostaCas;
                }
                else
                {
                    ChatRoom RispostaCas = new ChatRoom();
                    RispostaCas.Error = response.ReasonPhrase;

                    Log.Information($"MATRIX - RISULTATO CHIAMATA GET KO - " + response.ReasonPhrase);
                    Log.Information($"MATRIX - RISULTATO CHIAMATA GET CONTENT - " + response.Content.ToString());
                    Log.Information($"MATRIX - FINE CHIAMATA GET");

                    return RispostaCas;
                }
            }
            catch (HttpRequestException e)
            {
                ChatRoom RispostaCas = new ChatRoom();
                RispostaCas.Error = e.Message;

                Log.Information($"MATRIX - RISULTATO CHIAMATA GET KO Message 1 - " + e.InnerException);

                Log.Information($"MATRIX - FINE CHIAMATA GET");

                return RispostaCas;
            }
            catch (Exception e)
            {
                ChatRoom RispostaCas = new ChatRoom();
                RispostaCas.Error = e.Message;

                Log.Information($"MATRIX - RISULTATO CHIAMATA GET KO Message 2 - " + e.Message);

                Log.Information($"MATRIX - FINE CHIAMATA GET");

                return RispostaCas;
            }
        }

        public async Task<string> PostBotInChatRoom(string room_id)
        {
            try
            {
                //_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("test");

                Log.Information($"MATRIX - INIZIO CHIAMATA POST");

                Log.Information($"MATRIX - URL CHIAMATA POST - {_config.GetSection("UrlMatrix").Value}/rooms/{room_id.Replace("!", "%21")}/join?access_token=<KEY>");

                var response = await _client.PostAsync($"{_config.GetSection("UrlMatrix").Value}/rooms/{room_id.Replace("!", "%21")}/join?access_token=<KEY>", null).ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    Log.Information($"MATRIX - ESITO CHIAMATA POST OK ");
                    Log.Information($"MATRIX - FINE CHIAMATA POST ");
                    return " Bot inviato con successo";
                }
                else
                {
                    Log.Information($"MATRIX - ESITO CHIAMATA POST KO - " + response.ReasonPhrase);
                    Log.Information($"MATRIX - RISULTATO CHIAMATA POST CONTENT - " + response.Content.ToString());
                    Log.Information($"MATRIX - FINE CHIAMATA POST ");
                    return response.ReasonPhrase;
                }
            }
            catch (HttpRequestException e)
            {
                Log.Information($"MATRIX - ESITO CHIAMATA POST KO Message 3 - " + e.InnerException);
                Log.Information($"MATRIX - FINE CHIAMATA POST ");
                return e.Message;
            }
            catch (Exception e)
            {
                Log.Information($"MATRIX - ESITO CHIAMATA POST KO  Message 4 - " + e.Message);
                Log.Information($"MATRIX - FINE CHIAMATA POST ");

                return e.Message;
            }
        }

        public async Task<string> PutMessage(string room_id, string message)
        {
            var randomId = RandomString(10);

            try
            {
                //_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("test");

                Log.Information($"MATRIX - INIZIO CHIAMATA PUT");

                Log.Information($"MATRIX - URL CHIAMATA PUT - {_config.GetSection("UrlMatrix").Value}/rooms/{room_id.Replace("!", "%21")}/send/m.room.message/{randomId}?access_token=<KEY>");

                SendMessage send = new SendMessage()
                {
                    Body = message,
                    Msgtype = "m.text"
                };

                var jsonString = "{\"msgtype\":\"" + send.Msgtype + "\",\"body\":\"" + send.Body + "\"}";
                var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

                var response = await _client.PutAsync($"{_config.GetSection("UrlMatrix").Value}/rooms/{room_id.Replace("!", "%21")}/send/m.room.message/{randomId}?access_token=<KEY>", httpContent).ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    Log.Information($"MATRIX - ESITO CHIAMATA PUT OK ");
                    Log.Information($"MATRIX - FINE CHIAMATA PUT ");

                    return "Invio effettuato con successo";
                }
                else
                {
                    Log.Information($"MATRIX - ESITO CHIAMATA PUT KO - " + response.ReasonPhrase);
                    Log.Information($"MATRIX - RISULTATO CHIAMATA PUT CONTENT - " + response.Content.ToString());
                    Log.Information($"MATRIX - FINE CHIAMATA PUT ");
<commit_msg>Fix - LogMatrix<commit_after>﻿using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Serilog;
using SO115App.Models.Classi.Matrix;
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace SO115App.SignalR.Utility
{
    public class CallMatrix
    {
        private static Random random = new Random();
        private readonly HttpClient _client;
        private readonly IConfiguration _config;

        public CallMatrix(HttpClient client, IConfiguration config)
        {
            _client = client;
            _config = config;
        }

        public async Task<ChatRoom> GetChatRoomID(string codSede)
        {
            try
            {
                Log.Information("MATRIX - Inizio chiamata GET");

                Log.Information($"MATRIX - URL CHIAMATA GET - {_config.GetSection("UrlMatrix").Value}/directory/room/%23comando.{codSede.ToLower()}:vvf-test.cloud");

                //_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("test");
                var response = await _client.GetAsync($"{_config.GetSection("UrlMatrix").Value}/directory/room/%23comando.{codSede.ToLower()}:vvf-test.cloud").ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    using HttpContent content = response.Content;
                    var data = await content.ReadAsStringAsync().ConfigureAwait(false);
                    var RispostaCas = JsonConvert.DeserializeObject<ChatRoom>(data);

                    Log.Information($"MATRIX - RISULTATO CHIAMATA GET OK");

                    Log.Information($"MATRIX - FINE CHIAMATA GET");

                    return RispostaCas;
                }
                else
                {
                    ChatRoom RispostaCas = new ChatRoom();
                    RispostaCas.Error = response.ReasonPhrase;

                    Log.Information($"MATRIX - RISULTATO CHIAMATA GET KO - " + response.ReasonPhrase);
                    Log.Information($"MATRIX - RISULTATO CHIAMATA GET CONTENT - " + response.Content.ToString());
                    Log.Information($"MATRIX - FINE CHIAMATA GET");

                    return RispostaCas;
                }
            }
            catch (HttpRequestException e)
            {
                ChatRoom RispostaCas = new ChatRoom();
                RispostaCas.Error = e.Message;

                Log.Information($"MATRIX - RISULTATO CHIAMATA GET KO Message 1 - " + e.InnerException);

                Log.Information($"MATRIX - FINE CHIAMATA GET");

                return RispostaCas;
            }
            catch (Exception e)
            {
                ChatRoom RispostaCas = new ChatRoom();
                RispostaCas.Error = e.Message;

                Log.Information($"MATRIX - RISULTATO CHIAMATA GET KO Message 2 - " + e.Message);

                Log.Information($"MATRIX - FINE CHIAMATA GET");

                return RispostaCas;
            }
        }

        public async Task<string> PostBotInChatRoom(string room_id)
        {
            try
            {
                //_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("test");

                Log.Information($"MATRIX - INIZIO CHIAMATA POST");

                Log.Information($"MATRIX - URL CHIAMATA POST - {_config.GetSection("UrlMatrix").Value}/rooms/{room_id.Replace("!", "%21")}/join?access_token=<KEY>");

                var response = await _client.PostAsync($"{_config.GetSection("UrlMatrix").Value}/rooms/{room_id.Replace("!", "%21")}/join?access_token=<KEY>", null).ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    Log.Information($"MATRIX - ESITO CHIAMATA POST OK ");
                    Log.Information($"MATRIX - FINE CHIAMATA POST ");
                    return " Bot inviato con successo";
                }
                else
                {
                    Log.Information($"MATRIX - ESITO CHIAMATA POST KO - " + response.ReasonPhrase);
                    Log.Information($"MATRIX - RISULTATO CHIAMATA POST Headers - " + response.Headers.ToString());
                    Log.Information($"MATRIX - FINE CHIAMATA POST ");
                    return response.ReasonPhrase;
                }
            }
            catch (HttpRequestException e)
            {
                Log.Information($"MATRIX - ESITO CHIAMATA POST KO Message 3 - " + e.InnerException);
                Log.Information($"MATRIX - FINE CHIAMATA POST ");
                return e.Message;
            }
            catch (Exception e)
            {
                Log.Information($"MATRIX - ESITO CHIAMATA POST KO  Message 4 - " + e.Message);
                Log.Information($"MATRIX - FINE CHIAMATA POST ");

                return e.Message;
            }
        }

        public async Task<string> PutMessage(string room_id, string message)
        {
            var randomId = RandomString(10);

            try
            {
                //_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("test");

                Log.Information($"MATRIX - INIZIO CHIAMATA PUT");

                Log.Information($"MATRIX - URL CHIAMATA PUT - {_config.GetSection("UrlMatrix").Value}/rooms/{room_id.Replace("!", "%21")}/send/m.room.message/{randomId}?access_token=<KEY>");

                SendMessage send = new SendMessage()
                {
                    Body = message,
                    Msgtype = "m.text"
                };

                var jsonString = "{\"msgtype\":\"" + send.Msgtype + "\",\"body\":\"" + send.Body + "\"}";
                var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

                var response = await _client.PutAsync($"{_config.GetSection("UrlMatrix").Value}/rooms/{room_id.Replace("!", "%21")}/send/m.room.message/{randomId}?access_token=<KEY>", httpContent).ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    Log.Information($"MATRIX - ESITO CHIAMATA PUT OK ");
                    Log.Information($"MATRIX - FINE CHIAMATA PUT ");

                    return "Invio effettuato con successo";
                }
                else
                {
                    Log.Information($"MATRIX - ESITO CHIAMATA PUT KO - " + response.ReasonPhrase);
                    Log.Information($"MATRIX - RISULTATO CHIAMATA POST Headers - " + response.Headers.ToString());
                    Log.Information($"MATRIX - FINE CHIAMATA PUT ");

{'fraction_non_alphanumeric': 0.1092234834247914, 'fraction_numerical': 0.0037585507028489815, 'mean_word_length': 1.7206543967280163, 'pattern_counts': {'":': 4, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6534896', 'n_tokens_mistral': 3814, 'n_tokens_neox': 3472, 'n_words': 681}