using System;
using System.Collections.Generic;
using EspaceClient.BackOffice.Domaine.Results;
using EspaceClient.BackOffice.Silverlight.Business.Depots;
using EspaceClient.BackOffice.Silverlight.Business.Interfaces;
using EspaceClient.BackOffice.Silverlight.Infrastructure.Services;
using EspaceClient.FrontOffice.Domaine;
using nRoute.Components.Composition;
using EspaceClient.BackOffice.Silverlight.Infrastructure.ServicePieceJointe;

namespace EspaceClient.BackOffice.Silverlight.Data.Depots
{
    /// <summary>
    /// Dépôt des pieces jointes
    /// </summary>
    [MapResource(typeof(IDepotPieceJointe), InstanceLifetime.Singleton)]
    public class DepotPieceJointe : IDepotPieceJointe
    {
        private readonly IServiceClientFactory _services;
        private readonly IApplicationContext _applicationContext;

        /// <summary>
        /// Initialise une nouvelle instance du dépôt.
        /// </summary>
        /// <param name="services">Fournisseur de services.</param>
        /// <param name="scheduler">Scheduler de tâches.</param>
        [ResolveConstructor]
        public DepotPieceJointe(IApplicationContext applicationContext, IServiceClientFactory services)
        {
            _applicationContext = applicationContext;
            _services = services;
        }


        public void UploadPieceJointeStarted(PieceJointeDto pieceJointe, Action<PieceJointeDto> callback, Action<Exception> error)
        {
            _services.ServicePieceJointe.Call<UploadPieceJointeStartedCompletedEventArgs>(
                s => s.UploadPieceJointeStartedAsync(_applicationContext.Context, pieceJointe),
                r =>
                {
                    if (r.Error != null)
                        error(r.Error);
                    else
                        callback(r.Result);
                });
        }

        public void UploadPieceJointeFinished(PieceJointeDto pieceJointe, Action<PieceJointeResult> callback, Action<Exception> error)
        {
            _services.ServicePieceJointe.Call<UploadPieceJointeFinishedCompletedEventArgs>(
                s => s.UploadPieceJointeFinishedAsync(_applicationContext.Context, pieceJointe),
                r =>
                {
                    if (r.Error != null)
                        error(r.Error);
                    else
                        callback(r.Result);
                });
        }

        public void AddLink(PieceJointeDto pieceJointe, Action<PieceJointeResult> callback, Action<Exception> error)
        {
            _services.ServicePieceJointe.Call<AddLinkCompletedEventArgs>(
                s => s.AddLinkAsync(_applicationContext.Context, pieceJointe),
                r =>
                {
                    if (r.Error != null)
                        error(r.Error);
                    else
                        callback(r.Result);
                });
        }

        public void UploadPieceJointe(PieceJointeDto pieceJointe, byte[] data, long part, Action<PieceJointeDto> callback, Action<Exception> error)
        {
            _services.ServicePieceJointe.Call<UploadPieceJointeCompletedEventArgs>(
                s => s.UploadPieceJointeAsync(_applicationContext.Context, pieceJointe, data, part),
                r =>
                {
                    if (r.Error != null)
                        error(r.Error);
                    else
                        callback(r.Result);
                });
        }

        public void GetPieceJointes(long vueId, long idFonctionnel, Action<IEnumerable<PieceJointeResult>> callback, Action<Exception> error)
        {
            _services.ServicePieceJointe.Call<GetPieceJointesCompletedEventArgs>(
                s => s.GetPieceJointesAsync(_applicationContext.Context, vueId, idFonctionnel),
                r =>
                {
                    if (r.Error != null)
                        error(r.Error);
                    else
                        callback(r.Result);
                });
        }

        public void GetPieceJointesByIdStarted(long pieceJointeId, Action<PieceJointeDto> callback, Action<Exception> error)
        {
            _services.ServicePieceJointe.Call<GetPieceJointeByIdStartedCompletedEventArgs>(
                s => s.GetPieceJointeByIdStartedAsync(_applicationContext.Context, pieceJointeId),
                r =>
                {
                    if (r.Error != null)
                        error(r.Error);
                    else
                        callback(r.Result);
                });
        }

        public void GetPieceJointesById(long pieceJointeId, long part, Action<byte[]> callback, Action<Exception> error)
        {
            _services.ServicePieceJointe.Call<GetPieceJointeByIdCompletedEventArgs>(
                s => s.GetPieceJointeByIdAsync(_applicationContext.Context, pieceJointeId, part),
                r =>
                {
                    if (r.Error != null)
                        error(r.Error);
                    else
                        callback(r.Result);
                });
        }

        public void DeletePieceJointe(long pieceJointeId, Action<bool> callback, Action<Exception> error)
        {
            _services.ServicePieceJointe.Call<DeletePieceJointeCompletedEventArgs>(
                s => s.DeletePieceJointeAsync(_applicationContext.Context, pieceJointeId),
                r =>
                {
                    if (r.Error != null)
                        error(r.Error);
                    else
                        callback(r.Result);
                });
        }
    }
}