//  AppointmentViewMoreViewController.mm

#import "AppointmentViewMoreViewController.h"

@interface AppointmentViewMoreViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *topBar;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *toggleTabButton;

@property (nonatomic, strong) UIView *filterBar;
@property (nonatomic, strong) UILabel *filterLabel;
@property (nonatomic, strong) UIButton *filterButton;

@property (nonatomic, strong) UIView *calendarContainer;
@property (nonatomic, strong) UIView *listingContainer;
@property (nonatomic, strong) UITableView *listingTable;

@property (nonatomic, strong) UIView *bottomButtonContainer;
@property (nonatomic, strong) UIButton *makeAppointmentButton;

@property (nonatomic, strong) UIView *filterOverlay;

@property (nonatomic, strong) UIActivityIndicatorView *loadingView;

@property (nonatomic, assign) BOOL showingCalendar;

@end

@implementation AppointmentViewMoreViewController

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

    self.showingCalendar = YES;

    [self setupTopBar];
    [self setupFilterBar];
    [self setupCalendarView];
    [self setupListingView];
    [self setupBottomButton];
    [self setupFilterOverlay];
    [self setupLoadingOverlay];
}

#pragma mark - Top Bar

- (void)setupTopBar {
    self.topBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 48)];
    self.topBar.backgroundColor = [UIColor colorWithRed:0 green:0.4 blue:0.55 alpha:1];
    [self.view addSubview:self.topBar];

    self.backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];
    [self.backButton setImage:[UIImage imageNamed:@"back24"] forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside];
    [self.topBar addSubview:self.backButton];

    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(48, 0,
        self.view.bounds.size.width - 96, 48)];
    self.titleLabel.text = @"Appointment";
    self.titleLabel.textColor = UIColor.whiteColor;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.topBar addSubview:self.titleLabel];

    self.toggleTabButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 48, 0, 48, 48)];
    [self.toggleTabButton setImage:[UIImage imageNamed:@"listing_icon_white"] forState:UIControlStateNormal];
    [self.toggleTabButton addTarget:self action:@selector(onToggleTab) forControlEvents:UIControlEventTouchUpInside];
    [self.topBar addSubview:self.toggleTabButton];
}

#pragma mark - Filter Bar

- (void)setupFilterBar {
    self.filterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 48, self.view.bounds.size.width, 48)];
    self.filterBar.backgroundColor = [UIColor colorWithWhite:0.6 alpha:1];
    [self.view addSubview:self.filterBar];

    self.filterLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, self.view.bounds.size.width - 64, 48)];
    self.filterLabel.text = @"Filter by All (Upcoming)";
    self.filterLabel.textColor = UIColor.whiteColor;
    [self.filterBar addSubview:self.filterLabel];

    self.filterButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 48, 0, 48, 48)];
    [self.filterButton setImage:[UIImage imageNamed:@"filter_icon"] forState:UIControlStateNormal];
    [self.filterButton addTarget:self action:@selector(onFilterButton) forControlEvents:UIControlEventTouchUpInside];
    [self.filterBar addSubview:self.filterButton];
}

#pragma mark - Calendar (placeholder container)

- (void)setupCalendarView {
    CGFloat top = CGRectGetMaxY(self.filterBar.frame);
    self.calendarContainer = [[UIView alloc] initWithFrame:CGRectMake(0, top,
                                                                      self.view.bounds.size.width,
                                                                      self.view.bounds.size.height - top - 62)];
    self.calendarContainer.backgroundColor = UIColor.whiteColor;

    UILabel *placeholder = [[UILabel alloc] initWithFrame:self.calendarContainer.bounds];
    placeholder.text = @"CALENDAR VIEW PLACEHOLDER";
    placeholder.textAlignment = NSTextAlignmentCenter;
    placeholder.textColor = UIColor.darkGrayColor;
    [self.calendarContainer addSubview:placeholder];

    [self.view addSubview:self.calendarContainer];
}

#pragma mark - Listing Table

- (void)setupListingView {
    CGFloat top = CGRectGetMaxY(self.filterBar.frame);

    self.listingContainer = [[UIView alloc] initWithFrame:CGRectMake(0, top,
                                                                     self.view.bounds.size.width,
                                                                     self.view.bounds.size.height - top - 62)];
    self.listingContainer.backgroundColor = UIColor.whiteColor;
    self.listingContainer.hidden = YES;

    self.listingTable = [[UITableView alloc] initWithFrame:self.listingContainer.bounds style:UITableViewStyleGrouped];
    self.listingTable.delegate = self;
    self.listingTable.dataSource = self;
    [self.listingContainer addSubview:self.listingTable];

    [self.view addSubview:self.listingContainer];
}

#pragma mark - Bottom Button

- (void)setupBottomButton {
    CGFloat h = 62;
    self.bottomButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - h, self.view.bounds.size.width, h)];
    self.bottomButtonContainer.backgroundColor = [UIColor colorWithRed:0 green:0.4 blue:0.55 alpha:1];
    [self.view addSubview:self.bottomButtonContainer];

    self.makeAppointmentButton = [[UIButton alloc] initWithFrame:self.bottomButtonContainer.bounds];
    [self.makeAppointmentButton setTitle:@"Make Appointment" forState:UIControlStateNormal];
    [self.makeAppointmentButton addTarget:self action:@selector(onMakeAppointment) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomButtonContainer addSubview:self.makeAppointmentButton];
}

#pragma mark - Filter Overlay

- (void)setupFilterOverlay {
    self.filterOverlay = [[UIView alloc] initWithFrame:self.view.bounds];
    self.filterOverlay.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
    self.filterOverlay.hidden = YES;

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 40)];
    label.text = @"FILTER OPTIONS PLACEHOLDER";
    label.textAlignment = NSTextAlignmentCenter;

    [self.filterOverlay addSubview:label];
    [self.view addSubview:self.filterOverlay];
}

#pragma mark - Loading Overlay

- (void)setupLoadingOverlay {
    self.loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleLarge];
    self.loadingView.center = self.view.center;
    self.loadingView.hidesWhenStopped = YES;
    [self.view addSubview:self.loadingView];
}

#pragma mark - Actions

- (void)onBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onToggleTab {
    self.showingCalendar = !self.showingCalendar;
    self.calendarContainer.hidden = !self.showingCalendar;
    self.listingContainer.hidden = self.showingCalendar;
}

- (void)onFilterButton {
    self.filterOverlay.hidden = !self.filterOverlay.hidden;
}

- (void)onMakeAppointment {
    NSLog(@"Make appointment tapped");
}

#pragma mark - TableView placeholder methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3; // placeholder
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2; // placeholder
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"listingCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];

    cell.textLabel.text = @"Unit No";
    cell.detailTextLabel.text = @"Owner Name";

    return cell;
}

@end

