using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace Earthblend.Nts.Common.Statics
{

    [Serializable]
    public static partial class VirtualSystemUtil
    {
        public const String RootDataModelPath = @"C:\DataXts\DataModels\";
        public static String DataModel { get { return _dataModel; } set { _dataModel = value; } }

        public static String PermutationResultDataModelPath
        {
            get { return DataDrive + DataModel + @"PermutationResultDataModels\DefaultPermutationResultDataModel.xml"; }
            //get { return _PermutationResultDataModelPath; }
            //set { _PermutationResultDataModelPath=value;  }
            //public static String PermutationResultDataModelPath = DataDrive + DataModel + @"PermutationResultDataModels\DefaultPermutationResultDataModel.xml";
        }
        public static void PushDispatcherFrame()
        {
            DispatcherFrame frame = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
            Dispatcher.PushFrame(frame);
        }
        public static Object ExitFrame(Object f)
        {
            ((DispatcherFrame)f).Continue = false;

            return null;
        }
        static VirtualSystemUtil()
        {
            if (String.IsNullOrEmpty(OneDriveFolder))
                OneDriveFolder = OneDrivePath();
        }
        private static String _quote = @"""";
        //private static String _resourceDrive = @"D:\";
        private static String _systemDrive = @"C:\";
        private static String _dataDrive = @"C:\";
        //private static String _masterFiles = @"Data\Roslyn\MasterFiles\";
        private static String _dataModel = @"Data\DataModels\";
        //private static String _viewModel = "ViewModel";
        //private static String _systemBackgroundColor = "Yellow";
        //private static String _systemForegroundColor = "Black";
        //private static String _viewModelPath = "ViewModel";
        //private static String _layoutStyle = "Fixed";
        //private static String _dataItemViewType = "List";
        //private static String _masterFilesPath = @"Data\Roslyn\MasterFiles\";

        private static String _compilerMsBuild2015 = @"Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe";
        private static String _compilerDevEnv2015 = @"Program Files (x86)\Microsoft Visual Studio 14.0\2015\Common7\IDE\devenv.exe";

        private static String _compilerMsBuild2017 = @"Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MsBuild.exe";
        private static String _compilerDevEnv2017 = @"Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\devenv.exe";
        private static String _compilerDevEnv2019 = @"Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe";
        private static String _compilerDevPreviewEnv2019 = @"Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\devenv.exe";

        private static String _compilerDebugMsBuild2017CleanParameters = @"/m /t:Clean ";
        private static String _compilerReleaseMsBuild2017CleanParameters = @"/m /t:Clean ";
        private static String _compilerDebugMsBuild2017CleanBuildParameters = @"/m /t:Clean,Rebuild /p:Configuration=Debug ";
        private static String _compilerReleaseMsBuild2017CleanBuildParameters = @"/m /t:Clean,Rebuild /p:Configuration=Release ";

        private static String _compilerDebugDevEnv2017CleanParameters = @"/Clean ";
        private static String _compilerReleaseDevEnv2017CleanParameters = @"/Clean ";
        private static String _compilerDebugDevEnv2017CleanBuildParameters = @"/Rebuild Debug";
        private static String _compilerDebugDevEnv2019CleanBuildParameters = @"/Rebuild Debug";
        private static String _compilerReleaseDevEnv2017CleanBuildParameters = @"/Rebuild Release";


        public static String CompilerMsBuild2015 { get { return _compilerMsBuild2015; } set { _compilerMsBuild2015 = value; } }
        public static String CompilerDevEnv2015 { get { return _compilerDevEnv2015; } set { _compilerDevEnv2015 = value; } }

        public static String CompilerDevPreviewEnv2019 { get { return _compilerDevPreviewEnv2019; } set { _compilerDevPreviewEnv2019 = value; } }
        public static String CompilerDevEnv2019 { get { return _compilerDevEnv2019; } set { _compilerDevEnv2019 = value; } }
        public static String CompilerDevEnv2017 { get { return _compilerDevEnv2017; } set { _compilerDevEnv2017 = value; } }
        public static String CompilerDevEnvPreview2019 { get { return _compilerDevPreviewEnv2019; } set { _compilerDevPreviewEnv2019 = value; } }
        public static String CompilerMsBuild2017 { get { return _compilerMsBuild2017; } set { _compilerMsBuild2017 = value; } }

        public static String CompilerDebugMsBuild2017CleanParameters { get { return _compilerDebugMsBuild2017CleanParameters; } set { _compilerDebugMsBuild2017CleanParameters = value; } }
        public static String CompilerReleaseMsBuild2017CleanParameters { get { return _compilerReleaseMsBuild2017CleanParameters; } set { _compilerReleaseMsBuild2017CleanParameters = value; } }
        public static String CompilerDebugMsBuild2017CleanBuildParameters { get { return _compilerDebugMsBuild2017CleanBuildParameters; } set { _compilerDebugMsBuild2017CleanBuildParameters = value; } }
        public static String CompilerReleaseMsBuild2017CleanBuildParameters { get { return _compilerReleaseMsBuild2017CleanBuildParameters; } set { _compilerReleaseMsBuild2017CleanBuildParameters = value; } }

        public static String CompilerDebugDevEnv2017CleanParameters { get { return _compilerDebugDevEnv2017CleanParameters; } set { _compilerDebugDevEnv2017CleanParameters = value; } }
        public static String CompilerReleaseDevEnv2017CleanParameters { get { return _compilerReleaseDevEnv2017CleanParameters; } set { _compilerReleaseDevEnv2017CleanParameters = value; } }
        public static String CompilerDebugDevEnv2017CleanBuildParameters { get { return _compilerDebugDevEnv2017CleanBuildParameters; } set { _compilerDebugDevEnv2017CleanBuildParameters = value; } }
        public static String CompilerReleaseDevEnv2017CleanBuildParameters { get { return _compilerReleaseDevEnv2017CleanBuildParameters; } set { _compilerReleaseDevEnv2017CleanBuildParameters = value; } }

        public static String CompilerDebugDevEnvPreview2019CleanBuildParameters { get { return _compilerDebugDevEnv2019CleanBuildParameters; } set { _compilerDebugDevEnv2019CleanBuildParameters = value; } }

        public static String Quote { get { return _quote; } set { _quote = value; } }
        public static String SystemDrive { get { return _systemDrive; } set { _systemDrive = value; } }
        public static String DataDrive { get { return _dataDrive; } set { _dataDrive = value; } }

        public static String OneDriveFolder { get; set; }

        public static String GetImageDrive()
        {
            return OneDriveFolder + @"\ScientificResearchSuite\ReferenceData\";
        }

        private static String OneDrivePath()
        {
            const string userRoot = "HKEY_CURRENT_USER";
            const string subkey = @"Software\Microsoft\OneDrive";
            const string keyName = userRoot + "\\" + subkey;

            return (string)Microsoft.Win32.Registry.GetValue(keyName, "UserFolder", "Unknown");
        }

        #region Public Readonly Static Properties

        public static String QuotedCompilerMsBuild2015 { get { return Quote + SystemDrive + CompilerMsBuild2015 + Quote; } }
        public static String QuotedCompilerDevEnv2015 { get { return Quote + SystemDrive + CompilerDevEnv2015 + Quote; } }
        public static String QuotedCompilerMsBuild2017 { get { return Quote + SystemDrive + CompilerMsBuild2017 + Quote; } }
        public static String QuotedCompilerDevEnv2017 { get { return Quote + SystemDrive + CompilerDevEnv2017 + Quote; } }
        public static String QuotedCompilerDevPreviewEnv2019 { get { return Quote + SystemDrive + CompilerDevEnvPreview2019 + Quote; } }
        //public static String TempClonePath { get { return DataDrive + TempCloneBase; } }
        //public static String PlugInsPath { get { return PlugInsPathBase; } }

        #endregion

        public static void SaveRTBAsPNG(RenderTargetBitmap bmp, string filename)
        {
            var enc = new System.Windows.Media.Imaging.PngBitmapEncoder();
            enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp));
            using (var stm = System.IO.File.Create(filename))
            {
                enc.Save(stm);
            }
        }

        public static BitmapSource RenderToBitmap(this UIElement element, double scale, Brush background)
        {
            var renderWidth = (int)(element.RenderSize.Width * scale);
            var renderHeight = (int)(element.RenderSize.Height * scale);

            var renderTarget = new RenderTargetBitmap(renderWidth, renderHeight,
                                                      96, 96, PixelFormats.Default);
            var sourceBrush = new VisualBrush(element);

            var drawingVisual = new DrawingVisual();
            var drawingContext = drawingVisual.RenderOpen();

            var rect = new Rect(0, 0, element.RenderSize.Width, element.RenderSize.Height);

            using (drawingContext)
            {
                drawingContext.PushTransform(new ScaleTransform(scale, scale));
                drawingContext.DrawRectangle(background, null, rect); // here
                drawingContext.DrawRectangle(sourceBrush, null, rect);
            }

            renderTarget.Render(drawingVisual);

            return renderTarget;
        }
        //public static void PushDispatcherFrame()
        //{
        //    DispatcherFrame frame = new DispatcherFrame();
        //    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
        //    Dispatcher.PushFrame(frame);
        //}
    }
}
