//  AppointmentConfirmationViewController.mm

#import "AppointmentConfirmationViewController.h"
#import <WebKit/WebKit.h>

@interface AppointmentConfirmationViewController ()

// Top Bar
@property (nonatomic, strong) UIView *navBar;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *closeButton;

// Scroll
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;

// Summary Section
@property (nonatomic, strong) UILabel *unitLabel;
@property (nonatomic, strong) UIView *stepBanner;
@property (nonatomic, strong) UIView *summaryCard;
@property (nonatomic, strong) UILabel *appointmentTypeLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *timeLabel;

// Representative Section
@property (nonatomic, strong) UIView *representativeSection;
@property (nonatomic, strong) UILabel *repTitleLabel;
@property (nonatomic, strong) UIButton *repToggleButton;

// T&C
@property (nonatomic, strong) UILabel *tcTitleLabel;
@property (nonatomic, strong) WKWebView *htmlView;

// Bottom Buttons
@property (nonatomic, strong) UIView *bottomBar;
@property (nonatomic, strong) UIButton *editButton;
@property (nonatomic, strong) UIButton *confirmButton;

// Modal Placeholder
@property (nonatomic, strong) UIView *dimOverlay;

@end


@implementation AppointmentConfirmationViewController

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

    [self setupNavBar];
    [self setupScrollView];
    [self setupContentUI];
    [self setupBottomBar];
}

#pragma mark - Navigation Bar
- (void)setupNavBar {
    self.navBar = [[UIView alloc] init];
    self.navBar.backgroundColor = [UIColor colorWithRed:0 green:0.45 blue:0.55 alpha:1];
    self.navBar.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.navBar];

    // Title
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"Make Appointment";
    self.titleLabel.textColor = UIColor.whiteColor;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;

    [self.navBar addSubview:self.titleLabel];

    // Close Button
    self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.closeButton setImage:[UIImage imageNamed:@"icon_white_cancel"] forState:UIControlStateNormal];
    self.closeButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.navBar addSubview:self.closeButton];

    // Constraints
    [NSLayoutConstraint activateConstraints:@[
        [self.navBar.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
        [self.navBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.navBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.navBar.heightAnchor constraintEqualToConstant:48],

        [self.titleLabel.centerXAnchor constraintEqualToAnchor:self.navBar.centerXAnchor],
        [self.titleLabel.centerYAnchor constraintEqualToAnchor:self.navBar.centerYAnchor],

        [self.closeButton.leadingAnchor constraintEqualToAnchor:self.navBar.leadingAnchor constant:12],
        [self.closeButton.centerYAnchor constraintEqualToAnchor:self.navBar.centerYAnchor],
        [self.closeButton.widthAnchor constraintEqualToConstant:30],
        [self.closeButton.heightAnchor constraintEqualToConstant:30],
    ]];
}

#pragma mark - ScrollView
- (void)setupScrollView {
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.scrollView];

    self.contentView = [[UIView alloc] init];
    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.scrollView addSubview:self.contentView];

    [NSLayoutConstraint activateConstraints:@[
        [self.scrollView.topAnchor constraintEqualToAnchor:self.navBar.bottomAnchor],
        [self.scrollView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.scrollView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.scrollView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],

        [self.contentView.topAnchor constraintEqualToAnchor:self.scrollView.topAnchor],
        [self.contentView.leadingAnchor constraintEqualToAnchor:self.scrollView.leadingAnchor],
        [self.contentView.trailingAnchor constraintEqualToAnchor:self.scrollView.trailingAnchor],
        [self.contentView.bottomAnchor constraintEqualToAnchor:self.scrollView.bottomAnchor],
        [self.contentView.widthAnchor constraintEqualToAnchor:self.scrollView.widthAnchor],
    ]];
}

#pragma mark - Main UI
- (void)setupContentUI {

    // Unit Label
    self.unitLabel = [[UILabel alloc] init];
    self.unitLabel.text = @"ProjectName, Unit 12-34";
    self.unitLabel.font = [UIFont boldSystemFontOfSize:20];
    self.unitLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.unitLabel];

    // Step Banner
    self.stepBanner = [[UIView alloc] init];
    self.stepBanner.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    self.stepBanner.layer.cornerRadius = 12;
    self.stepBanner.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.stepBanner];

    UILabel *stepLabel = [[UILabel alloc] init];
    stepLabel.text = @"Step 3: Appointment Summary";
    stepLabel.font = [UIFont systemFontOfSize:15];
    stepLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.stepBanner addSubview:stepLabel];

    // Summary Card
    self.summaryCard = [[UIView alloc] init];
    self.summaryCard.backgroundColor = [UIColor colorWithRed:0 green:0.23 blue:0.30 alpha:1];
    self.summaryCard.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.summaryCard];

    self.appointmentTypeLabel = [[UILabel alloc] init];
    self.appointmentTypeLabel.text = @"Inspection";
    self.appointmentTypeLabel.textColor = UIColor.whiteColor;
    self.appointmentTypeLabel.font = [UIFont boldSystemFontOfSize:22];
    self.appointmentTypeLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.summaryCard addSubview:self.appointmentTypeLabel];

    self.dateLabel = [[UILabel alloc] init];
    self.dateLabel.text = @"20 Jan 2025";
    self.dateLabel.textColor = UIColor.whiteColor;
    self.dateLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.summaryCard addSubview:self.dateLabel];

    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.text = @"09:00 AM";
    self.timeLabel.textColor = UIColor.whiteColor;
    self.timeLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.summaryCard addSubview:self.timeLabel];

    // Representative Section
    self.representativeSection = [[UIView alloc] init];
    self.representativeSection.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.representativeSection];

    self.repTitleLabel = [[UILabel alloc] init];
    self.repTitleLabel.text = @"Representative Attending?";
    self.repTitleLabel.font = [UIFont boldSystemFontOfSize:17];
    self.repTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;

    self.repToggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.repToggleButton.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    self.repToggleButton.layer.cornerRadius = 14;
    self.repToggleButton.translatesAutoresizingMaskIntoConstraints = NO;

    [self.representativeSection addSubview:self.repTitleLabel];
    [self.representativeSection addSubview:self.repToggleButton];

    // Terms & Conditions
    self.tcTitleLabel = [[UILabel alloc] init];
    self.tcTitleLabel.text = @"Terms & Conditions";
    self.tcTitleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.tcTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.tcTitleLabel];

    self.htmlView = [[WKWebView alloc] init];
    self.htmlView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.htmlView];

    // Layout
    [NSLayoutConstraint activateConstraints:@[
        [self.unitLabel.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:20],
        [self.unitLabel.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:20],

        [self.stepBanner.topAnchor constraintEqualToAnchor:self.unitLabel.bottomAnchor constant:20],
        [self.stepBanner.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:16],
        [self.stepBanner.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-16],
        [self.stepBanner.heightAnchor constraintEqualToConstant:50],

        [stepLabel.centerYAnchor constraintEqualToAnchor:self.stepBanner.centerYAnchor],
        [stepLabel.leadingAnchor constraintEqualToAnchor:self.stepBanner.leadingAnchor constant:12],

        // Summary Card
        [self.summaryCard.topAnchor constraintEqualToAnchor:self.stepBanner.bottomAnchor constant:20],
        [self.summaryCard.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor],
        [self.summaryCard.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor],

        [self.appointmentTypeLabel.topAnchor constraintEqualToAnchor:self.summaryCard.topAnchor constant:20],
        [self.appointmentTypeLabel.leadingAnchor constraintEqualToAnchor:self.summaryCard.leadingAnchor constant:20],

        [self.dateLabel.topAnchor constraintEqualToAnchor:self.appointmentTypeLabel.bottomAnchor constant:16],
        [self.dateLabel.leadingAnchor constraintEqualToAnchor:self.summaryCard.leadingAnchor constant:20],

        [self.timeLabel.topAnchor constraintEqualToAnchor:self.dateLabel.bottomAnchor constant:8],
        [self.timeLabel.leadingAnchor constraintEqualToAnchor:self.summaryCard.leadingAnchor constant:20],
        [self.timeLabel.bottomAnchor constraintEqualToAnchor:self.summaryCard.bottomAnchor constant:-20],

        // Representative
        [self.representativeSection.topAnchor constraintEqualToAnchor:self.summaryCard.bottomAnchor constant:30],
        [self.representativeSection.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:20],
        [self.representativeSection.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-20],
        [self.representativeSection.heightAnchor constraintEqualToConstant:50],

        [self.repTitleLabel.centerYAnchor constraintEqualToAnchor:self.representativeSection.centerYAnchor],
        [self.repTitleLabel.leadingAnchor constraintEqualToAnchor:self.representativeSection.leadingAnchor],

        [self.repToggleButton.centerYAnchor constraintEqualToAnchor:self.representativeSection.centerYAnchor],
        [self.repToggleButton.trailingAnchor constraintEqualToAnchor:self.representativeSection.trailingAnchor],
        [self.repToggleButton.widthAnchor constraintEqualToConstant:28],
        [self.repToggleButton.heightAnchor constraintEqualToConstant:28],

        // Terms & Conditions
        [self.tcTitleLabel.topAnchor constraintEqualToAnchor:self.representativeSection.bottomAnchor constant:30],
        [self.tcTitleLabel.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:20],

        [self.htmlView.topAnchor constraintEqualToAnchor:self.tcTitleLabel.bottomAnchor constant:12],
        [self.htmlView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:20],
        [self.htmlView.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-20],
        [self.htmlView.heightAnchor constraintEqualToConstant:200],

        [self.htmlView.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:-40],
    ]];
}

#pragma mark - Bottom Bar
- (void)setupBottomBar {
    self.bottomBar = [[UIView alloc] init];
    self.bottomBar.backgroundColor = UIColor.whiteColor;
    self.bottomBar.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.bottomBar];

    self.editButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.editButton setTitle:@"Edit" forState:UIControlStateNormal];
    self.editButton.layer.borderColor = [UIColor colorWithRed:0 green:0.45 blue:0.55 alpha:1].CGColor;
    self.editButton.layer.borderWidth = 1;
    self.editButton.layer.cornerRadius = 20;
    self.editButton.translatesAutoresizingMaskIntoConstraints = NO;

    self.confirmButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.confirmButton setTitle:@"Confirm" forState:UIControlStateNormal];
    self.confirmButton.backgroundColor = [UIColor colorWithRed:0 green:0.45 blue:0.55 alpha:1];
    [self.confirmButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.confirmButton.layer.cornerRadius = 20;
    self.confirmButton.translatesAutoresizingMaskIntoConstraints = NO;

    [self.bottomBar addSubview:self.editButton];
    [self.bottomBar addSubview:self.confirmButton];

    [NSLayoutConstraint activateConstraints:@[
        [self.bottomBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.bottomBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.bottomBar.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor],
        [self.bottomBar.heightAnchor constraintEqualToConstant:70],

        [self.editButton.leadingAnchor constraintEqualToAnchor:self.bottomBar.leadingAnchor constant:20],
        [self.editButton.centerYAnchor constraintEqualToAnchor:self.bottomBar.centerYAnchor],
        [self.editButton.widthAnchor constraintEqualToAnchor:self.bottomBar.widthAnchor multiplier:0.45],
        [self.editButton.heightAnchor constraintEqualToConstant:48],

        [self.confirmButton.trailingAnchor constraintEqualToAnchor:self.bottomBar.trailingAnchor constant:-20],
        [self.confirmButton.centerYAnchor constraintEqualToAnchor:self.bottomBar.centerYAnchor],
        [self.confirmButton.widthAnchor constraintEqualToAnchor:self.bottomBar.widthAnchor multiplier:0.45],
        [self.confirmButton.heightAnchor constraintEqualToConstant:48],
    ]];
}

@end

