//
// JavaAppAPI.m
// Project
//
// Created by haibara on 5/11/17.
//
//

#import "JavaAppAPI.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "Vars.h"

@interface JavaAppAPI ()
@end

@implementation JavaAppAPI

- (instancetype)init {
	if (self = [super init]) {
		self.apiUid = defaultAPIID;
		self.apiPassword = defaultAPIPassword;
		self.domainId = [Vars clientDomainId];
		self.accessToken = [Vars appDelegate].loginUser.cookie_string;
	}
	return self;
}

#pragma mark - Protected

- (NSString *)endpoint {
	return [Vars javaAppEndpoint];
}

- (NSString *)address {
	NSMutableArray *pathComponents = [NSMutableArray array];
	[pathComponents addObject:@"rest"];
	if (self.anonymous) {
		[pathComponents addObject:@"guest"];
	}
	[pathComponents addObject:self.namespace];
	[pathComponents addObject:self.name];
	NSString *urlPath =  [NSString pathWithComponents:pathComponents];
    return  [NSString stringWithFormat:@"%@/%@",self.endpoint,urlPath];
}

- (NSString *)name {
	NSString *className = NSStringFromClass(self.class);
	NSString *string = [className stringByReplacingOccurrencesOfString:@"[A-Z]" withString:@"-$0" options:NSRegularExpressionSearch range:NSMakeRange(1, className.length - 1)].lowercaseString;
	return string;
}

- (NSString *)namespace {
	NSString *superclassName = NSStringFromClass(self.superclass);
	NSString *string = nil;
	string = [superclassName substringToIndex:superclassName.length - @"API".length];
	string = [string stringByReplacingOccurrencesOfString:@"[A-Z]" withString:@"-$0" options:NSRegularExpressionSearch range:NSMakeRange(1, string.length - 1)];
	return [@"API-" im_stringByAppendingString:string].lowercaseString;
}

- (BOOL)anonymous {
	return NO;
}

@end

@implementation JavaAppAPIResponse
@end
