using qtc_net_client_2.Services;
using QtCNETAPI.Events;
using QtCNETAPI.Models;
using QtCNETAPI.Services.ApiService;
using QtCNETAPI.Services.GatewayService;
using System;
using System.Collections.Generic;
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 Chat : Form
    {
        IGatewayService _gatewayService;
        IApiService _apiService;
        AudioService AudioService = new();
        public Chat(IGatewayService gatewayService, IApiService apiService)
        {
            _gatewayService = gatewayService;
            _apiService = apiService;
            InitializeComponent();
        }

        private void frmChat_Load(object sender, EventArgs e)
        {
            // subscribe to server message event
            _gatewayService.OnServerMessageReceived += _gatewayService_OnServerMessageReceived;

            if (_gatewayService.CurrentRoom != null) Text = $"QtC.NET Client - Chat Room - {_gatewayService.CurrentRoom.Name}";
            else if (_gatewayService.InLobby) Text = $"QtC.NET Client - Chat Room - Lobby";
        }

        private async void frmChat_FormClosing(object sender, FormClosingEventArgs e)
        {
            // unsubscribe from server message event
            _gatewayService.OnServerMessageReceived -= _gatewayService_OnServerMessageReceived;

            if (_gatewayService.CurrentRoom != null || _gatewayService.InLobby)
            {
                // leave any room user is in
                await _gatewayService.LeaveRoomAsync();
            }
        }

        private async void btnSend_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(rtxtChatbox.Text))
            {
                // construct message
                QtCNETAPI.Models.Message message = new() { Content = rtxtChatbox.Text };

                // send it and clear text box
                await _gatewayService.PostMessageAsync(message);
                rtxtChatbox.Clear();
                AudioService.PlaySoundEffect("sndSendClick");
            }
        }

        private void rtxtChatbox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnSend_Click(sender, e);
        }

        private void _gatewayService_OnServerMessageReceived(object? sender, EventArgs e)
        {
            var msgEventArgs = (ServerMessageEventArgs)e;

            AddMessage(msgEventArgs.Message);
            if (!msgEventArgs.Message.Contains(_apiService.CurrentUser.Username)) AudioService.PlaySoundEffect("sndMessage");
        }

        private void AddMessage(string message)
        {
            if (InvokeRequired)
                Invoke(delegate { rtxtChat.AppendText(message + Environment.NewLine); });
            else rtxtChat.AppendText(message + Environment.NewLine);
        }
    }
}
