//
//  FMDataBaseTool.m
//  ArcGis
//
//  Created by Mac Book DQ02 on 16/3/19.
//  Copyright © 2016年 Xu Mengzhu. All rights reserved.
//

#import "FMDataBaseTool.h"

@implementation FMDataBaseTool


//根据入口id，查询路线信息
-(NSArray *)selectRoutesByEntry:(NSString *)entryId
{
    _routeArr =[[NSMutableArray alloc]initWithCapacity:50];
    NSString *fileName = [[NSBundle mainBundle]pathForResource:@"gts" ofType:@"db"];
 
    FMDatabase *db = [FMDatabase databaseWithPath:fileName];
    if (![db open]) {
  
        return nil;
    }
    NSString *sql = @"select * from allRoad where RuKouId=";
    sql = [sql stringByAppendingString:entryId];
    sql = [sql stringByAppendingString:@";"];
 
    FMResultSet *result = [db executeQuery:sql];
    while ([result next]) {
        NSString *routeIndex =[result stringForColumn:@"RuKouIdAllRoadIndex"];
   
        NSString *routeInfo = [result stringForColumn:@"RuKouIdAllRoad"];
    

        NSArray *arr = [routeInfo componentsSeparatedByString:@"a"];
    
        
        RouteModel *model = [[RouteModel alloc]init];
        model.number = routeIndex;
        model.name = [arr objectAtIndex:0];
        model.distance = [arr objectAtIndex:1];
        model.time = [arr objectAtIndex:2];
        model.calorie = [arr objectAtIndex:3];
        model.level = [arr objectAtIndex:4];
        
        [_routeArr addObject:model];
    }

    return _routeArr;
}

//根据路线id ，查询路线上的路段
-(NSArray *)selectRoadSectionByRouteId:(NSString *)routeId
{
    
    _sectionArr = [[NSMutableArray alloc]initWithCapacity:200];
    NSString *fileName = [[NSBundle mainBundle]pathForResource:@"gts" ofType:@"db"];
  
    FMDatabase *db = [FMDatabase databaseWithPath:fileName];
    if (![db open]) {
        return nil;
    }
    NSString *sql = @"select * from road where count=";
    sql = [sql stringByAppendingString:routeId];
    sql = [sql stringByAppendingString:@";"];

    FMResultSet *result = [db executeQuery:sql];
    while ([result next]) {
        NSString *sectionIndex =[result stringForColumn:@"indexs"];
    
        [_sectionArr addObject:sectionIndex];
    }
    return _sectionArr;

}

//根据路段id，查询该路段的所有点信息
-(NSArray *)selectPointsBySectionId:(NSString *)sectionId
{
    _pointArr = [[NSMutableArray alloc]initWithCapacity:200];
    NSString *fileName = [[NSBundle mainBundle]pathForResource:@"gts" ofType:@"db"];
      FMDatabase *db = [FMDatabase databaseWithPath:fileName];
    if (![db open]) {
        return nil;
    }
    NSString *sql = @"select * from Point where RoadCount=";
    sql = [sql stringByAppendingString:sectionId];
    sql = [sql stringByAppendingString:@";"];
  
    FMResultSet *result = [db executeQuery:sql];
    while ([result next]) {
        double x = [result doubleForColumn:@"point_x"];
        double y = [result doubleForColumn:@"point_y"];
//        CGPoint point = CGPointMake(x, y);
//        NSString *str = NSStringFromCGPoint(point);
        AGSPoint *point = [[AGSPoint alloc]initWithX:x y:y spatialReference:[AGSSpatialReference wgs84SpatialReference]];
        [_pointArr addObject:point];
    }

    return _pointArr;
}


#pragma according to the pointArr,create a AGSGraphic object
-(AGSGraphic *)graphicWithPointArr:(NSArray *)pointArr
{
    MapInfo *info = [MapInfo shareMapInfo];
    AGSMutablePolyline *mutablePoly = [[AGSMutablePolyline alloc]initWithSpatialReference:info.mapSpatialReference];
    [mutablePoly addPathToPolyline];
    int i;
    for (i=0; i<pointArr.count; i++) {
//        CGPoint point = CGPointFromString([pointArr objectAtIndex:i]);
//
//        AGSPoint *agsPoint = [[AGSPoint alloc]initWithX:point.x y:point.y spatialReference:[AGSSpatialReference wgs84SpatialReference]];
    AGSPoint *agsPoint = [pointArr objectAtIndex:i];
      AGSPoint *poin = [FMDataBaseTool transferToMapViewSpatialreference:agsPoint];
        [mutablePoly addPointToPath:poin];
    }
    
    AGSSimpleLineSymbol* lineSymbol = [[AGSSimpleLineSymbol alloc] init];
    lineSymbol.color= [UIColor redColor];
    lineSymbol.width = 5;
    AGSGraphic *myGraphic = [[AGSGraphic alloc]initWithGeometry:mutablePoly symbol:lineSymbol attributes:nil];
    return myGraphic;
}


//根据路线id，获取所有的graphic数组，点的坐标系转换已经在获取点的函数中操作
+(NSArray *)getGraphicArr:(NSString *)routeId
{
    NSLog(@"routeId = %@",routeId);
    NSMutableArray *graphicArr= [[NSMutableArray alloc]initWithCapacity:10];
    FMDataBaseTool *too = [[FMDataBaseTool alloc]init];
    //根据路线id，获取路线上所有路段的信息
    NSArray *sectionArr=[too selectRoadSectionByRouteId:routeId];

    for (NSString *section in sectionArr) {
        //根据路段id，获取路段上所有点的信息
        NSArray *pointArrInSection=[too selectPointsBySectionId:section];
        //FMDataBaseTool自定义的函数，利用point数组，创建一个graphic对象
        AGSGraphic *myGraphic = [too graphicWithPointArr:pointArrInSection];
        [graphicArr addObject:myGraphic];
    }
    return graphicArr;
}

//将点转化成当前地图坐标系的点坐标
+(AGSPoint *)transferToMapViewSpatialreference:(AGSPoint *)pointFromSqlite
{
    MapInfo *info = [MapInfo shareMapInfo];
    AGSSpatialReference *reference = info.mapSpatialReference;
    AGSGeometryEngine *engine = [AGSGeometryEngine defaultGeometryEngine];
    AGSPoint *point = (AGSPoint *)[engine projectGeometry:pointFromSqlite toSpatialReference:reference];

    return point;
}


//判断点是否在多边形内,传入的点参数为转化过后的地图上的点
+(BOOL)whetherInMapPolygon:(AGSPoint *)point
{
    MapInfo *info = [MapInfo shareMapInfo];

    AGSMutablePolygon *poly = [[AGSMutablePolygon alloc]initWithSpatialReference:info.mapSpatialReference];
    [poly addRingToPolygon];
    AGSPoint *poin1 = [FMDataBaseTool transferToMapViewSpatialreference:[[AGSPoint alloc]initWithX:120.21219412757301 y:30.36401143321038 spatialReference:[AGSSpatialReference wgs84SpatialReference]]];
    [poly addPointToRing:poin1];
    
    AGSPoint *poin2 = [FMDataBaseTool transferToMapViewSpatialreference:[[AGSPoint alloc]initWithX:120.20128426169312 y:30.38188222644989 spatialReference:[AGSSpatialReference wgs84SpatialReference]]];
    [poly addPointToRing:poin2];
    
    AGSPoint *poin3 = [FMDataBaseTool transferToMapViewSpatialreference:[[AGSPoint alloc]initWithX:120.22533270219984 y:30.396097665260665 spatialReference:[AGSSpatialReference wgs84SpatialReference]]];
    [poly addPointToRing:poin3];
    
    AGSPoint *poin4 = [FMDataBaseTool transferToMapViewSpatialreference:[[AGSPoint alloc]initWithX:120.2390976409663 y:30.387840531675938 spatialReference:[AGSSpatialReference wgs84SpatialReference]]];
    [poly addPointToRing:poin4];
    
    if ([poly containsPoint:point]) {
        return YES;
    }
    return NO;
}


+(AGSGraphic *)getMarkSymGraphic:(AGSPoint *)poin type:(int)typ
{
    AGSPictureMarkerSymbol *symbol = [[AGSPictureMarkerSymbol alloc]initWithImageNamed:[NSString stringWithFormat:@"tp_%d",typ]];
    AGSPoint *point = [FMDataBaseTool transferToMapViewSpatialreference:poin];
    AGSGraphic *gra = [[AGSGraphic alloc]initWithGeometry:point symbol:symbol attributes:nil];
    
    return gra;
}
@end
