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

#import "BottomView.h"

BOOL click[2]={NO,NO};
@implementation BottomView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    float width = self.frame.size.width/2;
    float height = self.frame.size.height;
    self.btnArr = [[NSMutableArray alloc]initWithCapacity:10];
    
    NSString *imgName = @"bt";
    NSString *imgSelect = @"bt_select";
    
    NSArray *imgArr = @[@"route",@"mark"];
  // NSArray *selectImgArr = @[@"bt_selected_2.jpg",@"bt_selected_3.jpg"];
    
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        int i;
        for (i=0; i<2; i++) {
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(width*i, 0, width+2, height)];
            btn.tag=100+i;
            
           [btn setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:imgSelect] forState:UIControlStateSelected];
            
            UIImageView *v = [[UIImageView alloc]initWithFrame:CGRectMake((btn.frame.size.width - 37.5)/2, 4.5, 37.5, 40)];
            v.image = [[UIImage imageNamed:[imgArr objectAtIndex:i]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            v.tag = btn.tag +20;
            [btn addSubview:v];
            
            [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [self.btnArr addObject:btn];
            [self addSubview:btn];
        }
    }

    return self;

}

-(void)btnClicked:(UIButton *)sender
{
    sender.selected = !sender.selected;

    NSArray *imgArr = @[@"route",@"mark"];
    NSArray *imgArrS = @[@"routeS",@"markS"];
    unsigned long tag=sender.tag;
   
    if ([self.delegate respondsToSelector:@selector(displayWithBtn:andClick:)]) {
        //判断上一个按钮，如果是简介页的话，地图按钮第一次点击，不弹出路线，标签视图
            [self.delegate displayWithBtn:tag andClick:click[tag-100]];
            //当前操作的按钮，点击后，状态取反,并且按钮的选中状态和当前存储的值保持一致
            click[tag-100] = !click[tag-100];
            sender.selected = click[tag-100];
        if (sender.selected) {
            UIImageView *v = (UIImageView *)[self viewWithTag:tag+20];
            v.image = [UIImage imageNamed:[imgArrS objectAtIndex:tag-100]];
        }
        else
        {
            UIImageView *v = (UIImageView *)[self viewWithTag:tag+20];
            v.image = [UIImage imageNamed:[imgArr objectAtIndex:tag-100]];
        }
        //设置当前操作按钮之外的左右按钮恢复未点击状态,选中状态都是未选中
        //需要通过按钮的tag值先获取到按钮，再设置按钮的selected为NO
        int i;
        for (i=0; i<2; i++) {
            if (i == tag-100){
                continue;
            }
            click[i]=NO;
            UIButton *btn = (UIButton *)[self viewWithTag:i+100];
             btn.selected = NO;
            UIImageView *v = (UIImageView *)[self viewWithTag:i+120];
            v.image = [UIImage imageNamed:[imgArr objectAtIndex:i]];
        }
        
    }
    
    self.lastSelectedTag = tag;
}


//点击地图的空白处，此处接收到通知，底部视图收起，并且底部按钮的状态都恢复成未点击状态
-(void)changeState
{
    NSArray *imgArr = @[@"route",@"mark"];
    int i;
    for (i=0; i<2; i++) {
        click[i]=NO;
        UIButton *btn = (UIButton *)[self viewWithTag:i+100];
        btn.selected = NO;
        UIImageView *v = (UIImageView *)[self viewWithTag:i+120];
        v.image = [UIImage imageNamed:[imgArr objectAtIndex:i]];
    }
    
}

@end
