using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Media.Imaging;
using Newtonsoft.Json;

namespace HistoryWallEditTool.Helper
{
    public class UtilityFunctions
    {
        public static T DeserializeFromJson<T>(string json)
        {
            T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
            return deserializedProduct;
        }

        public static BitmapImage LoadBitmapImage(string path, int width = 0)
        {
            if (File.Exists(path))
            {
                try
                {
                    BitmapImage bitmapImg = new BitmapImage();
                    bitmapImg.BeginInit();
                    bitmapImg.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
                    bitmapImg.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                    bitmapImg.CacheOption = BitmapCacheOption.OnLoad;
                    if (width != 0)
                    {
                        bitmapImg.DecodePixelWidth = width;
                    }
                    bitmapImg.EndInit();
                    bitmapImg.Freeze();
                    return bitmapImg;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
    }
}
