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;

namespace qtc_net_client_2.Forms
{
    public partial class Profile : Form
    {
        UserInformationDto _userInformationDto;
        IApiService _apiService;
        IGatewayService _gatewayService;

        ServiceResponse<byte[]>? pfpRes;
        public Profile(UserInformationDto userInfo, ServiceResponse<byte[]>? pfp, IApiService apiService, IGatewayService gatewayService)
        {
            _userInformationDto = userInfo;
            _apiService = apiService;
            _gatewayService = gatewayService;
            pfpRes = pfp;

            InitializeComponent();
        }

        private async void frmProfile_Load(object sender, EventArgs e)
        {
            var contactsList = await _apiService.GetCurrentUserContacts();

            lblUsername.Text = _userInformationDto.Username;
            lblCurrencyAmount.Text = _userInformationDto.CurrencyAmount.ToString();
            rtxtBio.Text = _userInformationDto.Bio;

            if (pfpRes != null && pfpRes.Success && pfpRes.Data != null)
            {
                using (var ms = new MemoryStream(pfpRes.Data))
                {
                    pbUserPfp.Image = Image.FromStream(ms);
                    ms.Dispose();
                }
            }

            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.Id == _apiService.CurrentUser!.Id)
            {
                btnAddContact.Visible = false;
                btnMessage.Visible = false;
                return; // do not run contact getting code
            }
            else btnAddContact.Visible = true;

            if (contactsList != null && contactsList.Success && contactsList.Data != null)
            {
                var contact = contactsList.Data.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;

                await _gatewayService.RefreshContactsForUser(_userInformationDto);
            }
        }

        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;

                await _gatewayService.RefreshContactsForUser(_userInformationDto);
            }
        }

        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;

                await _gatewayService.RefreshContactsForUser(_userInformationDto);
            }
        }

        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;

                await _gatewayService.RefreshContactsForUser(_userInformationDto);
            }
        }

        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;

                await _gatewayService.RefreshContactsForUser(_userInformationDto);
            }
        }

        private void btnMessage_Click(object sender, EventArgs e)
        {
            DirectMessage frmDirectMessage = new DirectMessage(_gatewayService, _apiService, _userInformationDto);
            Close();
            frmDirectMessage.Show();
        }
    }
}
