using qtcnet_client.Properties;
using QtCNETAPI.Models;
using QtCNETAPI.Schema;
using QtCNETAPI.Services.ApiService;
using System.Drawing.Drawing2D;

namespace qtcnet_client.Factories
{
    public class ImageFactory
    {
        private readonly IApiService _apiService;
        private readonly HttpClient _http = new();
        public ImageFactory(IApiService apiService)
        {
            _apiService = apiService;
        }

        public async Task<Bitmap> GetAndCreateProfileImage(string userId, int status = -1, int cosmeticStoreId = 0)
        {
            var pfpTask = _apiService.GetUserProfilePic(userId);
            var cosmeticTask = cosmeticStoreId >= 0 ? _apiService.GetStoreItem(cosmeticStoreId) : Task.FromResult<ServiceResponse<StoreItem>?>(null)!;

            return await Task.Run(async () =>
            {
                Bitmap img1 = Resources.DefaultPfp;
                Bitmap? img2 = null;
                Bitmap img3 = Resources.OfflineIcon;

                var pfpRes = await pfpTask;
                if (pfpRes.Success && pfpRes.Data != null)
                {
                    using var ms = new MemoryStream(pfpRes.Data);
                    img1 = (Bitmap)Image.FromStream(ms);
                }

                if (cosmeticStoreId > 0)
                {
                    var storeRes = await cosmeticTask;
                    if (storeRes?.Success == true)
                    {
                        using var httpRes = await _http.GetAsync(storeRes.Data?.AssetUrl);
                        if (httpRes.IsSuccessStatusCode)
                        {
                            var bytes = await httpRes.Content.ReadAsByteArrayAsync();
                            using var ms = new MemoryStream(bytes);
                            img2 = (Bitmap)Image.FromStream(ms);
                        }
                    }
                }

                Bitmap combined = new(139, 138);
                using var g = Graphics.FromImage(combined);

                g.Clear(Color.Transparent);
                g.CompositingMode = CompositingMode.SourceOver;
                g.DrawImage(img1, 4, 6, 128, 128);

                if (img2 != null)
                {
                    img2.MakeTransparent();
                    g.DrawImage(img2, 0, 0, 139, 138);
                }

                if (!int.IsNegative(status))
                {
                    img3 = status switch
                    {
                        1 => Resources.OnlineIcon,
                        2 => Resources.AwayIcon,
                        3 => Resources.DNDIcon,
                        _ => Resources.OfflineIcon
                    };

                    img3.MakeTransparent();
                    g.DrawImage(img3, 104, 0, 35, 35);
                }

                return combined;
            });
        }

        public async Task<Bitmap> GetStoreItemThumb(int id)
        {
            // get store item
            var _item = await _apiService.GetStoreItem(id);
            if(_item.Success && _item.Data != null)
            {
                using var _http = new HttpClient();
                using var _res = await _http.GetAsync(_item.Data.ThumbnailUrl);
                if (_res.IsSuccessStatusCode)
                    return (Bitmap)Image.FromStream(_res.Content.ReadAsStream());
            }

            return Resources.QtCNETIcon; // temp no image icon
        }
    }
}
