using Microsoft.AspNetCore.Mvc.ModelBinding;
using qtc_net_client_2.Services;
using QtCNETAPI.Dtos.User;
using QtCNETAPI.Events;
using QtCNETAPI.Models;
using QtCNETAPI.Services.ApiService;
using QtCNETAPI.Services.GatewayService;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace qtc_net_client_2.Forms
{
    public partial class DirectMessage : Form
    {
        private IGatewayService _gatewayService;
        private IApiService _apiService;
        private QtCNETAPI.Models.Message? InitMessage;
        private AudioService AudioService = new();
        public UserInformationDto User { get; set; }
        public ObservableCollection<string> Messages { get; set; }
        public DirectMessage(IGatewayService gatewayService, IApiService apiService, UserInformationDto user, QtCNETAPI.Models.Message? initMsg = null)
        {
            _gatewayService = gatewayService;
            _apiService = apiService;
            User = user;
            Messages = new ObservableCollection<string>();
            InitMessage = initMsg;
            InitializeComponent();
        }

        private void frmDirectMessage_Load(object sender, EventArgs e)
        {
            lblUsername.Text = User.Username;
            Text = $"QtC.NET Client - Direct Message With {User.Username}";
            Messages.CollectionChanged += Messages_CollectionChanged;

            if (InitMessage != null)
            {
                Messages.Add($"[{User.Username}] {InitMessage.Content}\n");
                AudioService.PlaySoundEffect("sndDirectMsg");
            }
        }

        private async void btnSend_Click(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                await Invoke(async delegate ()
                {
                    if (!string.IsNullOrEmpty(rtxtChatbox.Text))
                    {
                        await _gatewayService.SendDirectMessageAsync(User, new QtCNETAPI.Models.Message { Content = rtxtChatbox.Text, AuthorId = _apiService.CurrentUser.Id });
                        Messages.Add($"[{_apiService.CurrentUser.Username}] {rtxtChatbox.Text}\n");
                        rtxtChatbox.Clear();
                        AudioService.PlaySoundEffect("sndSendClick");
                    }
                });
            }
            else
            {
                if (!string.IsNullOrEmpty(rtxtChatbox.Text))
                {
                    await _gatewayService.SendDirectMessageAsync(User, new QtCNETAPI.Models.Message { Content = rtxtChatbox.Text, AuthorId = _apiService.CurrentUser.Id });
                    BeginInvoke(delegate () { Messages.Add($"[{_apiService.CurrentUser.Username}] {rtxtChatbox.Text}\n"); });
                    BeginInvoke(delegate () { rtxtChatbox.Clear(); });
                    AudioService.PlaySoundEffect("sndSendClick");
                }
            }
        }

        private void rtxtChatbox_KeyPress(object sender, KeyEventArgs e)
        {
            // mimick clicking send
            if (e.KeyCode == Keys.Enter)
                btnSend_Click(sender, e);
        }

        private void Messages_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count > 0)
            {
                if (InvokeRequired)
                {
                    Invoke(delegate ()
                    {
                        var msg = e.NewItems.Cast<string>().FirstOrDefault();
                        rtxtChat.AppendText(msg);
                        if (!msg!.Contains(_apiService.CurrentUser.Username)) AudioService.PlaySoundEffect("sndMessage");
                    });
                }
                else
                {
                    var msg = e.NewItems.Cast<string>().FirstOrDefault();
                    rtxtChat.AppendText(msg);
                    if (!msg!.Contains(_apiService.CurrentUser.Username)) AudioService.PlaySoundEffect("sndMessage");
                }
            }
        }
    }
}
