// FilterViewController.m
#import "FilterViewController.h"

@interface FilterViewController ()
@property (nonatomic, strong) UIDatePicker *fromPicker;
@property (nonatomic, strong) UIDatePicker *toPicker;
@property (nonatomic, strong) UISegmentedControl *statusControl;
@property (nonatomic, strong) UIButton *applyButton;
@property (nonatomic, strong) UIButton *resetButton;
@end

@implementation FilterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor systemBackgroundColor];
    self.view.layer.cornerRadius = 12.0;
    
    UILabel *fromLabel = [[UILabel alloc] init];
    fromLabel.text = @"From:";
    fromLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:fromLabel];
    
    self.fromPicker = [[UIDatePicker alloc] init];
    self.fromPicker.datePickerMode = UIDatePickerModeDate;
    self.fromPicker.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.fromPicker];
    
    UILabel *toLabel = [[UILabel alloc] init];
    toLabel.text = @"To:";
    toLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:toLabel];
    
    self.toPicker = [[UIDatePicker alloc] init];
    self.toPicker.datePickerMode = UIDatePickerModeDate;
    self.toPicker.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.toPicker];
    
    self.statusControl = [[UISegmentedControl alloc] initWithItems:@[@"All", @"Status", @"Pending Access", @"W.I.P"]];
    self.statusControl.selectedSegmentIndex = 0;
    self.statusControl.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.statusControl];
  
    self.resetButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.resetButton setTitle:@"Reset" forState:UIControlStateNormal];
    self.resetButton.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
    [self.resetButton setTitleColor:[UIColor systemRedColor] forState:UIControlStateNormal];
    self.resetButton.layer.cornerRadius = 8;
    self.resetButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.resetButton addTarget:self action:@selector(resetTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.resetButton];

    self.applyButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.applyButton setTitle:@"Apply Filter" forState:UIControlStateNormal];
    self.applyButton.backgroundColor = [UIColor colorWithRed:0.0 green:0.43 blue:0.55 alpha:1.0];
    [self.applyButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.applyButton.layer.cornerRadius = 8;
    self.applyButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.applyButton addTarget:self action:@selector(applyTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.applyButton];
    
    [NSLayoutConstraint activateConstraints:@[
        [fromLabel.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:20],
        [fromLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:16],
        
        [self.fromPicker.topAnchor constraintEqualToAnchor:fromLabel.bottomAnchor constant:4],
        [self.fromPicker.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:16],
        [self.fromPicker.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-16],
        
        [toLabel.topAnchor constraintEqualToAnchor:self.fromPicker.bottomAnchor constant:12],
        [toLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:16],
        
        [self.toPicker.topAnchor constraintEqualToAnchor:toLabel.bottomAnchor constant:4],
        [self.toPicker.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:16],
        [self.toPicker.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-16],
        
        [self.statusControl.topAnchor constraintEqualToAnchor:self.toPicker.bottomAnchor constant:20],
        [self.statusControl.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:16],
        [self.statusControl.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-16],
        
        // 🔹 Reset + Apply buttons side by side
        [self.resetButton.topAnchor constraintEqualToAnchor:self.statusControl.bottomAnchor constant:20],
        [self.resetButton.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40],
        [self.resetButton.widthAnchor constraintEqualToConstant:120],
        [self.resetButton.heightAnchor constraintEqualToConstant:40],
        
        [self.applyButton.centerYAnchor constraintEqualToAnchor:self.resetButton.centerYAnchor],
        [self.applyButton.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-40],
        [self.applyButton.widthAnchor constraintEqualToConstant:150],
        [self.applyButton.heightAnchor constraintEqualToConstant:40],
    ]];

}

- (void)applyTapped {
    if (self.onApplyFilter) {
        NSString *selectedStatus = [self.statusControl titleForSegmentAtIndex:self.statusControl.selectedSegmentIndex];
        self.onApplyFilter(self.fromPicker.date, self.toPicker.date, selectedStatus);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)resetTapped {
    self.fromPicker.date = [NSDate date];
    self.toPicker.date = [NSDate date];
    self.statusControl.selectedSegmentIndex = 0;

    // Optional: give feedback
    [UIView animateWithDuration:0.2 animations:^{
        self.resetButton.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
    } completion:^(BOOL finished) {
        self.resetButton.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
    }];
}

@end
