#import "DetailsViewController.h"

@interface AddIssueDetailsViewController ()
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITextField *locationField;
@property (nonatomic, strong) UITextView *commentField;
@property (nonatomic, strong) UIButton *submitButton;
@property (nonatomic, strong) UIButton *resetButton;
@end

@implementation AddIssueDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;

    // Create all views (don’t position them yet)
    [self createHeader];
    [self createForm];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    CGFloat topInset = self.view.safeAreaInsets.top;
    CGFloat headerHeight = 48.0;

    // Header layout
    self.headerView.frame = CGRectMake(0, topInset, self.view.bounds.size.width, headerHeight);
  
    // Title Label
    UILabel *title = [self.headerView viewWithTag:101];
     if (title) {
         title.frame = CGRectMake(0, 8, self.headerView.bounds.size.width, 32);
     }

    // Scroll view layout (below header)
    CGFloat scrollY = topInset + headerHeight;
    self.scrollView.frame = CGRectMake(0, scrollY,
                                       self.view.bounds.size.width,
                                       self.view.bounds.size.height - scrollY);

    // Adjust internal subviews (since width may have changed)
    self.locationField.frame = CGRectMake(20, 50, self.view.bounds.size.width - 40, 40);
    self.commentField.frame = CGRectMake(20, 140, self.view.bounds.size.width - 40, 100);
    self.submitButton.frame = CGRectMake(20, 260, self.view.bounds.size.width - 40, 48);
}

#pragma mark - Setup

- (void)createHeader {
    // Header container
    self.headerView = [[UIView alloc] init];
    self.headerView.backgroundColor = [UIColor colorWithRed:0/255.0 green:109/255.0 blue:141/255.0 alpha:1];
    self.headerView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.headerView];
    
    // Back button (SF Symbol)
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.translatesAutoresizingMaskIntoConstraints = NO;

    UIImage *backImage = [UIImage systemImageNamed:@"chevron.left"];
    backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [backButton setImage:backImage forState:UIControlStateNormal];
    backButton.tintColor = [UIColor whiteColor];
    backButton.backgroundColor = [UIColor clearColor];
    [backButton addTarget:self action:@selector(dismissSelf)
         forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:backButton];
    
    // Title label
    UILabel *title = [[UILabel alloc] init];
    title.translatesAutoresizingMaskIntoConstraints = NO;
    title.text = @"Add Issue Details";
    title.textColor = UIColor.whiteColor;
    title.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    title.textAlignment = NSTextAlignmentCenter;
    [self.headerView addSubview:title];
    
    // ✅ Reset button (refresh icon)
    self.resetButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.resetButton.translatesAutoresizingMaskIntoConstraints = NO;

    UIImage *refreshImage = [UIImage systemImageNamed:@"arrow.clockwise"];
    refreshImage = [refreshImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [self.resetButton setImage:refreshImage forState:UIControlStateNormal];
    self.resetButton.tintColor = [UIColor whiteColor];
    self.resetButton.backgroundColor = [UIColor clearColor];
    [self.resetButton addTarget:self
                         action:@selector(handleReset)
               forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.resetButton];
    
    // Status bar background (same color as header)
    UIView *statusBarBackground = [[UIView alloc] init];
    statusBarBackground.backgroundColor = self.headerView.backgroundColor;
    statusBarBackground.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:statusBarBackground];

    // Auto Layout
    UILayoutGuide *safe = self.view.safeAreaLayoutGuide;
    [NSLayoutConstraint activateConstraints:@[
        // Header
        [self.headerView.topAnchor constraintEqualToAnchor:safe.topAnchor],
        [self.headerView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.headerView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.headerView.heightAnchor constraintEqualToConstant:48],

        // Back button
        [backButton.leadingAnchor constraintEqualToAnchor:self.headerView.leadingAnchor constant:8],
        [backButton.centerYAnchor constraintEqualToAnchor:self.headerView.centerYAnchor],
        [backButton.widthAnchor constraintEqualToConstant:44],
        [backButton.heightAnchor constraintEqualToConstant:44],

        // Title centered
        [title.centerXAnchor constraintEqualToAnchor:self.headerView.centerXAnchor],
        [title.centerYAnchor constraintEqualToAnchor:self.headerView.centerYAnchor],

        // ✅ Reset button (right)
        [self.resetButton.trailingAnchor constraintEqualToAnchor:self.headerView.trailingAnchor constant:-8],
        [self.resetButton.centerYAnchor constraintEqualToAnchor:self.headerView.centerYAnchor],
        [self.resetButton.widthAnchor constraintEqualToConstant:44],
        [self.resetButton.heightAnchor constraintEqualToConstant:44],

        // Status bar color fill
        [statusBarBackground.topAnchor constraintEqualToAnchor:self.view.topAnchor],
        [statusBarBackground.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [statusBarBackground.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [statusBarBackground.bottomAnchor constraintEqualToAnchor:safe.topAnchor],
    ]];
}

- (void)createForm {
    self.scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollView];

    UILabel *locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 20)];
    locationLabel.text = @"*Location";
    locationLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.scrollView addSubview:locationLabel];

    self.locationField = [[UITextField alloc] initWithFrame:CGRectZero];
    self.locationField.borderStyle = UITextBorderStyleRoundedRect;
    [self.scrollView addSubview:self.locationField];

    UILabel *commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 110, 200, 20)];
    commentLabel.text = @"Comment";
    commentLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.scrollView addSubview:commentLabel];

    self.commentField = [[UITextView alloc] initWithFrame:CGRectZero];
    self.commentField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.commentField.layer.borderWidth = 1.0;
    self.commentField.layer.cornerRadius = 8;
    [self.scrollView addSubview:self.commentField];

    self.submitButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.submitButton.frame = CGRectZero;
    self.submitButton.backgroundColor = [UIColor colorWithRed:0/255.0 green:109/255.0 blue:141/255.0 alpha:1];
    [self.submitButton setTitle:@"Submit" forState:UIControlStateNormal];
    [self.submitButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.submitButton.layer.cornerRadius = 8;
    [self.submitButton addTarget:self action:@selector(handleSubmit) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.submitButton];
}

- (void)handleReset {
    NSLog(@"🔄 Reset button tapped");
    self.locationField.text = @"";
    self.commentField.text = @"";

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Fields Cleared"
                                                                   message:@"All inputs have been reset."
                                                            preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"OK"
                                              style:UIAlertActionStyleDefault
                                            handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}


#pragma mark - Actions

- (void)dismissSelf {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)handleSubmit {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Submitted"
                                                                   message:@"Issue has been added (mock)."
                                                            preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

@end
