using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;

namespace Valg.Eva.Skanning.Scan.Presentation.MainWindow
{
    public partial class MainWindow : IMainWindow
    {
        private IMainWindowViewModel viewModel;

        public MainWindow(IMainWindowViewModel viewModel)
        {

            if (Application.ResourceAssembly == null)
                Application.ResourceAssembly = typeof(MainWindow).Assembly;

            InitializeComponent();
            this.viewModel = viewModel;
            DataContext = viewModel;
        }

        
        
        
        

        protected override void OnClosing(CancelEventArgs e)
        {
            Dispatcher.InvokeShutdown(); 
            viewModel = null;

            base.OnClosing(e);
        }


        public void ShowWindow(bool isElectionConfigInstalled, bool isKioskMode)
        {
            viewModel.Initialize(isElectionConfigInstalled);
           
          
            Show();

            if (isElectionConfigInstalled)
            {
               viewModel.Login();
            }
            else
            {
                viewModel.ShowElectionConfigMissingWindow();
            }
            if (isKioskMode)
            {
                HideMinimizeButton();
            }
        }

        private void HideMinimizeButton()
        {
            var hwnd = new WindowInteropHelper(this).Handle;
            Valg.EVA.Skanning.Tools.WindowExtensions.HideMinimizeButton(hwnd);

        }

        public Window AsWindow { get { return this; } }
    }
}

