<commit_before>﻿//Copyright (c) 2007-2016 ppy Pty Ltd <<EMAIL>>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE

using OpenTK;
using osu.Framework.GameModes.Testing;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Transformations;
using osu.Framework.Threading;
using osu.Game;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Chat;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace osu.Desktop.Tests
{
    class TestCaseChatDisplay : TestCase
    {
        private ScheduledDelegate messageRequest;

        public override string Name => @"Chat";
        public override string Description => @"Testing API polling";

        List<Channel> channels = new List<Channel>();
        private FlowContainer flow;

        Scheduler scheduler = new Scheduler();

        private APIAccess api => ((OsuGameBase)Game).API;

        public override void Reset()
        {
            base.Reset();

            flow = new FlowContainer()
            {
                Direction = FlowDirection.VerticalOnly,
                LayoutDuration = 100,
                LayoutEasing = EasingTypes.Out,
                Padding = new Vector2(1, 1)
            };


            lastMessageId = null;

            if (api.State != APIAccess.APIState.Online)
                api.OnStateChange += delegate { initializeChannels(); };
            else
                initializeChannels();


            ScrollContainer scrolling = new ScrollContainer()
            {
                Size = new Vector2(1, 0.5f)
            };

            scrolling.Add(flow);
            Add(scrolling);
        }

        protected override void Update()
        {
            scheduler.Update();
            base.Update();
        }

        private void initializeChannels()
        {
            if (api.State != APIAccess.APIState.Online)
                return;

            messageRequest?.Cancel();

            ListChannelsRequest req = new ListChannelsRequest();
            req.Success += delegate (List<Channel> channels)
            {
                this.channels = channels;
                messageRequest = scheduler.AddDelayed(requestNewMessages, 1000, true);
            };
            api.Queue(req);
        }

        long? lastMessageId = null;

        private void requestNewMessages()
        {
            messageRequest.Wait();

            Channel channel = channels.Find(c => c.Name == "#osu");

            GetMessagesRequest gm = new GetMessagesRequest(new List<Channel> { channel }, lastMessageId);
            gm.Success += delegate (List<Message> messages)
            {
                foreach (Message m in messages)
                {
                    //m.LineWidth = this.Size.X; //this is kinda ugly.
                    //m.Drawable.Depth = m.Id;
                    //m.Drawable.FadeInFromZero(800);

                    //flow.Add(m.Drawable);

                    //if (osu.Messages.Count > 50)<commit_msg>Add clear to ensure no pile-up of messages.<commit_after>﻿//Copyright (c) 2007-2016 ppy Pty Ltd <<EMAIL>>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE

using OpenTK;
using osu.Framework.GameModes.Testing;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Transformations;
using osu.Framework.Threading;
using osu.Game;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Chat;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace osu.Desktop.Tests
{
    class TestCaseChatDisplay : TestCase
    {
        private ScheduledDelegate messageRequest;

        public override string Name => @"Chat";
        public override string Description => @"Testing API polling";

        List<Channel> channels = new List<Channel>();
        private FlowContainer flow;

        Scheduler scheduler = new Scheduler();

        private APIAccess api => ((OsuGameBase)Game).API;

        public override void Reset()
        {
            base.Reset();

            flow = new FlowContainer()
            {
                Direction = FlowDirection.VerticalOnly,
                LayoutDuration = 100,
                LayoutEasing = EasingTypes.Out,
                Padding = new Vector2(1, 1)
            };


            lastMessageId = null;

            if (api.State != APIAccess.APIState.Online)
                api.OnStateChange += delegate { initializeChannels(); };
            else
                initializeChannels();


            ScrollContainer scrolling = new ScrollContainer()
            {
                Size = new Vector2(1, 0.5f)
            };

            scrolling.Add(flow);
            Add(scrolling);
        }

        protected override void Update()
        {
            scheduler.Update();
            base.Update();
        }

        private void initializeChannels()
        {
            if (api.State != APIAccess.APIState.Online)
                return;

            messageRequest?.Cancel();

            ListChannelsRequest req = new ListChannelsRequest();
            req.Success += delegate (List<Channel> channels)
            {
                this.channels = channels;
                messageRequest = scheduler.AddDelayed(requestNewMessages, 1000, true);
            };
            api.Queue(req);
        }

        long? lastMessageId = null;

        private void requestNewMessages()
        {
            messageRequest.Wait();

            Channel channel = channels.Find(c => c.Name == "#osu");

            GetMessagesRequest gm = new GetMessagesRequest(new List<Channel> { channel }, lastMessageId);
            gm.Success += delegate (List<Message> messages)
            {
                channel.Messages.Clear();
                foreach (Message m in messages)
                {
                    //m.LineWidth = this.Size.X; //this is kinda ugly.
                    //m.Drawable.Depth = m.Id;
                    //m.Drawable.FadeInFromZero(800);

                    //flow.Add(m.Drawable);

                    //if (osu.Messages.Count > 50)
{'fraction_non_alphanumeric': 0.10009609224855862, 'fraction_numerical': 0.008648302370275465, 'mean_word_length': 1.764497565294378, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 2, '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': '6434694', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1562, 'n_words': 371}