using Earthblend.Tts.Common.Core.Statics;
using System;
using System.IO;
using System.Windows.Markup;
using System.Windows.Media.Imaging;

namespace Earthblend.Tts.Common.Core.Markups
{
    public class EnvironmentVarExtension : MarkupExtension
    {
        private string _variableName;

        public EnvironmentVarExtension(string variableName)
        {
            _variableName = variableName;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            //if (File.Exists(VirtualSystemUtil.AppImagesStorageFolder + VariableName))
            //{
            //    return new BitmapImage(new Uri(VirtualSystemUtil.AppImagesStorageFolder + VariableName));
            //}
            //else 
            //{
                FileInfo fileInfo = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
                var executingAssemblySplit = fileInfo.DirectoryName + @"\";
                fileInfo = null;

                String embeddedResource = executingAssemblySplit + @"\Resources\Images\" + VariableName;
                if(File.Exists(embeddedResource))
                {
                    return new BitmapImage(new Uri(embeddedResource));
                }
                else return new BitmapImage();
            //}
        }

        [ConstructorArgument("variableName")]
        public string VariableName
        {
            get { return _variableName; }
            set { _variableName = value; }
        }
    }

    public class ImagePathExtension : MarkupExtension
    {
        private string _variableName;

        public ImagePathExtension(string variableName)
        {
            _variableName = variableName;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return VirtualSystemUtil.AppImagesStorageFolder + VariableName;
        }

        [ConstructorArgument("variableName")]
        public string VariableName
        {
            get { return _variableName; }
            set { _variableName = value; }
        }
    }
}