using Microsoft.Win32;
using System;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Windows;

namespace ShaferExample
{
    public static class WinSparkle
    {
        // Read more about basic setup https://github.com/vslavik/winsparkle/wiki/Basic-Setup
        // Note that some of these functions are not implemented by WinSparkle YET.
        [DllImport("WinSparkle.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void win_sparkle_init();
        [DllImport("WinSparkle.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void win_sparkle_cleanup();
        [DllImport("WinSparkle.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern void win_sparkle_set_appcast_url(String url);
        [DllImport("WinSparkle.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        public static extern void win_sparkle_set_app_details(String company_name,
                                             String app_name,
                                             String app_version);
        [DllImport("WinSparkle.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern void win_sparkle_set_registry_path(String path);
        [DllImport("WinSparkle.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void win_sparkle_check_update_with_ui();

        public static void Init()
        {
            SetUpdateReg();
            var url = ConfigurationManager.AppSettings["rssUrl"];
            if (!string.IsNullOrWhiteSpace(url))
            {
                _initialized = true;
                win_sparkle_set_appcast_url(url);
            }
        }

        public static void Setup(double? maintenanceIn)
        {
            //Stop Winsparkle when maintanance expired
            if (_initialized)
            {
                if ((maintenanceIn == null && _on == true) || (maintenanceIn <= 0 && _on == true))
                {
                    CleanUp();
                }
                else if (maintenanceIn > 0 && _on == false)
                {
                    Init();
                    win_sparkle_init();
                    _on = true;
                }
            }
        }

        public static void CleanUp()
        {
            if (_on)
            {
                win_sparkle_cleanup();
                _on = false;
            }
        }

        public static void Update(double? maintenanceIn)
        {
            if (_initialized)
            {
                Setup(maintenanceIn);
                if (maintenanceIn == null)
                {
                    MessageBox.Show("You are not under maintenance, please contact " +
                        "your Elevate sales rep in order to purchase access to ongoing updates and enhancements.");
                }
                else if (maintenanceIn > 0)
                {
                    win_sparkle_check_update_with_ui();
                }
                else if (maintenanceIn <= 0)
                {
                    MessageBox.Show("Your maintenance contract has expired.Please contact your Elevate sales rep in order to renew your maintenance" +
                        " agreement so you can have access to all the recent updates and enhancements.");
                }
            } else
            {
                MessageBox.Show("RSS feed not set");
            }
        }

        public static void SetUpdateReg()
        {
            //Prevent asking if user want to check for updated versions
            Registry.SetValue(KEYNAME, "DidRunOnce", "1");
            Registry.SetValue(KEYNAME, "CheckForUpdates", "1");
        }

        private static bool _initialized = false;
        private static bool _on = false;
        private const string USERROOT = "HKEY_CURRENT_USER";
        private const string SUBKEY = "Software\\ShaferExample\\WinSparkle";
        private const string KEYNAME = USERROOT + "\\" + SUBKEY;
    }
}
