#import "DashboardHeaderView.h"

@implementation DashboardHeaderView

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

- (void)setupUI {
    self.backgroundColor = [UIColor colorWithRed:0.0 green:0.43 blue:0.55 alpha:1.0]; // #006D8D

    // Background image
    _backgroundImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    _backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    _backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self addSubview:_backgroundImageView];

    // Overlay image
    _overlayImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    _overlayImageView.contentMode = UIViewContentModeScaleAspectFill;
    _overlayImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _overlayImageView.alpha = 0.4;
    [self addSubview:_overlayImageView];

    // Header container (for back, title, menu)
    UIView *headerBar = [[UIView alloc] init];
    headerBar.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:headerBar];

    // Back button part of header now

    // Project name (center title)
    _projectNameLabel = [[UILabel alloc] init];
    _projectNameLabel.textColor = [UIColor whiteColor];
    _projectNameLabel.font = [UIFont boldSystemFontOfSize:18];
    _projectNameLabel.textAlignment = NSTextAlignmentCenter;
    _projectNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [headerBar addSubview:_projectNameLabel];

    // Menu button
    UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [menuButton setTitle:@"Menu" forState:UIControlStateNormal];
    [menuButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    menuButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    menuButton.translatesAutoresizingMaskIntoConstraints = NO;
    [headerBar addSubview:menuButton];

    // Unit name (bottom left)
    _unitNameLabel = [[UILabel alloc] init];
    _unitNameLabel.textColor = [UIColor whiteColor];
    _unitNameLabel.font = [UIFont boldSystemFontOfSize:20];
    _unitNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_unitNameLabel];

    // Info button (bottom right)
    _infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _infoButton.translatesAutoresizingMaskIntoConstraints = NO;
    [_infoButton setImage:[UIImage systemImageNamed:@"info.circle"] forState:UIControlStateNormal];
    _infoButton.tintColor = [UIColor whiteColor];
    [self addSubview:_infoButton];

    // Layout constraints
    [NSLayoutConstraint activateConstraints:@[
        // Header bar top
        [headerBar.topAnchor constraintEqualToAnchor:self.safeAreaLayoutGuide.topAnchor constant:0],
        [headerBar.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:0],
        [headerBar.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:0],
        [headerBar.heightAnchor constraintEqualToConstant:44],

        // Back button

        // Menu button
        [menuButton.trailingAnchor constraintEqualToAnchor:headerBar.trailingAnchor constant:-16],
        [menuButton.centerYAnchor constraintEqualToAnchor:headerBar.centerYAnchor],

        // Title centered
        [_projectNameLabel.centerXAnchor constraintEqualToAnchor:headerBar.centerXAnchor],
        [_projectNameLabel.centerYAnchor constraintEqualToAnchor:headerBar.centerYAnchor],

        // Unit name bottom-left
        [_unitNameLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:16],
        [_unitNameLabel.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-16],

        // Info button bottom-right
        [_infoButton.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-16],
        [_infoButton.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-18],
        [_infoButton.widthAnchor constraintEqualToConstant:24],
        [_infoButton.heightAnchor constraintEqualToConstant:24],
    ]];
}

- (void)configureWithProjectName:(NSString *)projectName
                        unitName:(NSString *)unitName
                backgroundImage:(UIImage *)backgroundImage
                   overlayImage:(UIImage *)overlayImage {
    self.projectNameLabel.text = projectName ?: @"Project Name";
    self.unitNameLabel.text = unitName ?: @"Unit Name";
    self.backgroundImageView.image = backgroundImage ?: [UIImage imageNamed:@"placeholder_header"];
    self.overlayImageView.image = overlayImage ?: [UIImage imageNamed:@"placeholder_overlay"];
}

@end
