﻿using System;
using System.Windows.Input;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.PubSubEvents;
using SoftMod.Infrastructure.EventAggregation;
using Microsoft.Practices.ServiceLocation;

namespace SoftMod.StartModule.ViewModels
{
    [Export]
    public class StartViewModel
    {
        /// <summary>
        /// Start view model class
        /// </summary>
         // Declare region manager
        [Import]
        private IRegionManager regionManager { get; set; }
        [Import]
        private IServiceLocator serviceLocator { get; set; }
        // Declare raise new file command
        public ICommand RaiseNewFileCommand { get; private set; }
        // Declare event aggregator
        public IEventAggregator aggregator { get; set; }
        [ImportingConstructor]
        // Initialise
        public StartViewModel()
        {
            // Set delegates
            this.RaiseNewFileCommand = new DelegateCommand(this.OnRaiseNewFileCommand);
            // Set event aggregator
            this.aggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            // Set region manager
            regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
        }
        /// <summary>
        /// Raise new file command method
        /// </summary>
        private void OnRaiseNewFileCommand()
        {
            // Raise new file event
            this.aggregator.GetEvent<NewFile>().Publish(true);
            // Remove start view from grid region
            var view = this.regionManager.Regions["GridRegion"].GetView("StartView");
            // Get view and remove from region
            this.regionManager.Regions["GridRegion"].Remove(view);
        }

    }
}
