//  TutorialBubbleView.mm
#import "TutorialBubbleView.h"

static CGFloat const kArrowHeight = 14.0;
static CGFloat const kArrowPadding = 8.0;

@interface TutorialBubbleView ()
@property (nonatomic, strong) UILabel *stepLabel;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIStackView *buttonStack;
@property (nonatomic, strong) CAShapeLayer *arrowLayer;
@property (nonatomic, strong) NSLayoutConstraint *topConstraint;
@property (nonatomic, strong) NSLayoutConstraint *bottomConstraint;
@end

@implementation TutorialBubbleView

- (instancetype)init {
    self = [super initWithFrame:CGRectZero];
    if (!self) return nil;

    self.backgroundColor = UIColor.whiteColor;
    self.layer.cornerRadius = 16;
    self.layer.shadowColor = UIColor.blackColor.CGColor;
    self.layer.shadowOpacity = 0.15;
    self.layer.shadowRadius = 12;
    self.layer.shadowOffset = CGSizeMake(0, 6);
    self.layer.masksToBounds = NO;
    self.translatesAutoresizingMaskIntoConstraints = NO;

    CGFloat padding = 18;

    // ───────── Step container (pill) ─────────
    UIView *stepContainer = [[UIView alloc] init];
    stepContainer.translatesAutoresizingMaskIntoConstraints = NO;
    stepContainer.backgroundColor =
        [[UIColor systemTealColor] colorWithAlphaComponent:0.15];
    stepContainer.layer.cornerRadius = 10;
    [self addSubview:stepContainer];

    self.stepLabel = [[UILabel alloc] init];
    self.stepLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightSemibold];
    self.stepLabel.textColor = UIColor.secondaryLabelColor;
    self.stepLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.stepLabel.textAlignment = NSTextAlignmentCenter;
    [stepContainer addSubview:self.stepLabel];

    // ───────── Message label ─────────
    self.messageLabel = [[UILabel alloc] init];
    self.messageLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    self.messageLabel.textColor = UIColor.labelColor;
    self.messageLabel.numberOfLines = 0;
    self.messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:self.messageLabel];

    UIColor *primaryColor = [UIColor colorWithRed:0.0 green:0.5 blue:0.6 alpha:1.0];

     // ───────── Prev Button ─────────
     self.prevButton = [UIButton buttonWithType:UIButtonTypeSystem];
     [self.prevButton setTitle:@"Back" forState:UIControlStateNormal];
     self.prevButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
     [self.prevButton setTitleColor:primaryColor forState:UIControlStateNormal];
     self.prevButton.layer.cornerRadius = 10;
     self.prevButton.layer.borderWidth = 0.5;
     self.prevButton.layer.borderColor = primaryColor.CGColor;
     self.prevButton.contentEdgeInsets = UIEdgeInsetsMake(8, 18, 8, 18);
     self.prevButton.translatesAutoresizingMaskIntoConstraints = NO;
     [self addSubview:self.prevButton];

     // ───────── Next Button ─────────
     self.nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
     [self.nextButton setTitle:@"Next" forState:UIControlStateNormal];
     self.nextButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
     self.nextButton.backgroundColor = primaryColor;
     [self.nextButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
     self.nextButton.layer.cornerRadius = 10;
     self.nextButton.contentEdgeInsets = UIEdgeInsetsMake(8, 18, 8, 18);
     self.nextButton.translatesAutoresizingMaskIntoConstraints = NO;
     [self addSubview:self.nextButton];

    [NSLayoutConstraint activateConstraints:@[
        [self.heightAnchor constraintGreaterThanOrEqualToConstant:120],
        
        // Step pill
        [stepContainer.topAnchor constraintEqualToAnchor:self.topAnchor
                                                constant:padding],
        [stepContainer.leadingAnchor constraintEqualToAnchor:self.leadingAnchor
                                                    constant:padding],

        [self.stepLabel.topAnchor constraintEqualToAnchor:stepContainer.topAnchor constant:4],
        [self.stepLabel.bottomAnchor constraintEqualToAnchor:stepContainer.bottomAnchor constant:-4],
        [self.stepLabel.leadingAnchor constraintEqualToAnchor:stepContainer.leadingAnchor constant:10],
        [self.stepLabel.trailingAnchor constraintEqualToAnchor:stepContainer.trailingAnchor constant:-10],

        // Message
        [self.messageLabel.topAnchor constraintEqualToAnchor:stepContainer.bottomAnchor constant:8],
        [self.messageLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:padding],
        [self.messageLabel.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-padding],
        
        //Prev Button
        [self.prevButton.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:12],
        [self.prevButton.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-12],
        [self.prevButton.trailingAnchor constraintEqualToAnchor:self.centerXAnchor constant:-12],
        [self.prevButton.topAnchor constraintEqualToAnchor:self.messageLabel.bottomAnchor constant:16],

        //Next Button
        [self.nextButton.leadingAnchor constraintEqualToAnchor:self.centerXAnchor constant:12],
        [self.nextButton.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-12],
        [self.nextButton.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-12],
        [self.nextButton.topAnchor constraintEqualToAnchor:self.messageLabel.bottomAnchor constant:16],
    ]];

    // ───────── Arrow ─────────
    self.arrowLayer = [CAShapeLayer layer];
    self.arrowLayer.fillColor = UIColor.whiteColor.CGColor;
    [self.layer addSublayer:self.arrowLayer];

    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat arrowWidth = 18.0;
    CGFloat arrowHeight = kArrowHeight;

    CGFloat centerX = (self.arrowCenterX > 0) ? self.arrowCenterX : CGRectGetMidX(self.bounds);

    UIBezierPath *path = [UIBezierPath bezierPath];

    if (self.arrowDirection == BubbleArrowDirectionUp) {
        self.arrowLayer.frame = CGRectMake(0, -arrowHeight, self.bounds.size.width, self.bounds.size.height + arrowHeight);

        // Arrow tip at the very top
        CGPoint tip = CGPointMake(centerX, 0);
        CGPoint baseLeft = CGPointMake(centerX - arrowWidth / 2, arrowHeight);
        CGPoint baseRight = CGPointMake(centerX + arrowWidth / 2, arrowHeight);

        [path moveToPoint:baseLeft];
        [path addLineToPoint:baseRight];
        [path addLineToPoint:tip];

    } else { // BubbleArrowDirectionDown
        self.arrowLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - arrowHeight);

        CGFloat bubbleHeight = self.bounds.size.height;

        // Arrow tip at the very bottom
        CGPoint tip = CGPointMake(centerX, bubbleHeight + arrowHeight);
        CGPoint baseLeft = CGPointMake(centerX - arrowWidth / 2, bubbleHeight);
        CGPoint baseRight = CGPointMake(centerX + arrowWidth / 2, bubbleHeight);

        [path moveToPoint:baseLeft];
        [path addLineToPoint:baseRight];
        [path addLineToPoint:tip];
    }

    [path closePath];
    self.arrowLayer.path = path.CGPath;
}


- (void)configureWithText:(NSString *)text
                    step:(NSInteger)step
                   total:(NSInteger)total
                  isFirst:(BOOL)isFirst
                   isLast:(BOOL)isLast {

    self.stepLabel.text = [NSString stringWithFormat:@"Step %ld",
                           (long)(step + 1)];

    self.messageLabel.text = text;

    self.prevButton.hidden = isFirst;
    [self.nextButton setTitle:(isLast ? @"Done" : @"Next")
                     forState:UIControlStateNormal];
}

- (void)setArrowDirection:(BubbleArrowDirection)arrowDirection {
    _arrowDirection = arrowDirection;

    CGFloat padding = 20;

    if (arrowDirection == BubbleArrowDirectionUp) {
        self.topConstraint.constant = padding + kArrowHeight;
        self.bottomConstraint.constant = -padding;
    } else {
        self.topConstraint.constant = padding;
        self.bottomConstraint.constant = -(padding + kArrowHeight);
    }

    [self setNeedsLayout];
    [self layoutIfNeeded];
}

- (CGSize)intrinsicContentSize {
    CGSize size = [super intrinsicContentSize];
    return CGSizeMake(size.width, size.height + kArrowHeight);
}


@end
