269 lines
10 KiB
C#

using QtCNETAPI.Dtos.User;
using QtCNETAPI.Services.ApiService;
using QtCNETAPI.Models;
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;
using QtCNETAPI.Services.GatewayService;
using qtc_net_client_2.ClientModel;
using qtc_net_client_2.Properties;
using qtc_net_client_2.Services;
using QtCNETAPI.Schema;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Drawing.Design;
namespace qtc_net_client_2.Forms
{
public partial class Profile : Form
{
private UserInformationDto _userInformationDto;
private IApiService _apiService;
private IGatewayService _gatewayService;
private ServiceResponse<byte[]>? pfpRes;
private List<Contact> contactsList;
public Profile(UserInformationDto userInfo, ServiceResponse<byte[]>? pfp, List<Contact> contacts, IApiService apiService, IGatewayService gatewayService)
{
_userInformationDto = userInfo;
_apiService = apiService;
_gatewayService = gatewayService;
pfpRes = pfp;
contactsList = contacts;
InitializeComponent();
}
private async void frmProfile_Load(object sender, EventArgs e)
{
lblUsername.Text = _userInformationDto.Username;
lblCurrencyAmount.Text = _userInformationDto.CurrencyAmount.ToString("N0");
rtxtBio.Text = _userInformationDto.Bio;
pbUserPfp.Location = new(13, 11);
pbUserPfp.Size = new(128, 128);
if (pfpRes != null && pfpRes.Success && pfpRes.Data != null)
{
using (var ms = new MemoryStream(pfpRes.Data))
{
Bitmap bmp = new Bitmap(ms);
if (bmp.Width != 128 && bmp.Height != 128)
bmp.SetResolution(128, 128);
pbUserPfp.Image = bmp;
}
}
var userStatus = (UserStatus)_userInformationDto.Status;
switch (userStatus)
{
case UserStatus.Online:
pbUserStatus.Image = Resources.OnlineIcon;
break;
case UserStatus.Away:
pbUserStatus.Image = Resources.AwayIcon;
break;
case UserStatus.DoNotDisturb:
pbUserStatus.Image = Resources.DNDIcon;
break;
case UserStatus.Offline:
pbUserStatus.Image = Resources.OfflineIcon;
break;
}
if(_userInformationDto.ProfileCosmeticId != 0)
{
var res = await _apiService.GetStoreItem(_userInformationDto.ProfileCosmeticId);
if (res != null && res.Success && res.Data != null)
{
var client = new HttpClient();
var response = await client.GetAsync(res.Data.AssetUrl);
if (response.IsSuccessStatusCode)
{
using(var stream = await response.Content.ReadAsStreamAsync())
{
CombineProfileImageWithCosmetic(pbUserPfp.Image, new Bitmap(stream));
}
response.Dispose();
}
client.Dispose();
}
}
if (_userInformationDto.Id == _apiService.CurrentUser!.Id)
{
btnAddContact.Visible = false;
btnMessage.Visible = false;
return; // do not run contact getting code
}
else btnAddContact.Visible = true;
if (contactsList != null)
{
var contact = contactsList.FirstOrDefault(e => e.UserId == _userInformationDto.Id || e.OwnerId == _userInformationDto.Id);
if (contact != null)
{
if (contact.OwnerId == _apiService.CurrentUser.Id)
{
// the user requesting this profile initiated a contact relationship with this user
switch (contact.OwnerStatus)
{
case Contact.ContactStatus.AwaitingApprovalFromOther:
btnAddContact.Enabled = false;
using (var ms = new MemoryStream(Resources.RequestSentIcon)) { btnAddContact.Image = Image.FromStream(ms); ms.Dispose(); }
btnCancelRequest.Visible = true;
break;
case Contact.ContactStatus.Accepted:
btnAddContact.Visible = true;
btnAddContact.Enabled = true;
btnAddContact.Image = Resources.RemoveContactIcon;
btnAddContact.Click += btnAddContact_Click_Remove;
if (_userInformationDto.Status >= 1) btnMessage.Visible = true;
break;
}
}
else if (contact.UserId == _apiService.CurrentUser.Id)
{
// the user requesting this profile did not initiate a contact relationship with the user
switch (contact.UserStatus)
{
case Contact.ContactStatus.AwaitingApprovalFromSelf:
btnAddContact.Visible = false;
btnAccept.Visible = true;
btnDecline.Visible = true;
btnCancelRequest.Visible = false;
break;
case Contact.ContactStatus.Accepted:
btnAccept.Visible = false;
btnDecline.Visible = false;
btnCancelRequest.Visible = false;
btnAddContact.Visible = true;
btnAddContact.Enabled = true;
btnAddContact.Image = Resources.RemoveContactIcon;
btnAddContact.Click += btnAddContact_Click_Remove;
if (_userInformationDto.Status >= 1) btnMessage.Visible = true;
break;
}
}
}
else
{
btnAddContact.Visible = true;
btnAddContact.Click += btnAddContact_Click_Add;
}
}
else
{
btnAddContact.Visible = true;
btnAddContact.Click += btnAddContact_Click_Add;
}
}
private void Profile_FormClosed(object sender, FormClosedEventArgs e)
{
_userInformationDto = null!; // ignoring warning since this is just disposing the form
pfpRes = null;
}
private async void btnAddContact_Click_Add(object sender, EventArgs e)
{
var result = await _apiService.AddContactToCurrentUser(_userInformationDto.Id);
if (result.Success)
{
btnAddContact.Enabled = false;
using (var ms = new MemoryStream(Resources.RequestSentIcon)) { btnAddContact.Image = Image.FromStream(ms); ms.Dispose(); }
btnCancelRequest.Visible = true;
}
}
private async void btnAddContact_Click_Remove(object sender, EventArgs e)
{
var result = await _apiService.RemoveContactFromCurrentUser(_userInformationDto.Id);
if (result.Success)
{
btnAddContact.Image = Resources.AddContactIcon;
btnAddContact.Click += btnAddContact_Click_Add;
btnMessage.Visible = false;
}
}
private async void btnAccept_Click(object sender, EventArgs e)
{
var result = await _apiService.AcceptContactRequest(_userInformationDto.Id);
if (result.Success)
{
btnAccept.Visible = false;
btnDecline.Visible = false;
btnAddContact.Visible = true;
btnAddContact.Image = Resources.RemoveContactIcon;
btnAddContact.Click += btnAddContact_Click_Remove;
if (_userInformationDto.Status >= 1) btnMessage.Visible = true;
}
}
private async void btnDecline_Click(object sender, EventArgs e)
{
var result = await _apiService.RemoveContactFromCurrentUser(_userInformationDto.Id);
if (result.Success)
{
btnAccept.Visible = false;
btnDecline.Visible = false;
btnAddContact.Visible = true;
btnAddContact.Image = Resources.AddContactIcon;
btnAddContact.Click += btnAddContact_Click_Add;
}
}
private async void btnCancelRequest_Click(object sender, EventArgs e)
{
var result = await _apiService.RemoveContactFromCurrentUser(_userInformationDto.Id);
if (result.Success)
{
btnAddContact.Enabled = true;
btnAddContact.Image = Resources.AddContactIcon;
btnAddContact.Click += btnAddContact_Click_Add;
btnCancelRequest.Visible = false;
}
}
private void btnMessage_Click(object sender, EventArgs e)
{
DirectMessage frmDirectMessage = new DirectMessage(_gatewayService, _apiService, _userInformationDto);
Close();
frmDirectMessage.Show();
}
private void CombineProfileImageWithCosmetic(Image pfp, Bitmap cosmetic)
{
cosmetic.MakeTransparent();
Bitmap combined = new Bitmap(139, 138);
using (Graphics g = Graphics.FromImage(combined))
{
g.Clear(Color.Transparent);
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
g.DrawImage(pfp, 4, 6, 128, 128);
g.DrawImage(cosmetic, 0, 0, 139, 138);
}
pbUserPfp.Location = new(9, 5);
pbUserPfp.Size = new(139, 138);
pbUserPfp.Image = combined;
}
}
}