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 Bitmap GetAndCreateProfileImage(string userId, int status = -1, int cosmeticStoreId = 0)
        {
            // get images from api

            // profile pic
            Bitmap img1 = Resources.DefaultPfp;
            var _pfpTask = Task.Run(() => _apiService.GetUserProfilePic(userId));
            var _pfpRes = _pfpTask.Result;
            if(_pfpRes.Success && _pfpRes.Data != null)
            {
                using var ms = new MemoryStream(_pfpRes.Data);
                img1 = new Bitmap(ms);
            }

            // cosmetic
            Bitmap? img2 = null;
            var _storeItemTask = Task.Run(() => _apiService.GetStoreItem(cosmeticStoreId));
            var _storeItem = _storeItemTask.Result;
            if(_storeItem.Success && _storeItem.Data != null)
            {
                var _webTask = Task.Run(() => _http.GetAsync(_storeItem.Data.AssetUrl));
                using var _webRes = _webTask.Result;
                if (_webRes.IsSuccessStatusCode)
                    img2 = new Bitmap(_webRes.Content.ReadAsStream());
            }

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

            // return combined
            return CombineImages(img1, img2, img3);
        }

        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 new Bitmap(await _res.Content.ReadAsStreamAsync());
            }

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

        private static Bitmap CombineImages(Bitmap img1, Bitmap? img2, Bitmap? img3)
        {
            Bitmap combined = new(139, 138);
            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(img3 != null)
            {
                img3.MakeTransparent();
                g.DrawImage(img3, 104, 0, 35, 35);
            }

            return combined;
        }
    }
}
