using Afnor.Silverlight.Toolkit.Services;
using Afnor.Silverlight.Toolkit.ViewServices;
using EspaceClient.BackOffice.Silverlight.Business.Depots;
using EspaceClient.BackOffice.Silverlight.Business.Interfaces;
using EspaceClient.BackOffice.Silverlight.Business.Loader;
using EspaceClient.BackOffice.Silverlight.ViewModels.Common.Modularity;
using EspaceClient.BackOffice.Silverlight.ViewModels.Helper.Reference;
using EspaceClient.BackOffice.Silverlight.ViewModels.ModelBuilders.Common;
using EspaceClient.BackOffice.Silverlight.ViewModels.ModelBuilders.References.Reference;
using nRoute.Components.Composition;
using nRoute.ViewServices.Contracts;
using OGDC.Silverlight.Toolkit.Services.Services;

namespace EspaceClient.BackOffice.Silverlight.ViewModels.References.Reference
{
    public class ReferenceViewModel : ModuleBase
    {
        private readonly ILoaderReferentiel _referentiel;
        private readonly IResourceWrapper _resourceWrapper;
        private readonly INavigatableChildWindowService _childWindowService;
        private readonly IMessenging _messengingService;
        private readonly IApplicationContext _applicationContext;

        private readonly IModelBuilderSearch _builderSearch;
        private readonly IModelBuilderDetails _builderDetails;

        private string _typeOM;
        private string _traductionTypeOM;

        /// <summary>
        /// Initialise une nouvelle instance de la page d'acceuil.
        /// </summary>
        /// <param name="referentiel">Loader des référentiels.</param>
        [ResolveConstructor]
        public ReferenceViewModel(
            ILoaderReferentiel referentiel,
            IResourceWrapper resourceWrapper,
            INavigatableChildWindowService childWindowService,
            IMessenging messengingService,
            IApplicationContext applicationContext,
            IModelBuilderTranslator translator,
            IModelBuilderSearch builderSearch,
            IModelBuilderDetails builderDetails,
            IBrowserNavigationService browserNavigationService,
            IDepotPieceJointe pieceJointes,
            IOpenFileViewService openFileService)
            : base(applicationContext, messengingService, childWindowService, translator, browserNavigationService, pieceJointes, resourceWrapper, openFileService)
        {
            _referentiel = referentiel;
            _resourceWrapper = resourceWrapper;
            _childWindowService = childWindowService;
            _messengingService = messengingService;
            _applicationContext = applicationContext;

            _builderSearch = builderSearch;
            _builderDetails = builderDetails;
        }

        public string TypeOM
        {
            get { return _typeOM; }
            set { _typeOM = value; }
        }

        public string TraductionTypeOM
        {
            get { return _traductionTypeOM; }
            set { _traductionTypeOM = value; }
        }


        protected override void OnIntialize(nRoute.Components.ParametersCollection state)
        {
            if (state.Count == 2)
            {
                TypeOM = state["TypeOM"].ToString();
                TraductionTypeOM = state["TraductionTypeOM"].ToString();
            }

            base.OnIntialize(state);

            InitializeUI();
        }


        public void InitializeUI()
        {
            ReferenceHelper.AddSearchTab(TypeOM, _messengingService, _resourceWrapper, _builderSearch);

            SelectedTabIndex = 0;
        }


        protected override void OnRequestAdd()
        {
            ReferenceHelper.AddNewTab(TypeOM, _messengingService, _resourceWrapper, _builderDetails);
        }
    }
}
