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

#import "MySearchView.h"

@implementation MySearchView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        WS(ws);
    
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = 1;
        self.layer.borderColor = [[UIColor colorWithRed:215.0/255 green:215.0/255 blue:215.0/255 alpha:1] CGColor];
        
       CALayer *layer = [self layer];
        layer.shadowOffset = CGSizeMake(0, 5);
        layer.shadowRadius = 5.0;
        layer.shadowColor = [UIColor colorWithRed:246/255 green:214.0/255 blue:215.0/255 alpha:1].CGColor;
        layer.shadowOpacity = 0.95;
        
        _searchbtn = [[UIButton alloc]init];
        [_searchbtn setBackgroundImage:[UIImage imageNamed:@"chazhao.png"] forState:UIControlStateNormal];
        [_searchbtn addTarget:self action:@selector(editEnd:) forControlEvents:UIControlEventTouchUpInside];
        _searchbtn.tag =0;
        
        UILabel *label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor colorWithRed:215.0/255 green:215.0/255 blue:215.0/255 alpha:1];
        [self addSubview:label];
        
        _searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 5, 245, 28)];
        _searchTextField.delegate = self;
        _searchTextField.backgroundColor = [UIColor whiteColor];
       
        [_searchTextField addTarget:self action:@selector(editEnd:) forControlEvents:UIControlEventEditingDidEnd];
        _searchTextField.tag =1;
        
        _cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(290, 7, 18, 18)];
        [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"quxiao.png"] forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_searchbtn];
        [self addSubview:_searchTextField];
        [self addSubview:_cancelBtn];
        [self.cancelBtn setHidden:YES];
        
        //添加约束
        [_searchbtn mas_makeConstraints:^(MASConstraintMaker *make) {
            if (IS_IPHONE6_UP) {
                make.left.equalTo(ws).offset(12);
                make.top.equalTo(ws).offset(7);
            }
            else
            {
                make.left.equalTo(ws).offset(8);
                make.top.equalTo(ws).offset(5);
            }
            make.width.mas_equalTo(17);
            make.height.mas_equalTo(17);
        }];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_searchbtn.mas_right).offset(4);
            make.top.equalTo(ws.mas_top).offset(5);
            make.bottom.equalTo(ws.mas_bottom).offset(-5);
            make.width.mas_equalTo(1);
        }];
        
        
        [_searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(label.mas_right).offset(2);
            make.top.equalTo(ws).offset(1);
            make.bottom.equalTo(ws).offset(3);
            make.width.mas_equalTo(200);
        }];
        
        [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            if (IS_IPHONE6_UP) {
                make.right.equalTo(ws).offset(-8);
                make.top.equalTo(ws).offset(7);
            }
            else
            {
                make.right.equalTo(ws).offset(-8);
                make.top.equalTo(ws).offset(7);
            }
            make.width.mas_equalTo(15);
            make.height.mas_equalTo(15);
        }];
    }
    return self;
}

#pragma mark 编辑完成事件
-(void)editEnd:(id)sender
{
    if ([_searchTextField.text isEqualToString:@""]) {
        [self.cancelBtn setHidden:YES];
       [self.delegate passAllModel:nil andGraphicArr:nil];
        return;
    }
    else
    {
        [self.cancelBtn setHidden:NO];
    }
  
    NSDictionary *parameter = [[NSDictionary alloc]initWithObjectsAndKeys:self.searchTextField.text,@"name", nil];
    [Manager getInformationForType:parameter andBlock:^(NSArray *modelArr) {
        NSArray *graphicArr = [Manager spotsGraphicArrWithModelArr:modelArr];
          if ([self.delegate respondsToSelector:@selector(passAllModel:andGraphicArr:)]) {
              [self.delegate passAllModel:modelArr andGraphicArr:graphicArr];
          }
    }];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.searchTextField resignFirstResponder];    //主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收
    return YES;
}

//取消按钮的点击事件，键盘收起，主控制器的tableview收起
-(void)cancelBtnClicked:(UIButton *)sender
{
    self.searchTextField.text=@"";
    [self.searchTextField resignFirstResponder];
    [sender setHidden:YES];
    if ([self.delegate respondsToSelector:@selector(hideTableView)]) {
        [self.delegate hideTableView];
    }
}



#pragma UITextFieldDelegate event
//开始编辑的时候，清除按钮显示
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    //开始编辑的时候，文本框有内容就显示删除按钮，没有就不显示
    if ([textField.text isEqualToString:@""])
        [self.cancelBtn setHidden:YES];
    else
        [self.cancelBtn setHidden:NO];
    
    //注册通知，视图收起后，通知bottomView的button，恢复成未click状态
    NSNotification *noti = [[NSNotification alloc]initWithName:@"changeState" object:nil userInfo:nil];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotification:noti];
    if ([self.delegate respondsToSelector:@selector(hideSubViews)]) {
        [self.delegate hideSubViews];
    }
}

//编辑变化的时候，将清除按钮显示
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    if ([self.searchTextField.text isEqualToString:@""]) {
        [self.cancelBtn setHidden:YES];
    }
    else
    {
         [self.cancelBtn setHidden:NO];
    }
    return YES;
}


@end
