#import "VendorDatePickController.h"
#import "Lang.h"
#import "SDK.h"
#import "CalendarManager.h"
#import "CocoaUtils.h"
#import "UIConstants.h"
#import <BlocksKit/BlocksKit.h>
#import <BlocksKit/A2DynamicDelegate.h>
#import <BlocksKit/BlocksKit+UIKit.h>
#import <libextobjc/EXTScope.h>
#import <PureLayout/PureLayout.h>
#import "Utility.h"
#import "ProductPickViewController.h"
#import "BookingReportScheduleListViewController.h"
#import "Vars.h"

@interface VendorDatePickController ()
@property (nonatomic, strong) FSCalendar *calendar;
@property (nonatomic, strong) CalendarManager *calendarManager;
@property(nonatomic, strong) UIButton *oldBookingReportButton;
@property(nonatomic, strong) UIButton *dbdBookingReportButton;
@property(nonatomic, strong) UIButton *sbdBookingReportButton;
@end

@implementation VendorDatePickController

- (instancetype)init {
    if (self = [super init]) {
        // Custom initialization
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.title = IMLocalizedString(@"Departure Date");
        [[Vars appDelegate] applyDefaultConfigurationForViewController:self];
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.calendarManager = [[CalendarManager alloc] init];
    self.calendarManager.minimumDate = [DateUtils addMonths:[NSDate date] amount:-6];
    self.calendarManager.maximumDate = [DateUtils addMonths:[NSDate date] amount:6];
    
    self.calendar = [[FSCalendar alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.calendar.backgroundColor = [UIColor whiteColor];
    self.calendar.locale = [CocoaUtils applicationLocale];
    self.calendar.pagingEnabled = YES;
    self.calendar.dataSource = self.calendarManager;
    self.calendar.delegate = self.calendarManager;
    [self.view addSubview:self.calendar];
    [self.calendar autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [self.calendar autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [self.calendar autoAlignAxisToSuperviewAxis:ALAxisVertical];
    
    self.oldBookingReportButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.oldBookingReportButton.backgroundColor = [Vars defaultTintColor];
    self.oldBookingReportButton.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont buttonFontSize]];
    [self.oldBookingReportButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if ([Vars appDelegate].loginUser.enable_destination_based_booking_report) {
        [self.oldBookingReportButton setTitle:@"Product Booking Report" forState:UIControlStateNormal];
    } else {
        [self.oldBookingReportButton setTitle:@"Booking Report" forState:UIControlStateNormal];
    }
    NSLayoutConstraint *oldBookingButtonHeightConstraint = [NSLayoutConstraint constraintWithItem: self.oldBookingReportButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIConstants defaultTableViewRowHeight]];
    oldBookingButtonHeightConstraint.active = YES;
    oldBookingButtonHeightConstraint.priority = UILayoutPriorityDefaultHigh;
    [self.oldBookingReportButton bk_addEventHandler:^(id sender) {
        if (![Vars appDelegate].loginUser.products) {
            [[Vars appDelegate] prefetchLoginPostData:^{
                [self performSegueForOldBookingReport];
            }];
        } else {
            [self performSegueForOldBookingReport];
        }
    } forControlEvents:UIControlEventTouchUpInside];
    [Utility addRoundedBorderToButton:self.oldBookingReportButton];
    [self.view addSubview:self.oldBookingReportButton];
    
    self.dbdBookingReportButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.dbdBookingReportButton.backgroundColor = [Vars defaultTintColor];
    self.dbdBookingReportButton.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont buttonFontSize]];
    [self.dbdBookingReportButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.dbdBookingReportButton setTitle:@"Destination Based Booking Report" forState:UIControlStateNormal];
    [self.dbdBookingReportButton addTarget:self action:@selector(performSegueForNewBookingReport:) forControlEvents:UIControlEventTouchUpInside];
    NSLayoutConstraint *dbdBookingReportButtonHeightConstraint = [NSLayoutConstraint constraintWithItem: self.dbdBookingReportButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIConstants defaultTableViewRowHeight]];
    dbdBookingReportButtonHeightConstraint.active = YES;
    dbdBookingReportButtonHeightConstraint.priority = UILayoutPriorityDefaultHigh;
    [Utility addRoundedBorderToButton:self.dbdBookingReportButton];
    [self.view addSubview:self.dbdBookingReportButton];
    
    self.sbdBookingReportButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.sbdBookingReportButton.backgroundColor = [Vars defaultTintColor];
    self.sbdBookingReportButton.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont buttonFontSize]];
    [self.sbdBookingReportButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.sbdBookingReportButton setTitle:@"Stop Based Booking Report" forState:UIControlStateNormal];
    [self.sbdBookingReportButton addTarget:self action:@selector(performSegueForNewBookingReport:) forControlEvents:UIControlEventTouchUpInside];
    NSLayoutConstraint *sbdBookingReportButtonHeightConstraint = [NSLayoutConstraint constraintWithItem: self.sbdBookingReportButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIConstants defaultTableViewRowHeight]];
    sbdBookingReportButtonHeightConstraint.active = YES;
    sbdBookingReportButtonHeightConstraint.priority = UILayoutPriorityDefaultHigh;
    [Utility addRoundedBorderToButton:self.sbdBookingReportButton];
    [self.view addSubview:self.sbdBookingReportButton];
    
    UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.oldBookingReportButton,self.dbdBookingReportButton,self.sbdBookingReportButton]];
    stackView.axis = UILayoutConstraintAxisVertical;
    stackView.distribution = UIStackViewDistributionFill;
    stackView.alignment = UIStackViewAlignmentFill;
    stackView.spacing = 15;
    [self.view addSubview:stackView];
    [stackView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.calendar withOffset:15];
    [stackView autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:15];
    [stackView autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:15];
    [stackView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:15];
    
    if ([Vars appDelegate].loginUser.id == 485) {
        return;
    }else if ([Vars appDelegate].loginUser.enable_stop_based_booking_report){
        self.dbdBookingReportButton.hidden = YES;
        self.oldBookingReportButton.hidden = YES;
        [self.sbdBookingReportButton setTitle:@"Booking Report" forState:UIControlStateNormal];
    }else if ([Vars appDelegate].loginUser.enable_destination_based_booking_report){
        self.sbdBookingReportButton.hidden = YES;
        self.oldBookingReportButton.hidden = YES;
        [self.dbdBookingReportButton setTitle:@"Booking Report" forState:UIControlStateNormal];
    }else {
        self.dbdBookingReportButton.hidden = YES;
        self.sbdBookingReportButton.hidden = YES;
    }
    
    [CalendarManager calendar:self.calendar selectDate:[NSDate date]];
}

#pragma mark - Segue

- (void)performSegueForOldBookingReport {
    if (![Vars appDelegate].loginUser.products) {
        return;
    }
    ProductPickViewController *viewController = [[ProductPickViewController alloc] init];
    viewController.service_date = [[DateFormatUtils ISO_DATE_FORMAT] format:self.calendar.selectedDate];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)performSegueForNewBookingReport:(id)sender{
    BookingReportScheduleListViewController *viewController = [[BookingReportScheduleListViewController alloc] init];
    UIButton *button = (UIButton *)sender;
    if (button == self.dbdBookingReportButton) {
        viewController.isDBDBookingReport = YES;
    }
    viewController.service_date = [[DateFormatUtils ISO_DATE_FORMAT] format:self.calendar.selectedDate];
    [self.navigationController pushViewController:viewController animated:YES];
}

@end
