//
//  RouteTaskInnerView.m
//  TestAlphaView
//
//  Created by Mac Book DQ02 on 16/4/9.
//  Copyright © 2016年 Xu Mengzhu. All rights reserved.
//

#import "RouteTaskInnerView.h"

@implementation RouteTaskInnerView
@synthesize endingField;
@synthesize startingField;


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 200, 30)];
        titleLabel.text =@"路径规划";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:titleLabel];
        
        UILabel *startLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 40, 40, 30)];
        startLabel.text =@"起点";
        [self addSubview:startLabel];
        
        UIImageView *qv = [[UIImageView alloc]initWithFrame:CGRectMake(45, 40, 30, 30)];
        qv.image = [UIImage imageNamed:@"qidian"];
        [self addSubview:qv];
        
        self.startingField = [[UITextField alloc]initWithFrame:CGRectMake(80, 40, 120, 30)];
        self.startingField.text = @"我的位置";
        self.startingField.textColor = [UIColor blueColor];
        self.startingField.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.startingField];
        
        UILabel *endLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 75, 40, 30)];
        endLabel.text =@"终点";
        [self addSubview:endLabel];
        
        UIImageView *zv = [[UIImageView alloc]initWithFrame:CGRectMake(45, 75, 30, 30)];
        zv.image = [UIImage imageNamed:@"zhongdian"];
        [self addSubview:zv];

        
        self.endingField = [[UITextField alloc]initWithFrame:CGRectMake(80, 75, 120, 30)];
        self.endingField.textColor = [UIColor blueColor];
        self.endingField.font = [UIFont systemFontOfSize:14];
        self.endingField.delegate = self;
        self.endingField.placeholder = @"请输入终点";
        [self addSubview:self.endingField];
        
        UIButton *taskBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 110, 200, 40)];
        [taskBtn setTitle:@"规划" forState:UIControlStateNormal];
        
        taskBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [taskBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:taskBtn];
        [taskBtn addTarget:self action:@selector(taskBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        self.backgroundColor = [UIColor whiteColor];
  
    }
    return self;
}


-(void)taskBtnClicked
{
//    MapInfo *info = [MapInfo shareMapInfo];
//    AGSPoint *start = [[AGSPoint alloc]initWithX:120.219047 y:30.383156 spatialReference:info.mapSpatialReference];
//    AGSPoint *end = [[AGSPoint alloc]initWithX:120.213425 y:30.384623 spatialReference:info.mapSpatialReference];
//    //起点，终点值传给电子导游控制器
//    if ([self.delegate respondsToSelector:@selector(transferStart:end:type:)]) {
//        [self.delegate transferStart:start end:end type:2];
//    }
    

    //如果输入框里面没有内容，不允许规划
    if ([self.startingField.text isEqualToString:@""] || [self.endingField.text isEqualToString:@""]) {
        //起点，终点值传给电子导游控制器
        if ([self.delegate respondsToSelector:@selector(transferStart:end:type:)]) {
            [self.delegate transferStart:nil end:nil type:0];
        }
        return;
    }
 
    __block AGSPoint *start;
    __block AGSPoint *end;
    NSLog(@"1");
    //两种情况，起点是我的位置 或者 是自己手输的
    //先请求终点，再请求起点
    NSDictionary *endPara = [[NSDictionary alloc]initWithObjectsAndKeys:self.endingField.text,@"fullName", nil];
    [Manager getInformationForType:endPara andBlock:^(NSArray *modelArr) {
        if (modelArr.count == 0) {
            end = nil;
        }else{
            MarkInfoModel *model = (MarkInfoModel *)[modelArr objectAtIndex:0];
            end = [[AGSPoint alloc]initWithX:model.objPoint.x y:model.objPoint.y spatialReference:[AGSSpatialReference wgs84SpatialReference]];
            end = [FMDataBaseTool transferToMapViewSpatialreference:end];
        }
        
        NSLog(@"2");
        if ([self.startingField.text isEqualToString:@"我的位置"]) {
            Singleton *single = [Singleton defaultSingleton];
            AGSPoint *cuPoint = [[AGSPoint alloc]initWithX:single.currentLocation.coordinate.longitude y:single.currentLocation.coordinate.latitude spatialReference:[AGSSpatialReference wgs84SpatialReference]];
            AGSPoint *po = [FMDataBaseTool transferToMapViewSpatialreference:cuPoint];
            start = po;
            //起点，终点值传给电子导游控制器
            if ([self.delegate respondsToSelector:@selector(transferStart:end:type:)]) {
                [self.delegate transferStart:start end:end type:1];
            }
        }else{
            NSLog(@"3");
            NSDictionary *parameter = [[NSDictionary alloc]initWithObjectsAndKeys:self.startingField.text,@"fullName", nil];
            [Manager getInformationForType:parameter andBlock:^(NSArray *modelArr) {
                NSLog(@"modelArr.count = %ld",(unsigned long)modelArr.count);

                if (modelArr.count == 0) {
                    start = nil;
                }else{
                    MarkInfoModel *model = (MarkInfoModel *)[modelArr objectAtIndex:0];
                    start = [[AGSPoint alloc]initWithX:model.objPoint.x y:model.objPoint.y spatialReference:[AGSSpatialReference wgs84SpatialReference]];
                    start = [FMDataBaseTool transferToMapViewSpatialreference:start];
                }
                
                //起点，终点值传给电子导游控制器
                if ([self.delegate respondsToSelector:@selector(transferStart:end:type:)]) {
                    [self.delegate transferStart:start end:end type:2];
                }
                
            }];
        }
        
    }];
    

}




@end
