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

#import "MyNetWork.h"

@implementation MyNetWork

#pragma mark - -环信登录
+ (void)checkEaseInfo{
//    if (![AppConfig needLogin] && ([AppConfig getUserInfo].hide_flag != 1)) {
//        if (![[EMClient sharedClient] isLoggedIn]) {
//            [MyNetWork easeLoginWithUsername:S(@"%d",[AppConfig getUserInfo].user_id) spw:[AppConfig getUserInfo].spw];
//        }else{
//            NSString *em_name = [[EMClient sharedClient] currentUsername];
//            if (StringIsNullOrEmpty(em_name)) {
//                return;
//            }
//            BOOL isMe = [em_name isEqualToString:S(@"%d",[AppConfig getUserInfo].user_id)];
//            if (!isMe) {//非一人
//                [MyNetWork easeLoginWithUsername:S(@"%d",[AppConfig getUserInfo].user_id) spw:[AppConfig getUserInfo].spw];
//            }
//        }
//    }
}

+ (void)easeLoginWithUsername:(NSString *)name spw:(NSString *)spw{
    
//    [[EMClient sharedClient] loginWithUsername:name password:spw completion:^(NSString *aUsername, EMError *aError) {
//        if (!aError) {
//            //设置是否自动登录
//            [[EMClient sharedClient].options setIsAutoLogin:YES];
//            
//            //发送自动登陆状态通知
//            [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@YES];
//            //保存最近一次登录用户名
//            //      [self saveLastLoginUsername];
//            DebugLog(@"环信登录成功：%@",aError.errorDescription);
//        }else{
//            DebugLog(@"环信登录失败：%@",aError.errorDescription);
//            
//            [MBProgressHUD showMessage:S(@"%@",aError.errorDescription) toView:nil];
//
//        }
//    }];
}

#pragma mark - -登录后加载
+ (void)loginAfterLoadUserProfile:(EventHandler )myNetBlock{
    NSMutableDictionary *pDic = [[NSMutableDictionary alloc] init];
    [pDic setObject:@([AppConfig getUserInfo].user_id) forKey:@"user_id"];
    [[AFNetworkingClient sharedClient] runPost:SS_GET_USERPROFILE parameters:pDic success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([MyNetWork isSuccessServerData:responseObject]) {
            UserInfo * ui = [UserInfo mj_objectWithKeyValues:responseObject[@"data"]];
            UserInfo *userInfo = [AppConfig getUserInfo];
            userInfo.b = ui.b;
            userInfo.c = ui.c;
            userInfo.m = ui.m;
            [AppConfig setUser:userInfo];
            BlockCallWithOneArg(myNetBlock, userInfo);
            
            
            if (userInfo.c.c_s > 0) {
                [AppConfig setCurrentType:UserCurrentTypeCourier];
            }else{
                [AppConfig setCurrentType:UserCurrentTypeCustom];
            }
            
            [[NSNotificationCenter defaultCenter] postNotificationName:kAppLoadProfileSuccess object:nil];
        }else{
            BlockCallWithOneArg(myNetBlock, nil);
            [MBProgressHUD showMessage:[responseObject objectForKey:@"resperr"] toView:nil];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        BlockCallWithOneArg(myNetBlock, nil);
    }];
}


+ (void)loadGroupInfoWithGroupId:(int)group_id myNetBlock:(EventHandler )myNetBlock{
    //
    NSMutableDictionary *pDic = [NSMutableDictionary dictionary];
    //所在城市
    [pDic setAnyObject:[AppConfig getLocationName] forKey:@"city"];
    [pDic setAnyObject:[AppConfig getDistrictName] forKey:@"district"];
    
    [pDic setObject:@(group_id) forKey:@"group_id"];
    
    [[AFNetworkingClient sharedClient] runPost:SS_GET_GINFO parameters:pDic success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([MyNetWork isSuccessServerData:responseObject]) {
            NSDictionary *dic = responseObject[@"data"];
            
            [AppTools sharedAppTools].cityDic = [[NSMutableDictionary alloc] initWithDictionary:dic];
            
            GroupItem *groupItem = [GroupItem mj_objectWithKeyValues:dic];
            
            [AppConfig saveGroupID:groupItem.group_id];
            [AppConfig saveShowLocationName:groupItem.city];
            [AppConfig saveShowDistrictName:groupItem.district];
            [AppTools sharedAppTools].item = groupItem;
            
            BlockCallWithOneArg(myNetBlock, dic);
        }else{
            [MBProgressHUD showMessage:[responseObject objectForKey:@"resperr"] toView:nil];

        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}
+ (NSMutableDictionary *)parameterShowCityAndDistrict:(NSMutableDictionary *)pDic{
    [pDic setAnyObject:[AppConfig getShowLocationName] forKey:@"city"];
    [pDic setAnyObject:[AppConfig getShowDistrictName] forKey:@"district"];
    return pDic;
}


#pragma mark -服务器数据是否加载成功
+ (BOOL)isSuccessServerData:(id)responseObject{
    return StringNotNullAndEmpty([responseObject objectForKey:@"respcd"]) && [[responseObject objectForKey:@"respcd"] isEqualToString:@"0000"];
}

@end
