//
//  KKBubbleView.m
//  Demo
//
//  Created by 雷俊博 on 2021/8/2.
//  Copyright © 2021 iHandle. All rights reserved.
//

#import "KKBubbleView.h"

#define kArrorHeight    15      //底部距离高度
@interface KKBubbleView ()

@property (nonatomic, assign)CGFloat bubbleWidth;

@property (nonatomic, assign)CGFloat bubbleHeight;

@property (nonatomic, strong)UILabel *lb_text;

@end

@implementation KKBubbleView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.bubbleWidth = frame.size.width;
        self.bubbleHeight = frame.size.height;
        [self setDefaultProperty];
        [self setupUI];
    }
    return self;
}

// 设置默认参数
- (void)setDefaultProperty
{
    _arrowWidth = 12;
    _arrowHeight = 6;
    _arrowToLeftDistance = (self.bubbleHeight - self.arrowHeight)/2.0;
}

- (void)setupUI
{
    UILabel *lb_context = [UILabel new];
    lb_context.text = @"可以切换新闻模式啦";
    lb_context.textAlignment = NSTextAlignmentCenter;
    lb_context.font = [UIFont systemFontOfSize:12];
    lb_context.textColor = [UIColor whiteColor];
    [self addSubview:lb_context];
    self.lb_text = lb_context;
    self.lb_text.frame = CGRectMake((self.bubbleHeight - self.arrowHeight)/2.0, self.arrowHeight, self.bubbleWidth - (self.bubbleHeight - self.arrowHeight) , self.bubbleHeight - self.arrowHeight);
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    [self drawInContext:UIGraphicsGetCurrentContext()];
    
//    self.layer.shadowColor = [[UIColor clearColor] CGColor];
//
//    self.layer.shadowOpacity = 1.0;
//    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}

- (void)drawInContext:(CGContextRef)context
{
    CGContextSetLineWidth(context, 2.0);

       
    CGContextSetFillColorWithColor(context, K_MainColor.CGColor); //气泡填充色

       
    [self getDrawPath:context];
}

//气泡背景绘制

- (void)getDrawPath:(CGContextRef)context

{
    

    CGContextSaveGState(context); //保存上下文 1

    

    CGContextBeginPath(context);

    

    //底部三角

    CGContextMoveToPoint(context, self.arrowToLeftDistance, self.arrowHeight);

    CGContextAddLineToPoint(context, self.arrowToLeftDistance + self.arrowWidth/2.0, 0);

    CGContextAddLineToPoint(context, self.arrowToLeftDistance + self.arrowWidth, self.arrowHeight);
    
    CGContextAddLineToPoint(context, self.bubbleWidth - (self.bubbleHeight - self.arrowHeight)/2.0, self.arrowHeight);
    
    CGContextAddArc(context, self.bubbleWidth - (self.bubbleHeight - self.arrowHeight)/2.0, self.arrowHeight + (self.bubbleHeight - self.arrowHeight)/2.0, (self.bubbleHeight - self.arrowHeight)/2.0, - M_PI/2.0, M_PI/2.0, 0);//画圆
    
    CGContextAddLineToPoint(context, (self.bubbleHeight - self.arrowHeight)/2.0, self.bubbleHeight);
    
    CGContextAddArc(context, (self.bubbleHeight - self.arrowHeight)/2.0, self.arrowHeight + (self.bubbleHeight - self.arrowHeight)/2.0, (self.bubbleHeight - self.arrowHeight)/2.0,  M_PI/2.0, 3 *M_PI/2.0, 0);//画圆
    
    CGContextAddLineToPoint(context, self.arrowToLeftDistance, self.arrowHeight);

    CGContextFillPath(context); //渲染三角形



    CGContextClosePath(context);

}

@end
