//
//  KWMSearchBrandView.m
//  iCemarose
//
//  Created by 陈荣科 on 16/8/30.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMSearchBrandView.h"

@implementation KWMSearchBrandView{
    BOOL isSearching;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initContentView];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self initContentView];
    }
    return self;
}

- (void)initContentView{
    [[NSBundle mainBundle] loadNibNamed:@"KWMSearchBrandView" owner:self options:nil];
    [self addSubview:_vContent];
    self.vContent.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.brandLeft.constant = (UI_SCREEN_WIDTH - 71)*0.5;
    self.searchBtnRight.constant = 10;
    self.tfBrand.returnKeyType = UIReturnKeySearch;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.vContent.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}


- (void)startAnimate{
    if (isSearching) {//搜索状态
        self.brandLeft.constant = 22 + 10 + 71 - UI_SCREEN_WIDTH;
        self.searchBtnRight.constant = UI_SCREEN_WIDTH - 22 - 30 + 15;
        self.tfWidth.constant = UI_SCREEN_WIDTH - (30+10+22)-(30+6) - 10;
        self.btnBackLeft.constant = -UI_SCREEN_WIDTH;
    }else{//不是搜索状态
        self.brandLeft.constant = (UI_SCREEN_WIDTH - 71)*0.5;
        self.searchBtnRight.constant = 10;
        self.btnBackLeft.constant = 0;
    }
    [UIImageView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
        if (isSearching) {
            self.tfBrand.text = nil;
            [self.tfBrand becomeFirstResponder];
        }
        self.btnCancel.hidden = !isSearching;
        self.tfBrand.hidden = !isSearching;
    }];
}

- (IBAction)onClickSearchBtn:(id)sender {
    NSLog(@"点击了搜索按钮");
    if(!isSearching){
        isSearching = YES;
        [self startAnimate];
    }
}
- (IBAction)onClickACancelBtn:(id)sender {
    NSLog(@"点击了取消按钮");
    self.btnCancel.hidden = YES;
    isSearching = NO;
    [self.tfBrand resignFirstResponder];
    [self startAnimate];
    if ([self.delegate respondsToSelector:@selector(kwm_cancelSearch)]) {
        [self.delegate kwm_cancelSearch];
    }
}

//点击背景取消动画
- (void)CancelSearch{
    self.btnCancel.hidden = YES;
    isSearching = NO;
    [self.tfBrand resignFirstResponder];
    [self startAnimate];
}

- (void)onClickBackBtn:(id)sender {
    NSLog(@"点击返回按钮");
    if ([self.delegate respondsToSelector:@selector(kwm_GoBackLastController)]) {
        [self.delegate kwm_GoBackLastController];
    }
}

- (IBAction)tfValueChanged:(id)sender {

    if ([self.delegate respondsToSelector:@selector(kwm_tfValueChange:)]) {
        [self.delegate kwm_tfValueChange:_tfBrand.text];
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([self.delegate respondsToSelector:@selector(kwm_tfSearchBeginEdit)]) {
        [self.delegate kwm_tfSearchBeginEdit];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([textField.text isEqualToString:@""] && [string isEqualToString:@" "]) {
        if ([self.delegate respondsToSelector:@selector(kwm_InputSpace:)]) {
            [self.delegate kwm_InputSpace:textField.text];
        }
        return NO;
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    if ([self.delegate respondsToSelector:@selector(kwm_InputSpace:)]) {
        [self.delegate kwm_InputSpace:textField.text];
    }
    return YES;
}

@end