using Krypton.Toolkit;
using QtCNETAPI.Models;
using QtCNETAPI.Services.ApiService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace qtcnet_client.Forms
{
    public partial class AdminPanelForm : Form
    {
        private IApiService _apiService;
        public AdminPanelForm(IApiService apiService)
        {
            _apiService = apiService;
            InitializeComponent();
        }

        private async void AdminPanelForm_Load(object sender, EventArgs e)
        {
            // get data
            var _roomData = await _apiService.GetAllRoomsAsync();
            var _userData = await _apiService.GetAllUsersAsync();

            if (_roomData.Success && _roomData.Data != null)
                bsRooms.DataSource = _roomData.Data;

            if (_userData.Success && _userData.Data != null)
                bsUsers.DataSource = _userData.Data;

            dgUsers.Update();
            dgRooms.Update();
        }

        private async void tsiAddRoom_Click(object sender, EventArgs e)
        {
            // create a form to get room name
            AddRoomForm _nameForm = new();
            var _result = _nameForm.ShowDialog();
            if (_result == DialogResult.OK)
            {
                // create the room and refresh room list
                var now = DateTime.UtcNow;
                var _apiRes = await _apiService.CreateRoomAsync(new()
                {
                    Name = _nameForm.RoomName,
                    CreatedAt = now,
                });

                if (_apiRes.Success)
                    AdminPanelForm_Load(sender, e); // refresh data
            }
        }

        private async void dgRooms_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (dgRooms.CurrentCell?.Value is string roomId)
            {
                DialogResult _res = KryptonMessageBox.Show("Are You Sure You Want To Delete This Room?\n" +
                    "Deleting Rooms Also Kicks All Users In It Out.", "are you sure...?",
                    KryptonMessageBoxButtons.YesNo, KryptonMessageBoxIcon.Question);

                if (_res == DialogResult.Yes)
                {
                    var _apiRes = await _apiService.DeleteRoomAsync(roomId);
                    if (_apiRes.Success)
                        AdminPanelForm_Load(sender, e); // refresh data
                }

                e.Cancel = true; // cancel the local delete, refresh should get rid of it
            }
        }

        private async void dgUsers_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if(dgUsers.CurrentCell?.Value is string userId)
            {
                if(_apiService.CurrentUser?.Id == userId)
                {
                    KryptonMessageBox.Show("lol why would you delete yourself", "what");
                    e.Cancel = true;
                    return;
                }

                DialogResult _res = KryptonMessageBox.Show("Deleting Users Should Only Be Done As A Last Resort. Proper Moderation Features Are Coming Soon.\n" +
                    "Are You Sure?", "are you sure...?",
                    KryptonMessageBoxButtons.YesNo, KryptonMessageBoxIcon.Warning);

                if (_res == DialogResult.Yes)
                {
                    // delete the user
                    var _apiRes = await _apiService.DeleteUserById(userId);
                    if(_apiRes.Success)
                        AdminPanelForm_Load(sender, e); // refresh data
                }

                e.Cancel = true; // cancel the local delete, refresh should get rid of it
            }
        }
    }
}
