//
//  FYVerifiButton.m
//  FYIntelligentBox
//
//  Created by PCD on 2018/6/21.
//  Copyright © 2018年 PCD. All rights reserved.
//

#import "SLVerifiButton.h"

@interface SLVerifiButton()

@property(nonatomic,retain) NSTimer *timer;

@property(nonatomic,assign) NSInteger nCount;

@end

@implementation SLVerifiButton


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [self addTarget:self action:@selector(activeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    _isEnAble = YES;
}


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        [self addTarget:self action:@selector(activeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return self;
}

-(void) activeButtonClick:(id)send
{
//    NSString *strPhone =self.iphoneField.text;
//
//    if (strPhone && strPhone.length == 11) {
    
//        [[FYVerifiCodeButtonViewMode shareVerifiCodeButtonViewMode] sendVerifiCode:strPhone Type:_strType];
//

//        
//        if (self.buttonClickBlock) {
//            self.buttonClickBlock(YES);
//        }
//
//    }else{
//
//        [PCUtil showAlert:Prompt message:@"请输入手机号码！"];
//    }
//
    
//    _nCount = 60;
//    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
//
    if (self.isEnAble) {
        if (self.buttonClickBlock) {
            self.buttonClickBlock(YES);
        }
    }
}


- (void)startTiming
{
    if (_isEnAble) {
        _nCount = 60;
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        
        _isEnAble = NO;
    }
}

-(void)setStrTitle:(NSString *)strTitle
{
    _strTitle =strTitle;
    [self setTitle:strTitle forState:UIControlStateNormal];
}

-(void)timerAction
{
    _nCount --;
    if (_nCount <=0) {
        [self setTitle:_strTitle forState:UIControlStateNormal];
        [_timer invalidate];
        _timer = nil;
        _isEnAble = YES;
//        [self setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

    }else{
        
        [self setTitle:[NSString stringWithFormat:@"%ld秒",_nCount] forState:UIControlStateNormal];
//        [self setTitleColor:YMColor(E84848"] forState:UIControlStateNormal];

    }
}

@end
