//
//  GMDrawDottedView.m
//  Gengmei
//
//  Created by 刘鹿杰 on 2019/6/27.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

#import "GMDrawDottedView.h"

#define HArrowSize CGSizeMake(4.5, 3.5)       // 水平线时箭头宽高
#define VArrowSize CGSizeMake(3.5, 4.5)       // 竖直线时箭头宽高
#define HLineStrat 6.5                        // 画虚线的起始点位置
#define arrowWidth 3.5

@interface GMDrawDottedView()

@property (nonatomic, assign) GMLineType lineType;    // 控制横竖线
@property (nonatomic, assign) GMWordPlace wordPlace;  // 位置
@property (nonatomic, copy) NSString *dect;           // 展示文字

@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UIImageView *rigthImageView;
@property (nonatomic, strong) UIImageView *upImageView;
@property (nonatomic, strong) UIImageView *bottomImageView;

@property (nonatomic, strong) GMLabel *textLabel;  // 展示文字label
@property (nonatomic, assign) CGSize labelSize;    // 文字的尺寸

@property (nonatomic, strong)NSMutableArray *pointArray;

@end

@implementation GMDrawDottedView

- (instancetype)initWithLineType:(GMLineType)lineType {
    if (self = [super init]) {
        self.lineType = lineType;
        [self setUpUI];
    }
    return self;
}

- (instancetype)initWithLineType:(GMLineType)lineType addlabel:(GMWordPlace)wordPlace addDect:(NSString *)dect {
    if (self = [super init]) {
        self.lineType = lineType;
        self.wordPlace = wordPlace;
        self.dect = dect;
        [self calculateSize];
        [self setUpUI];
    }
    return self;
}

- (void)calculateSize {
    self.labelSize = [self.dect sizeWithAttributes:@{NSFontAttributeName:[UIFont gmFont:10]}];
}

- (void)setUpUI {
    if (self.lineType == drawViewTypeHorizontalDotted) {
        self.leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"facev3_arrowleft"]];
        [self addSubview:_leftImageView];
        self.rigthImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"facev3_arrowrigth"]];
        [self addSubview:_rigthImageView];
    }else{
        self.leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"facev3_arrowtop"]];
        [self addSubview:_leftImageView];
        self.rigthImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"facev3_arrowdown"]];
        [self addSubview:_rigthImageView];
    }
    self.textLabel = [GMLabel labelWithTextColor:UIColor.whiteColor fontSize:9];
    self.textLabel.shadowColor = [UIColor colorWithHexString:@"000000" alpha:0.3];
    self.textLabel.shadowOffset = CGSizeMake(0, 1);
    self.textLabel.layer.shadowRadius = 0.5;
    _textLabel.textAlignment = NSTextAlignmentCenter;
    _textLabel.numberOfLines = 2;
    _textLabel.text = self.dect;
    [self addSubview:_textLabel];
    // 添加约束
    [self addDectLabelConstraints];
}

// 添加文字约束
- (void)addDectLabelConstraints {
    if (self.lineType == drawViewTypeHorizontalDotted) {  // 如果为横线
        [self addBaseHorizontalConstraints];  // 添加基本布局
        if (self.wordPlace == wordPlaceCenter) {
            [self addHorizontalCenterDottedConstraints];   // 文字在中间
        }else if (self.wordPlace == wordPlaceTop) {
            [self addHorizontalTopDottedConstraints];      // 文字在上面
        }else {
            [self addHorizontalBottomDottedConstraints];   // 文字在下面
        }
    }
    if (self.lineType == drawViewTypeVerticalDotted) {  // 如果为竖线
        [self addBaseVerticalConstraints];  // 添加基本布局
        if (self.wordPlace == wordPlaceCenter) {
            [self addvertlcalCenterDottedConstraints];    // 文字在中间
        }else if (self.wordPlace == wordPlaceLeft) {
            [self addvertlcalLeftDottedConstraints];    // 文字在左侧
        }else { // 文字在下面
            [self addvertlcalRightDottedConstraints];    // 文字在右侧
        }
    }
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGFloat hLineWidth = self.width - HLineStrat * 2;
    CGFloat hLinehight = self.height - HLineStrat * 2;
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] set];
    CGContextSetLineWidth(context, 0.5);
    CGFloat length[] = {2,2};
    CGContextSetLineDash(context, 0, length, 2);
    if (self.lineType == drawViewTypeHorizontalDotted) {  // 横线
        if (self.wordPlace == wordPlaceCenter) {
            CGContextMoveToPoint(context, HLineStrat, self.height / 2);
            CGContextAddLineToPoint(context, (hLineWidth - self.labelSize.width)/ 2 + HLineStrat, self.height / 2);
            CGContextMoveToPoint(context, (hLineWidth + self.labelSize.width) / 2 + HLineStrat, self.height / 2);
            CGContextAddLineToPoint(context, hLineWidth + HLineStrat, self.height / 2);
        }else if (self.wordPlace == wordPlaceTop) {
            CGContextMoveToPoint(context, HLineStrat, self.height - arrowWidth / 2);
            CGContextAddLineToPoint(context, hLineWidth + HLineStrat,self.height - arrowWidth / 2);
        }else{
            CGContextMoveToPoint(context, HLineStrat, arrowWidth / 2);
            CGContextAddLineToPoint(context, hLineWidth + HLineStrat, arrowWidth / 2);
        }
    }else{ // 竖线
        if (self.wordPlace == wordPlaceCenter) {
            CGFloat centerX = self.width / 2;
            CGContextMoveToPoint(context, centerX, HLineStrat);
            CGContextAddLineToPoint(context, centerX, (hLinehight - self.labelSize.height)/ 2 + HLineStrat);
            CGContextMoveToPoint(context, centerX, (hLinehight + self.labelSize.height) / 2 + HLineStrat);
            CGContextAddLineToPoint(context, centerX, hLinehight + HLineStrat);
        }else if (self.wordPlace == wordPlaceRight) {
            CGContextMoveToPoint(context, arrowWidth / 2, HLineStrat);
            CGContextAddLineToPoint(context, arrowWidth / 2,hLinehight + HLineStrat);
        }else{
            CGContextMoveToPoint(context, self.width - arrowWidth / 2, HLineStrat);
            CGContextAddLineToPoint(context, self.width - arrowWidth / 2,hLinehight + HLineStrat);
        }
    }
    CGContextStrokePath(context);
}


#pragma mark 为水平线时的约束

// 为水平线时，leftImageView、rigthImageView 基本布局
- (void)addBaseHorizontalConstraints {
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_labelSize.width);
        make.height.mas_equalTo(_labelSize.height);
        make.centerX.mas_equalTo(self.centerX);
    }];
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(HArrowSize.width);
        make.height.mas_equalTo(HArrowSize.height);
        make.left.mas_equalTo(0);
    }];
    [self.rigthImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(HArrowSize.width);
        make.height.mas_equalTo(HArrowSize.height);
        make.right.mas_equalTo(0);
    }];
}

// 水平线 - 文字在中心位置
- (void)addHorizontalCenterDottedConstraints {
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.center);
    }];
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.centerY);
    }];
    [self.rigthImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.centerY);
    }];
}

// 水平线 - 文字在上放位置
- (void)addHorizontalTopDottedConstraints {
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.top);
    }];
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.bottom);
    }];
    [self.rigthImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.bottom);
    }];
}

// 水平线 - 文字在下方位置
- (void)addHorizontalBottomDottedConstraints {
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.top);
    }];
    [self.rigthImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.top);
    }];
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.bottom);
    }];
}


#pragma mark 为竖线时的约束

// 为竖线时，leftImageView、rigthImageView 基本布局
- (void)addBaseVerticalConstraints {
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_labelSize.width);
        make.height.mas_equalTo(_labelSize.height);
        make.centerY.mas_equalTo(self.centerY);
    }];
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(VArrowSize.width);
        make.height.mas_equalTo(VArrowSize.height);
        make.top.mas_equalTo(self.top);
    }];
    [self.rigthImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(VArrowSize.width);
        make.height.mas_equalTo(VArrowSize.height);
        make.bottom.mas_equalTo(self.bottom);
    }];
}

- (void)addvertlcalCenterDottedConstraints {
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.center);
    }];
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.centerX);
    }];
    [self.rigthImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.centerX);
    }];
}

- (void)addvertlcalLeftDottedConstraints {
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.left);
    }];
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.right);
    }];
    [self.rigthImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.right);
    }];
}

- (void)addvertlcalRightDottedConstraints {
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.left);
    }];
    [self.rigthImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.left);
    }];
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.right);
    }];
}

@end
