//
//  GMEmptyView.m
//  Gengmei
//
//  Created by Terminator on 15/10/16.
//  Copyright © 2015年 Wanmeichuangyi. All rights reserved.
//

#import "GMEmptyView.h"
#import <Masonry/Masonry.h>
#import "GMButton.h"
#import "GMFont.h"
#import "UIView+Layout.h"
#import <UIColor+GMTheme.h>
#import <Constant.h>

@implementation GMEmptyView

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

- (void)awakeFromNib{
    [super awakeFromNib];
    [self setup];
}

- (void)setup{
    self.backgroundColor = UIColor.whiteColor;
    _tipIcon = [UIImageView new];
    _tipLabel = [UILabel new];
    _tipButton = [GMButton buttonWithCustomType:GMButtonTypeRedSemicircleGradient];
    _tipButton.frame = CGRectMake(0, 0, 10, 10); //给一个默认的rect，让系统调用DrawRect方法
    [self addSubview:_tipIcon];
    [_tipIcon addSubview:_tipLabel];
    [self addSubview:_tipButton];

    _tipLabel.numberOfLines = 2;
    _tipLabel.preferredMaxLayoutWidth = 300;
    _tipLabel.textColor = UIColor.bodyText;
    _tipLabel.font = [UIFont gmFont:15];
    _tipLabel.textAlignment = NSTextAlignmentCenter;

    _tipButton.hidden = YES;
    [_tipButton setTitle:@"重新加载" forState:UIControlStateNormal];
    _tipButton.titleLabel.font = [UIFont gmFont:16];
    [_tipButton setTitleColor:UIColor.bodyText forState:UIControlStateNormal];
    [_tipButton addTarget:self action:@selector(reloadBtnTap) forControlEvents:UIControlEventTouchUpInside];

    // 剩下的位置信息将在layoutSubViews在指定
    [_tipLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_tipIcon.mas_bottom).offset(-15);
        make.centerX.mas_equalTo(self);
    }];
    [_tipButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(33);
    }];
}

- (void)setType:(GMEmptyViewType)type{
    _type = type;
    switch (self.type) {
        case GMEmptyViewTypeEmpty:{
            _tipButton.hidden = YES;
            _tipIcon.image = [UIImage imageNamed:@"empty"];
            _tipLabel.text = @"此处太寂寥，社区等你撩";
            break;
        }
        case GMEmptyViewTypeException:{
            _tipButton.hidden = NO;
            _tipIcon.image = [UIImage imageNamed:@"network_failed"];
            _tipLabel.text = @"原谅我一看到美人就不淡定";
        }
        default:
            break;
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];

    // 在即将显示时，通过判断emptyView.y的坐标来优化subviews的坐标，以达到UI的要求
    CGRect emptyViewFrameInWindow = [self.superview convertRect:self.frame toView:[UIApplication sharedApplication].keyWindow];
    CGRect newTipIconFrameInWindow = CGRectMake(0, 0, 175, 199);
    newTipIconFrameInWindow.origin.x = (MAINSCREEN_WIDTH - newTipIconFrameInWindow.size.width) / 2;

    /*
     以下坐标均相对于window
     按照设计图
     如果emptyViewFrameInWindow.y <= 64，那么tipIcon.y = 155
     如果emptyViewFrameInWindow.y > 64 && y < 140，那么tipIcon.y = 185
     另外测试时发现标签详情页的头部更多，所有else分支也成为了一种可能
     如果emptyViewFrameInWindow.y > 140，那么tipIcon.y = 220
     */
    // 按照设计图总结出来，
    if (emptyViewFrameInWindow.origin.y <= 64) {
        // 正常空间
        newTipIconFrameInWindow.origin.y = 155;
    } else if (emptyViewFrameInWindow.origin.y > 64.0 && emptyViewFrameInWindow.origin.y <= 140){

        // 需要更多空间
        newTipIconFrameInWindow.origin.y = 185;
    } else {
        newTipIconFrameInWindow.origin.y = 220;
    }

    CGRect newTipIconFrame = [[UIApplication sharedApplication].keyWindow convertRect:newTipIconFrameInWindow toView:self];
    newTipIconFrameInWindow.origin.y = newTipIconFrame.origin.y;
    _tipIcon.frame = newTipIconFrameInWindow;
    _tipButton.top = _tipIcon.bottom + 15;
}

- (void)reloadBtnTap {
    if ([self.delegate respondsToSelector:@selector(emptyViewDidClickReload)]) {
        [self.delegate emptyViewDidClickReload];
    }
}
@end
