//  CalendarDayCell.mm

#import "CalendarDayCell.h"

@implementation CalendarDayCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.selectionRing = [[UIView alloc] init];
        self.selectionRing.translatesAutoresizingMaskIntoConstraints = NO;
        self.selectionRing.layer.cornerRadius = 20;
        self.selectionRing.layer.masksToBounds = YES;
        self.selectionRing.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.selectionRing];

        self.dayLabel = [[UILabel alloc] init];
        self.dayLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self.dayLabel.textAlignment = NSTextAlignmentCenter;
        self.dayLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
        [self.selectionRing addSubview:self.dayLabel];

        self.statusDot = [[UIView alloc] init];
        self.statusDot.translatesAutoresizingMaskIntoConstraints = NO;
        self.statusDot.layer.cornerRadius = 5;
        self.statusDot.hidden = YES;
        [self.contentView addSubview:self.statusDot];

        [NSLayoutConstraint activateConstraints:@[
            [self.selectionRing.centerXAnchor constraintEqualToAnchor:self.contentView.centerXAnchor],
            [self.selectionRing.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor constant:-6],
            [self.selectionRing.widthAnchor constraintEqualToConstant:40],
            [self.selectionRing.heightAnchor constraintEqualToConstant:40],

            [self.dayLabel.centerXAnchor constraintEqualToAnchor:self.selectionRing.centerXAnchor],
            [self.dayLabel.centerYAnchor constraintEqualToAnchor:self.selectionRing.centerYAnchor],

            [self.statusDot.topAnchor constraintEqualToAnchor:self.selectionRing.bottomAnchor constant:6],
            [self.statusDot.centerXAnchor constraintEqualToAnchor:self.selectionRing.centerXAnchor],
            [self.statusDot.widthAnchor constraintEqualToConstant:10],
            [self.statusDot.heightAnchor constraintEqualToConstant:10],
        ]];
    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    self.dayLabel.text = @"";
    self.statusDot.hidden = YES;
    self.selectionRing.backgroundColor = [UIColor clearColor];
    self.dayLabel.textColor = [UIColor blackColor];
}

- (void)configureWithDay:(nullable NSNumber *)day
               inMonth:(BOOL)inMonth
                enabled:(BOOL)enabled
                 status:(nullable NSString *)status
               selected:(BOOL)selected
{
    if (day == nil || !inMonth) {
        // empty cell (padding)
        self.dayLabel.text = @"";
        self.userInteractionEnabled = NO;
        self.dayLabel.alpha = 0.0;
        self.statusDot.hidden = YES;
        return;
    }

    self.dayLabel.alpha = 1.0;
    self.dayLabel.text = [NSString stringWithFormat:@"%@", day];
    self.userInteractionEnabled = enabled;

    // style for enabled/disabled
    if (!enabled) {
        self.dayLabel.textColor = [UIColor colorWithWhite:0.8 alpha:1.0];
    } else {
        self.dayLabel.textColor = [UIColor colorWithWhite:0.12 alpha:1.0];
    }

    // status dot
    if (status != nil && status.length > 0) {
        self.statusDot.hidden = NO;
        if ([status isEqualToString:@"available"]) {
            self.statusDot.backgroundColor = [UIColor colorWithRed:0.27 green:0.85 blue:0.36 alpha:1.0]; // green
        } else if ([status isEqualToString:@"limited"]) {
            self.statusDot.backgroundColor = [UIColor colorWithRed:1.0 green:0.59 blue:0.0 alpha:1.0]; // orange
        } else if ([status isEqualToString:@"unavailable"]) {
            self.statusDot.backgroundColor = [UIColor colorWithRed:1.0 green:0.23 blue:0.19 alpha:1.0]; // red
        } else {
            self.statusDot.hidden = YES;
        }
    } else {
        self.statusDot.hidden = YES;
    }

    // selected ring
    if (selected) {
        self.selectionRing.backgroundColor = [UIColor colorWithRed:0.0 green:0.43 blue:0.55 alpha:1.0];
        self.dayLabel.textColor = [UIColor whiteColor];
    } else {
        self.selectionRing.backgroundColor = [UIColor clearColor];
        // small subtle background for current day could be added here
    }
}

@end

