//
//  XMLAnalysisTool.m
//  gaotingshan
//
//  Created by Mac Book DQ02 on 16/6/2.
//  Copyright © 2016年 Xu Mengzhu. All rights reserved.
//

#import "XMLAnalysisTool.h"

#define STOPSDETAILURLSTRING @"http://218.108.4.35:8080/CarInfoService.asmx/GetCarInfoList"

@implementation XMLAnalysisTool

//获取停车场信息
-(NSArray *)analysisXML:(PassArrBlock)block
{
    _stopModelArr = [[NSMutableArray alloc]initWithCapacity:10];
    NSArray *pointArr = [[NSArray alloc]initWithObjects:
                                [AGSPoint pointWithX:120.228260374 y:30.3814676320001 spatialReference:[AGSSpatialReference wgs84SpatialReference]],
                                [AGSPoint pointWithX:120.228615931 y:30.3828102570001 spatialReference:[AGSSpatialReference wgs84SpatialReference]],
                                [AGSPoint pointWithX:120.229834063 y:30.3876983580001 spatialReference:[AGSSpatialReference wgs84SpatialReference]],
                                [AGSPoint pointWithX:120.213989501 y:30.3678059690001 spatialReference:[AGSSpatialReference wgs84SpatialReference]],
                                [AGSPoint pointWithX:120.233061191 y:30.386647108 spatialReference:[AGSSpatialReference wgs84SpatialReference]],
    
                                nil];
    AFHTTPSessionManager *manager= [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [manager POST:STOPSDETAILURLSTRING parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //从服务器获取的数据，打印出来发现是两层xml，所以要一层一层剥，
        NSLog(@"responseObject= %@",responseObject);
        //先解出最外面的xml，拿到 rootElement，这其实还是一个xml
        GDataXMLDocument *doc1 = [[GDataXMLDocument alloc]initWithData:responseObject error:nil];
        GDataXMLElement *rootElement = [doc1 rootElement];
        //把拿到的里层的element，先弄成字符串，再转化成一个document，
        NSString *dataStr = [rootElement stringValue];
        NSLog(@"datastr= %@",dataStr);
        NSData *data2 = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
        GDataXMLDocument *doc2 = [[GDataXMLDocument alloc]initWithData:data2 error:nil];
        //再取一次根节点
        rootElement = [doc2 rootElement];
        
        NSArray *CarInfo = [rootElement elementsForName:@"CarInfo"];
       
        __block int i=0;
        for (GDataXMLElement *carInfo in CarInfo) {
            MarkInfoModel *model = [[MarkInfoModel alloc]init];
            //获取节点属性
            GDataXMLElement *codeElement = [[carInfo elementsForName:@"Code"] objectAtIndex:0];
            NSString *code = [codeElement stringValue];
            model.objCode = code;
            
            GDataXMLElement *nameElement = [[carInfo elementsForName:@"Name"] objectAtIndex:0];
            NSString *name = [nameElement stringValue];
            model.objName = name;
            
            //GDataXMLElement *totalElement = [[carInfo elementsForName:@"Total"] objectAtIndex:0];
           // NSString *total = [totalElement stringValue];
            
            
         //   GDataXMLElement *comeElement = [[carInfo elementsForName:@"ComeCount"] objectAtIndex:0];
         //   NSString *come = [comeElement stringValue];
            
            //UnComeCount
            GDataXMLElement *unComeElement = [[carInfo elementsForName:@"UnComeCount"] objectAtIndex:0];
            NSString *unCome = [unComeElement stringValue];
            model.unComeCount = unCome;
        
            AGSPoint *poin = [pointArr objectAtIndex:i];
            model.objPoint = CGPointMake(poin.x, poin.y);
            
            [_stopModelArr addObject:model];//第二次添加的值，会覆盖之前的所有，是为什么
            i++;
        }
        block(_stopModelArr);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    return _stopModelArr;
}
@end
