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

#import "RouteView.h"

@implementation RouteView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //20140414 上午改
        [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]];
        
        //设置“入口选择”，“路线选择”的文字标签
        UILabel *entry = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width/2, 30)];
        entry.text = @"入口选择";
        entry.textColor = [UIColor colorWithRed:34.0/255 green:205.0/255 blue:119.0/255 alpha:1];
        entry.backgroundColor = [UIColor clearColor];
        [self addSubview:entry];
        
        UILabel *route = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width/2, 0, self.frame.size.width/2, 30)];
         route.textColor = [UIColor colorWithRed:34.0/255 green:205.0/255 blue:119.0/255 alpha:1];
        route.text = @"路线选择";
        [self addSubview:route];
        
        //设置入口的点坐标
        _entryPointArr = [[NSMutableArray alloc]initWithCapacity:10];
        NSArray *pointX = [[NSArray alloc]initWithObjects:
                           @"120.21135165600003",
                           @"120.21292190500003",
                           @"120.21560865100003",
                           @"120.21899675000009",
                           @"120.23082345100011",
                           @"120.22834998300004",
                           @"120.23176593300002", nil];
        NSArray *pointY = [[NSArray alloc]initWithObjects:
                           @"30.372420485000077",
                           @"30.374837409000065",
                           @"30.381783136000024",
                           @"30.383145310000032",
                           @"30.390086887000052",
                           @"30.390791830000069",
                           @"30.391480468000054", nil];
        int i;
        for (i=0; i<7; i++) {
            AGSPoint *point = [[AGSPoint alloc]initWithX:[[pointX objectAtIndex:i] doubleValue] y:[[pointY objectAtIndex:i] doubleValue] spatialReference:[AGSSpatialReference wgs84SpatialReference]];
            [_entryPointArr addObject:point];
        }
        //设置入口列表tableview
        _entryArr = [[NSArray alloc]initWithObjects:
                                    @"王蒙山隐入口",
                                    @"点将台入口",
                                    @"友梅轩入口",
                                    @"茶祖峒入口",
                                    @"捍海石宕入口",
                                    @"龙居诗碑入口",
                                    @"钱王城寨入口", nil];
        _entryTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, self.frame.size.width/2, self.frame.size.height-30)];
       
        _entryTable.tag=0;
        _entryTable.delegate=self;
        _entryTable.dataSource=self;
    
    
        [_entryTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        [self addSubview:_entryTable];
        
        //设置路线信息tableview
        _routeTable = [[UITableView alloc]initWithFrame:CGRectMake(self.frame.size.width/2, 30, self.frame.size.width/2, self.frame.size.height-30)];
        _routeTable.tag=1;
        _routeTable.delegate=self;
        _routeTable.dataSource=self;
        [self addSubview:_routeTable];
        
        
    }
    return self;
}

#pragma tableview darasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 0) {
        return _entryArr.count;
    }
    else
    {
        return _routeArr.count;

    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 0) {
        static NSString *entryStr = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:entryStr forIndexPath:indexPath];
        cell.textLabel.text = [_entryArr objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (IS_IPHONE6_UP)
            cell.textLabel.font = [UIFont systemFontOfSize:16];
        else 
            cell.textLabel.font = [UIFont systemFontOfSize:14];
        //20160506
        UIView* bgview = [[UIView alloc]initWithFrame:cell.frame];
        bgview.opaque = NO;
        bgview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_default.png"]];
        [cell setBackgroundView:bgview];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
        
        return cell;
    }
    else
    {
        NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"RouteCell" owner:self options:nil];
        RouteCell *cell = [arr lastObject];
        [cell setWithModel:[_routeArr objectAtIndex:indexPath.row]];
        UIView* bgview = [[UIView alloc]initWithFrame:cell.frame];
        bgview.opaque = YES;
        bgview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_default.png"]];
        [cell setBackgroundView:bgview];
            return cell;
    }
   
}


#pragma tableview delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 0) {
        if (IS_IPHONE6_UP)
            return 40;
        return 28;
    }
    else
    {
        return 65;
    }
    
}
//tableview点击事件，入口的tableview点击后，入口点标记，并且地图点移到屏幕中心，地图放大到一定倍率，并且右边的路线tableview要刷新视图
//路线的tableview点击事件，确定路线，得到graphic数组，
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 0) {
        //通过选的入口，得到路线信息，然后刷新路线表
        dispatch_queue_t down_queue=dispatch_queue_create("searchRoute", NULL);
        dispatch_async(down_queue, ^{
            FMDataBaseTool *too = [[FMDataBaseTool alloc]init];
            _routeArr=[too selectRoutesByEntry:[NSString stringWithFormat:@"%ld",(long)indexPath.row+1]];
            
            dispatch_async ( dispatch_get_main_queue (), ^{
                
                _routeTable.contentOffset = CGPointMake(0, 0);
                [_routeTable reloadData];
            });
        });
        
               //通知主控制器，在地图上标记出入口点(用点构造graphic传递)
        AGSPictureMarkerSymbol *pt=[AGSPictureMarkerSymbol pictureMarkerSymbolWithImageNamed:@"yuan.png"];
       AGSPoint *po = [_entryPointArr objectAtIndex:indexPath.row];
        AGSGeometryEngine *engine = [AGSGeometryEngine defaultGeometryEngine];
        MapInfo *info = [MapInfo shareMapInfo];
        AGSPoint *poin =(AGSPoint *)[engine projectGeometry:po toSpatialReference:info.mapSpatialReference];
        
        AGSGraphic * graphic = [[AGSGraphic alloc]initWithGeometry:poin symbol:pt attributes:nil];
        
        if ([self.delegate respondsToSelector:@selector(transferEntryGraphic:andPoint:)]) {
            [self.delegate transferEntryGraphic:graphic andPoint:poin];
        }
    }
    else
    {
        RouteCell * cell = [tableView cellForRowAtIndexPath:indexPath];
        //路线tableview的cell点击后，根据得到的路线id，查询数据库，得到graphic数组，
        
        dispatch_queue_t down_queue=dispatch_queue_create("searchpoint", NULL);
        dispatch_async(down_queue, ^{
            
           NSArray * graphicArr = [FMDataBaseTool getGraphicArr:cell.routeId];
            
            dispatch_async ( dispatch_get_main_queue (), ^{
                
                //判断代理是否实现函数，然后传值给GuideMapController，操作Map
                if ([self.delegate respondsToSelector:@selector(transferRouteId:)]) {
                    [self.delegate transferRouteId:graphicArr];
                }
            });
        });
        
    }
    
    
    //处理cell点击后，背景颜色的改变
    if (tableView.tag == 0) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIView* bgview = [[UIView alloc]initWithFrame:cell.frame];
        bgview.opaque = NO;
        bgview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"entry_pressed.png"]];
        [cell setBackgroundView:bgview];
    }
    else
    {
        RouteCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIView* bgview = [[UIView alloc]initWithFrame:cell.frame];
        bgview.opaque = NO;
        bgview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"route_pressed.png"]];
        [cell setBackgroundView:bgview];
        
    }
    
}


//tableview取消选择的时候，执行的函数
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 0) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIView* bgview = [[UIView alloc]initWithFrame:cell.frame];
        bgview.opaque = NO;
        bgview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_default.png"]];
        [cell setBackgroundView:bgview];
    }
    else
    {
        RouteCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIView* bgview = [[UIView alloc]initWithFrame:cell.frame];
        bgview.opaque = NO;
        bgview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_default.png"]];
        [cell setBackgroundView:bgview];
    }
    
    
}

@end
