GsDChat/src/GsDChat.Client/Screens/ChannelsScreen.cs
2026-10-10 16:40:35 +00:00

161 lines
5.4 KiB
C#

using System;
using System.Text;
using System.Threading.Tasks;
using GsDChat.Client.Network;
using GsDChat.Client.Rendering;
using GsDChat.Client.State;
namespace GsDChat.Client.Screens;
public class ChannelsScreen : IScreen
{
private readonly ClientState _state;
private readonly ChatClientConnection _connection;
private int _selectedIndex = 0;
private bool _isCreating = false;
private string _createBuffer = string.Empty;
public string ScreenId => "F2_Channels";
public string Title => "Channels";
public string ShortcutKey => "F2";
public ChannelsScreen(ClientState state, ChatClientConnection connection)
{
_state = state;
_connection = connection;
}
public void Render(int width, int height, StringBuilder sb)
{
sb.AppendLine($" {Ansi.Bold}{Ansi.Yellow}Channel Browser{Ansi.Reset} {Ansi.Gray}(Press ENTER to join, 'C' to create, 'D' to delete){Ansi.Reset}");
sb.AppendLine(new string('─', Math.Max(0, width)));
if (_state.Channels.Count == 0)
{
sb.AppendLine(" No channels available.");
return;
}
if (_selectedIndex >= _state.Channels.Count) _selectedIndex = _state.Channels.Count - 1;
if (_selectedIndex < 0) _selectedIndex = 0;
for (int i = 0; i < _state.Channels.Count; i++)
{
var ch = _state.Channels[i];
bool isSelected = i == _selectedIndex;
bool isActive = ch.Id == _state.ActiveChannelId;
string prefix = isSelected ? $"{Ansi.BrightYellow}► " : " ";
string activeTag = isActive ? $" {Ansi.BrightGreen}[ACTIVE]{Ansi.Reset}" : "";
string name = $"{Ansi.Bold}#{ch.Name}{Ansi.Reset}";
string topic = string.IsNullOrEmpty(ch.Topic) ? "" : $" {Ansi.Gray}- {ch.Topic}{Ansi.Reset}";
if (isSelected)
{
sb.AppendLine($"{prefix}{Ansi.Inverse}#{ch.Name}{Ansi.Reset}{activeTag}{topic}");
}
else
{
sb.AppendLine($"{prefix}{name}{activeTag}{topic}");
}
}
sb.AppendLine(new string('─', Math.Max(0, width)));
if (_isCreating)
{
sb.AppendLine($" {Ansi.BrightYellow}Create Channel > Name [Topic]:{Ansi.Reset} {_createBuffer}█");
}
else
{
sb.AppendLine($" {Ansi.Gray}[↑/↓] Navigate [Enter] Join Channel [C] Create Channel [D] Delete Channel{Ansi.Reset}");
}
}
public async Task HandleKeyAsync(ConsoleKeyInfo key)
{
if (_isCreating)
{
if (key.Key == ConsoleKey.Enter)
{
string input = _createBuffer.Trim();
_isCreating = false;
_createBuffer = string.Empty;
if (!string.IsNullOrEmpty(input))
{
var parts = input.Split(' ', 2);
string name = parts[0];
string topic = parts.Length > 1 ? parts[1] : string.Empty;
await _connection.CreateChannelAsync(name, topic);
}
_state.NeedsRedraw = true;
}
else if (key.Key == ConsoleKey.Escape)
{
_isCreating = false;
_createBuffer = string.Empty;
_state.NeedsRedraw = true;
}
else if (key.Key == ConsoleKey.Backspace)
{
if (_createBuffer.Length > 0)
{
_createBuffer = _createBuffer[..^1];
_state.NeedsRedraw = true;
}
}
else if (!char.IsControl(key.KeyChar))
{
_createBuffer += key.KeyChar;
_state.NeedsRedraw = true;
}
return;
}
if (key.Key == ConsoleKey.UpArrow)
{
_selectedIndex = Math.Max(0, _selectedIndex - 1);
_state.NeedsRedraw = true;
}
else if (key.Key == ConsoleKey.DownArrow)
{
_selectedIndex = Math.Min(_state.Channels.Count - 1, _selectedIndex + 1);
_state.NeedsRedraw = true;
}
else if (key.Key == ConsoleKey.Enter)
{
if (_selectedIndex >= 0 && _selectedIndex < _state.Channels.Count)
{
var selected = _state.Channels[_selectedIndex];
await _connection.JoinChannelAsync(selected.Id);
_state.ActiveScreenId = "F1_Chat";
_state.SetStatus($"Switched to #{selected.Name}");
_state.NeedsRedraw = true;
}
}
else if (key.Key == ConsoleKey.C)
{
_isCreating = true;
_createBuffer = string.Empty;
_state.NeedsRedraw = true;
}
else if (key.Key == ConsoleKey.D)
{
if (_selectedIndex >= 0 && _selectedIndex < _state.Channels.Count)
{
var selected = _state.Channels[_selectedIndex];
if (selected.Name == "general")
{
_state.SetStatus("Cannot delete the general channel.");
}
else
{
await _connection.DeleteChannelAsync(selected.Id);
_state.SetStatus($"Deleting #{selected.Name}...");
}
_state.NeedsRedraw = true;
}
}
}
}