//
//  AppConfig.m
//  fastservice
//
//  Created by xuning on 5/5/25.
//  Copyright © 2025 FastService. All rights reserved.
//

#import "AppConfig.h"


@implementation AppConfig

DEF_SINGLETION(AppConfig);

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}
+ (void)setEnvironmentType:(int)type
{
    [[NSUserDefaults standardUserDefaults] setInteger:type forKey:kAppEnvironmentType];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
+ (int)getEnvironmentType
{
    int type = (int)[[NSUserDefaults standardUserDefaults] integerForKey:kAppEnvironmentType];
    return type;
}

/// 设置用户type
+ (void)setCurrentType:(UserCurrentType)type
{
    [[NSUserDefaults standardUserDefaults] setInteger:type forKey:kAppPersonalType];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/// 获取用户type
+ (UserCurrentType)getCurrentType
{
    UserCurrentType type = (UserCurrentType)[[NSUserDefaults standardUserDefaults] integerForKey:kAppPersonalType] ;
    return type;
}


+ (void)clearUserInfo{
    [AppConfig sharedAppConfig].user = nil;
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:kAppUserInfo];
    
    [[NSUserDefaults standardUserDefaults]synchronize];
}
+ (void)setUser:(UserInfo *)userInfo{
    [AppConfig sharedAppConfig].user = userInfo;
    [[NSUserDefaults standardUserDefaults] setObject:userInfo.mj_keyValues forKey:kAppUserInfo];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
+ (UserInfo *)getUserInfo{
    if ([AppConfig sharedAppConfig].user) {
        return [AppConfig sharedAppConfig].user;
    }else{
        NSDictionary * dic = [[NSUserDefaults standardUserDefaults] objectForKey:kAppUserInfo];
        UserInfo * userInfo;
        if (dic) {
            userInfo = [UserInfo mj_objectWithKeyValues:dic];
        }
        [AppConfig sharedAppConfig].user = userInfo;
        return userInfo;
    }
}
+ (NSString * )cookie{
    UserInfo * userInfo = [self getUserInfo];
    return userInfo.cookie;
}
//courier_status 0=未提交认证信息，1=已提交认证信息，2=认证成功，正常工作，3=收工状态 4-已封禁配送权限
+ (BOOL)needCer{
    UserInfo * userInfo = [self getUserInfo];
    if (userInfo.c.c_s == 0) {
        return YES;
    }
    if (userInfo.c.c_s >= 1) {
        return NO;
    }
    return YES;
}

+ (BOOL)needLogin{
    UserInfo * userInfo = [self getUserInfo];
    if (userInfo == nil) {
        return YES;
    }
    if (StringNotNullAndEmpty(userInfo.cookie)) {
        return NO;
    }
    return YES;
}


//本地缓存标识 int
+ (void)saveLocalCache:(LocalCache *)localCache{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [path stringByAppendingPathComponent:@"localCache.plist"];
    [@[localCache.mj_keyValues] writeToFile:filePath atomically:YES];
}

+ (LocalCache *)getLocalCache{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [path stringByAppendingPathComponent:@"localCache.plist"];
    NSArray *plist_arr = [NSArray arrayWithContentsOfFile:filePath];
    LocalCache *localCache;
    if ([plist_arr firstObject]) {
        localCache = [LocalCache mj_objectWithKeyValues:[plist_arr firstObject]];
    }
    return localCache;
}

//保存经纬度
+ (void)saveLocationCoordinate:(CLLocationCoordinate2D)coordinate{
    [[NSUserDefaults standardUserDefaults] setValue:@{@"longitude":@(coordinate.longitude),@"latitude":@(coordinate.latitude)} forKey:kAppLocationCoo];
    [[NSUserDefaults standardUserDefaults] synchronize];

}
+ (void)saveCustomLocationCoordinateWithLongitude:(NSString *)longitude Latitude:(NSString *)latitude {
    [[NSUserDefaults standardUserDefaults] setValue:@{@"longitude":longitude,@"latitude":latitude} forKey:kAppCustomLocationCoo];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
+ (void)deleteCustomLocationCoordinate {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kAppCustomLocationCoo];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
+ (CLLocationCoordinate2D)getLocationCoordinate{
    NSDictionary *dic;
    LocalCache *lc = [AppConfig getLocalCache];
    if (lc.is_custom == 1) {
        dic = [[NSUserDefaults standardUserDefaults] objectForKey:kAppCustomLocationCoo];
    } else {
        dic = [[NSUserDefaults standardUserDefaults] objectForKey:kAppLocationCoo];
    }
    if (dic) {
        CLLocationCoordinate2D cood = CLLocationCoordinate2DMake([[dic objectForKey:@"latitude"] doubleValue], [[dic objectForKey:@"longitude"] doubleValue]);
        return cood;
    }
    return CLLocationCoordinate2DMake(0, 0);
}


//城市
+ (void)saveLocationName:(NSString * )name{
    [[NSUserDefaults standardUserDefaults] setObject:name forKey:kAppLocationName];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)saveDistrictName:(NSString *)name{
    [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"districtName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)getLocationName{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kAppLocationName];
}

+ (NSString *)getDistrictName{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"districtName"];
}

+ (void)saveShowLocationName:(NSString * )name{
    [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"showLocationName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)saveShowDistrictName:(NSString *)name{
    [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"showDistrictName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)getShowLocationName{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"showLocationName"];
}

+ (NSString *)getShowDistrictName{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"showDistrictName"];
}
+ (void)saveGroupID:(int)group_id{
    [[NSUserDefaults standardUserDefaults] setObject:@(group_id) forKey:@"group_id"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (int)getGroupID{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:@"group_id"] intValue];
}

@end

