//
//  CashTicketViewController.m
//  Project
//
//  Created by Vae on 2018/6/12.
//

#import "CashTicketViewController.h"
#import "UIToolkit.h"
#import "Vars.h"
#import "DrawUtils.h"
#import "UIConstants.h"
#import <BlocksKit/BlocksKit.h>
#import <BlocksKit/BlocksKit+UIKit.h>
#import <libextobjc/EXTScope.h>
#import "DZNEmptyDataSetDefault.h"
#import "BusStatusApi.h"
#import "CheckoutViewController.h"
#import "Schedule.h"
#import "BeanUtils.h"
#import "Utility.h"
#import "ScheduleAPI.h"
#import "CocoaUtils.h"
#import "BookingReportDetailResponseJsonMapper.h"
#import "IMLocalization.h"
@interface CashTicketViewController ()<DZNEmptyDataSetDefault>
@property (nonatomic, strong) NSArray *cellDataArray;
@end

@implementation CashTicketViewController


- (instancetype)init {
    if (self = [super init]) {
        [[Vars appDelegate] applyDefaultConfigurationForViewController:self];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self buildCellDataSource];
    self.title = self.service_date;
    self.tableView.delegate = self;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (!self.bookingReportSummaryList) {
        [self performRequestForBookingReportDetail];
    }
}

- (void)performRequestForBookingReportDetail {

    BookingReportDetail *bookingReportDetail = [[BookingReportDetail alloc] init];
    bookingReportDetail.departureDate = self.service_date;
    bookingReportDetail.scheduleId = self.schedule.id;     
    bookingReportDetail.isDBDSearch = self.isDBDBookingReport;
    
    NSDictionary *requestDict = [BeanUtils describeAccessibleSimple:bookingReportDetail];
    self.tableView.emptyDataSetSource = nil;
    [Utility performRESTRequestWithMethod:@"POST" url:bookingReportDetail.address parameters:requestDict success:^(id jsonObject) {
        //NSLog(@"%@", jsonObject);
        BookingReportDetailResponseJsonMapper *mapper = [[BookingReportDetailResponseJsonMapper alloc] initWithIsDBDSearch:self.isDBDBookingReport];        if (![mapper parseWithJSONObject:jsonObject]) {
            BookingReportDetailResponse *response = mapper.response;
            if (response.errorCode > 0) {
                [CocoaUtils showAlertWithTitle:IMLocalizedString(@"Error") message:response.errorMessage];
            } else {
                self.bookingReportSummaryList = response.bookingReportSummaryList;
                self.loadSuccess(response.bookingReportSummaryList);
                [self buildCellDataSource];
                self.tableView.emptyDataSetSource = self;
                [self.tableView reloadData];
            }
        }
    }];
}


- (void)buildCellDataSource {
    @weakify(self)
    NSMutableArray *arrays = [NSMutableArray array];
    
    for (BookingReportSummaryBean *bookingReportSummary in self.bookingReportSummaryList) {
        if (bookingReportSummary.defaultBookingReportDetail.departureScheduleStop.id == self.stopStationId) {
           {
            TableViewCellData *data = [[TableViewCellData alloc] init];
            data.image = [DrawUtils imageWithFAKIcon:[FAKFontAwesome locationArrowIconWithSize:24] fillColor:[UIColor lightGrayColor]];
            data.text = bookingReportSummary.defaultBookingReportDetail.departureScheduleStop.city.show_name;
            data.detailText = bookingReportSummary.defaultBookingReportDetail.departureScheduleStop.busstation.name;
            data.style = UITableViewCellStyleSubtitle;
            [arrays addObject:data];
           }
        for (BookingReportDetailBean *bookingReportDetail in bookingReportSummary.bookingReportDetailList) {

            TableViewCellData *data = [[TableViewCellData alloc] init];
            data.style = UITableViewCellStyleSubtitle;
            data.image = [DrawUtils imageWithFAKIcon:[FAKFontAwesome mapMarkerIconWithSize:24] fillColor:[UIColor lightGrayColor]];
            data.text = bookingReportDetail.arrivalScheduleStop.city.show_name;
            data.detailText = bookingReportDetail.arrivalScheduleStop.busstation.name;
            [data setAccessoryViewBlock:^UIView *(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
                return [[UIImageView alloc] initWithImage:[DrawUtils imageWithFAKIcon:[FAKFontAwesome moneyIconWithSize:24] fillColor:[Vars defaultTintColor]]];
            }];
            [data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
                @strongify(self)
                [self performSegueForCashTicketWithObject: bookingReportDetail];
            }];
            [arrays addObject:data];
            }
        }
    }
    self.cellDataArray = [NSArray arrayWithArray:arrays];
}


- (void)performSegueForCashTicketWithObject:(BookingReportDetailBean *)bookingReportDetail {
    CheckoutViewController *viewController = [[CheckoutViewController alloc] init];
    viewController.service_date = self.service_date;
    viewController.bookingReportDetail = bookingReportDetail;
    [self.navigationController pushViewController:viewController animated:YES];
}


#pragma mark - tableView DataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.cellDataArray.count;
}

#pragma mark - Tableview Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCellData *data = self.cellDataArray[indexPath.row];
    return  [UIToolkit tableView:tableView atIndexPath:indexPath rowHeightWithData:data];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCellData *data = self.cellDataArray[indexPath.row];
    return [UIToolkit tableView:tableView atIndexPath:indexPath cellWithData:data];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TableViewCellData *data = self.cellDataArray[indexPath.row];
    [UIToolkit tableView:tableView atIndexPath:indexPath didSelectWithData:data];
}

@end
