using QtCNETAPI.Models;
using QtCNETAPI.Services.ApiService;
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using Microsoft.Extensions.FileProviders;
using qtcnet_client.Properties;

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

        public async Task<Bitmap> GetAndCreateProfileImage(string userId, int status = -1, int cosmeticStoreId = 0)
        {
            Bitmap combined = new(139, 138);

            Bitmap img1 = Resources.DefaultPfp;
            Bitmap? img2 = null;
            Bitmap img3 = Resources.OfflineIcon;

            var _pfpRes = await _apiService.GetUserProfilePic(userId);
            if (_pfpRes.Success && _pfpRes.Data != null)
            {
                using var ms = new MemoryStream(_pfpRes.Data);
                img1 = (Bitmap)Image.FromStream(ms);
            }

            // get cosmetic image
            if (cosmeticStoreId >= 0)
            {
                var _storeRes = await _apiService.GetStoreItem(cosmeticStoreId);
                if (_storeRes.Success && _storeRes.Data != null)
                {
                    using var _http = new HttpClient();
                    using var _httpRes = await _http.GetAsync(_storeRes.Data.AssetUrl);
                    if (_httpRes.IsSuccessStatusCode)
                    {
                        var _imgByes = _httpRes.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
                        using var ms = new MemoryStream(_imgByes);
                        img2 = (Bitmap?)Image.FromStream(ms);
                    }
                }
            }

            // set status image
            if (!int.IsNegative(status))
            {
                switch (status)
                {
                    case 0:
                        img3 = Resources.OfflineIcon;
                        break;
                    case 1:
                        img3 = Resources.OnlineIcon;
                        break;
                    case 2:
                        img3 = Resources.AwayIcon;
                        break;
                    case 3:
                        img3 = Resources.DNDIcon;
                        break;
                }
            }

            // finally, combine the images
            Graphics 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.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
        }
    }
}
