//
//  AppLocalConfig.m
//  Project
//
//  Created by Vae on 2019/1/11.
//

#import "AppLocalConfig.h"
#import "Vars.h"

static NSString * const kUserConfig = @"userconfig";
static NSString * const kNotificationEnable = @"notificationEnable";
static NSString * const kNotificationRegister = @"notificationRegister";
static NSString * const kDeviceToken = @"deviceToken";
static NSString * const kNotificationUnreadMessage = @"unreadMessage";
@implementation AppLocalConfig

+ (instancetype)sharedInstance {
	static dispatch_once_t onceToken;
	static AppLocalConfig *instance = nil;
	dispatch_once(&onceToken,^{
		instance = [[super allocWithZone:NULL] init];
	});
	return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
	return [self sharedInstance];
}

-(void)setUserDeviceToken:(NSString *)userDeviceToken{
	NSMutableDictionary *userConfig = [[self userConfig] mutableCopy];
	if (!userConfig) {
		userConfig = [NSMutableDictionary dictionary];
	}
	[userConfig setValue:userDeviceToken forKey:kDeviceToken];
	[self updateUserConfig:userConfig];
}

-(NSString *)userDeviceToken{
	NSDictionary *userConfig = [self userConfig];
	return [userConfig valueForKey:kDeviceToken];
}

-(void)setUserNotificationsRegister:(BOOL)userNotificationsRegister{
	NSMutableDictionary *userConfig = [[self userConfig] mutableCopy];
	if (!userConfig) {
		userConfig = [NSMutableDictionary dictionary];
	}
	[userConfig setValue:[NSNumber numberWithBool:userNotificationsRegister] forKey:kNotificationRegister];
	
	[self updateUserConfig:userConfig];
}

- (BOOL)userNotificationsRegister{
	NSDictionary *userConfig = [self userConfig];
	return [[userConfig valueForKey:kNotificationRegister] boolValue];
}


-(void)setUserNotificationsEnable:(BOOL)userNotificationsEnable{
	NSMutableDictionary *userConfig = [[self userConfig] mutableCopy];
	if (!userConfig) {
		userConfig = [NSMutableDictionary dictionary];
	}
	[userConfig setValue:[NSNumber numberWithBool:userNotificationsEnable] forKey:kNotificationEnable];
	[self updateUserConfig:userConfig];
}

-(BOOL)userNotificationsEnable{
	NSDictionary *userConfig = [self userConfig];
	return [[userConfig valueForKey:kNotificationEnable] boolValue];
}

-(void)setHasUnreadMessage:(BOOL)hasUnreadMessage{
	NSMutableDictionary *userConfig = [[self userConfig] mutableCopy];
	if (!userConfig) {
		userConfig = [NSMutableDictionary dictionary];
	}
	[userConfig setValue:[NSNumber numberWithBool:hasUnreadMessage] forKey:kNotificationUnreadMessage];
	[self updateUserConfig:userConfig];
}

-(BOOL)hasUnreadMessage{
	NSDictionary *userConfig = [self userConfig];
	return [[userConfig valueForKey:kNotificationUnreadMessage] boolValue];
}


-(NSDictionary *)userConfig{
	NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
	NSDictionary *userConfig = [userdefaults objectForKey:kUserConfig];
	NSDictionary *localUserConfig = [userConfig valueForKey:[NSString stringWithFormat:@"%d",[Vars appDelegate].loginUser.id]];
	return localUserConfig;
}

-(void)updateUserConfig:(NSMutableDictionary *)dic{
	NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
	NSMutableDictionary *userconfig = [[userdefaults objectForKey:kUserConfig] mutableCopy];
	if (!userconfig) {
		userconfig = [NSMutableDictionary dictionary];
	}
	[userconfig setValue:dic forKey:[NSString stringWithFormat:@"%d",[Vars appDelegate].loginUser.id]];
	[userdefaults setObject:userconfig forKey:kUserConfig];
}

@end
