//
//  CustomURL.h
//  URLCustom
//
//  Created by Allan bouvard on 28/02/2014.
//  Copyright (c) 2014 Apps-Panel. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef enum {
	APWSErrorTypeNoConnection,
	APWSErrorTypeUnknown,
} APWSErrorType;

@interface APWSError : NSObject
@property (nonatomic, strong) NSString *message;
@property (nonatomic, readwrite) APWSErrorType errorType;
@property (nonatomic, readwrite) int code;
@end

@protocol APWSManagerDelegate;

@interface APWSManager : NSObject

#if __has_feature(objc_arc_weak)
@property (weak) id<APWSManagerDelegate> delegate;
#elif __has_feature(objc_arc)
@property (unsafe_unretained) id<APWSManagerDelegate> delegate;
#else
@property (assign) id<APWSManagerDelegate> delegate;
#endif

@property (nonatomic, strong) NSMutableURLRequest *theRequest;

@property (nonatomic, readwrite) int tag;

// Object qui stocke le résultat retourné des WS après traitement
@property (nonatomic, strong) id result;
@property (nonatomic, strong) NSString *dateLastCache;
@property (nonatomic, strong) APWSError *apwsError;

// If TRUE le cache n'est pas utilisé
@property (nonatomic, readwrite) BOOL desactiveCache;

- (id)initWithRequest:(NSMutableURLRequest *)request delegate:(id)del andTag:(int)aTag;
- (id)initWithAction:(NSString *)action delegate:(id)del WithGet:(NSDictionary *)dictionnary andTag:(int)aTag;
- (id)initWithAction:(NSString *)action delegate:(id)del WithGet:(NSDictionary *)dictionnary andPost:(NSDictionary *)postDictionnary andTag:(int)aTag;

// Méthode pour commencer le téléchargement des données
- (void)start;

// Méthode pour Cancel la NSURLConnection et réinitialiser les datas
- (void)cancel;

// Constructeur d'URL
+ (NSMutableURLRequest *)getRequestForAction:(NSString *)action WithGet:(NSDictionary *)dictionnary andPost:(NSDictionary *)postDictionnary;
+ (NSMutableURLRequest *)getRequestForAction:(NSString *)action WithGet:(NSDictionary *)dictionnary;

@end

/*
 * Delegate avec méthodes optionnelles
 */
@protocol APWSManagerDelegate <NSObject>
@required
//Called after the request Failed
- (void)apwsFailedDownload:(APWSManager *)apws;
//Called after the request Failed
- (void)apwsFinishedDownload:(APWSManager *)apws;
@end

