using System;
using System.Collections.Generic;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using System.Linq;
using System.IO;
using Axiom.Utils;

namespace PortfolioManager.Cache
{
  public class ImageCache : IDisposable
  {
    public enum ImageType {BlueTriangleUp,GreenTriangleUp,GreenTriangleDown,RedTriangleUp,RedTriangleDown,YellowTriangleUp,
      OrangeTriangleUp, AmberTriangleUp,MagentaTriangleUp, DarkBlueTriangleUp}
    private Dictionary<ImageType,Bitmap> imageCache = new Dictionary<ImageType,Bitmap>();
    private Object thisLock=new Object();
    private static ImageCache imageCacheInstance=null;

    private ImageCache()
    {
      String currentDirectory = Directory.GetCurrentDirectory();
      String pathToAssets = currentDirectory;
      if (!Directory.Exists(currentDirectory + "/Assets"))
      {
        pathToAssets = currentDirectory + "/publish" + "/Assets";
      }
      else
      {
        pathToAssets = currentDirectory + "/Assets";
      }
      MDTrace.WriteLine(LogLevel.DEBUG, $"Reading assets from {pathToAssets}");
      imageCache.Add(ImageCache.ImageType.BlueTriangleUp, new Bitmap(pathToAssets + "/BlueTriangleUp.png"));
      imageCache.Add(ImageCache.ImageType.GreenTriangleUp, new Bitmap(pathToAssets + "/GreenTriangleUp.png"));
      imageCache.Add(ImageCache.ImageType.GreenTriangleDown, new Bitmap(pathToAssets + "/GreenTriangleDown.png"));
      imageCache.Add(ImageCache.ImageType.RedTriangleUp, new Bitmap(pathToAssets + "/RedTriangleUp.png"));
      imageCache.Add(ImageCache.ImageType.RedTriangleDown, new Bitmap(pathToAssets + "/RedTriangleDown.png"));
      imageCache.Add(ImageCache.ImageType.YellowTriangleUp, new Bitmap(pathToAssets + "/YellowTriangleUp.png"));
      imageCache.Add(ImageCache.ImageType.OrangeTriangleUp, new Bitmap(pathToAssets + "/OrangeTriangleUp.png"));
      imageCache.Add(ImageCache.ImageType.AmberTriangleUp, new Bitmap(pathToAssets + "/AmberTriangleUp.png"));
      imageCache.Add(ImageCache.ImageType.MagentaTriangleUp,new Bitmap(pathToAssets+"/MagentaTriangleUp.png"));
      imageCache.Add(ImageCache.ImageType.DarkBlueTriangleUp,new Bitmap(pathToAssets+"/DarkBlueTriangleUp.png"));
    }
    
    public static ImageCache GetInstance()
    {
      lock (typeof(SymbolCache))
      {
        if (null == imageCacheInstance) imageCacheInstance = new ImageCache();
        return imageCacheInstance;
      }
    }
    
    public void Clear()
    {
      lock (thisLock)
      {
        imageCache = new Dictionary<ImageType, Bitmap>();
      }
    }
    
    public void Dispose()
    {
      lock (thisLock)
      {
        if (null == imageCacheInstance) return;
        List<Bitmap> bitmaps = imageCache.Values.ToList();
        foreach (Bitmap bitmap in bitmaps)
        {
          bitmap.Dispose();
        }
        imageCache = null;
        imageCacheInstance = null;
      }
    }

    public IImage GetImage(ImageCache.ImageType imageType)
    {
      lock(this)
      {
        return imageCache[imageType];
      }
    }
  }
}
