//
//  ICCalendarManageController.m
//  superCleaner
//
//  Created by Mazy on 2020/7/29.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "ICCalendarManageController.h"
#import "WMEventCalendarTool.h"
#import "ICContactManageViewCell.h"
#import "ICCalenderEventViewController.h"
#import "ICCalenderReminderController.h"

@interface ICCalendarManageController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
/// 联系人
@property (nonatomic, strong) NSMutableArray<EKEvent *> *events;
/// 提醒事项
@property (nonatomic, strong) NSMutableArray<EKReminder *> *reminders;

@end

@implementation ICCalendarManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = ICLocalized(@"Manage");
    self.tableView = [[UITableView alloc] initWithFrame: self.view.bounds style: UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 90;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    [self.tableView registerNib: [UINib nibWithNibName:@"ICContactManageViewCell" bundle:nil] forCellReuseIdentifier:@"ICContactManageViewCell"];
    
    [self fetchCalendarEvents];
    [self fetchCalendarReminders];
    
}

- (void)fetchCalendarEvents {
    [[WMEventCalendarTool sharedCalendar] checkCalendarCanUsedCompletion:^(BOOL granted, NSError *error) {
        if (granted) {
            NSArray *events = [[WMEventCalendarTool sharedCalendar] checkToStartDate:[NSDate dateWithTimeIntervalSince1970:1514375207] addEndDate: [NSDate date] addModifytitle:nil addCalendarIdentifier: nil];
            self.events = events.mutableCopy;
            [self.tableView reloadData];
            
//            for (int i = 0; i < events.count; i++) {
//                EKEvent *event = events[i];
//                NSLog(@"%@ - %@",event.title,event.eventIdentifier);
//            }
        } else {
            NSLog(@"需要打开权限");
        }
    }];
}

- (void)fetchCalendarReminders {
    
    [[WMEventCalendarTool sharedCalendar] checkCalendarReminderCanUsedCompletion:^(BOOL granted, NSError *error) {
        if (granted) {
            [[WMEventCalendarTool sharedCalendar] checkCompletedRemindersStartDate:[NSDate dateWithTimeIntervalSince1970:1514375207] addEndDate:[NSDate date] fetchCompleted:^(NSArray<EKReminder *> *reminders) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.reminders = reminders.mutableCopy;
                    [self.tableView reloadData];
//                    //                EKReminder *event = reminders[i];
//                    for (EKReminder *reminder in reminders) {
//                        NSLog(@"提醒事项 - %@ ",reminder.title);
//                    }
                });
            }];
        } else {
            NSLog(@"需要打开权限");
        }
    }];
}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:false animated:true];
    [self fetchCalendarEvents];
    [self fetchCalendarReminders];
}

# pragma UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 15;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ICContactManageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ICContactManageViewCell" forIndexPath:indexPath];
    switch (indexPath.row) {
        case 0:
        {
            cell.mainTitleLabel.text = ICLocalized(@"OverdueCalendar"); //@"过期日历";
            cell.countLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)self.events.count];
        }
            break;
        case 1:
        {
            cell.mainTitleLabel.text = ICLocalized(@"OverdueReminders"); //@"过期提醒事项";
            cell.countLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)self.reminders.count];
        }
            break;
            
        default:
            break;
    }
    cell.subTitleLabel.hidden = true;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
   
    if ([ICUserManager shared].isMember) {
        if (indexPath.row == 0) {
            ICCalenderEventViewController *calendarVC = [[ICCalenderEventViewController alloc] init];
            [self.navigationController pushViewController:calendarVC animated:true];
        } else {
            ICCalenderReminderController *calendarVC = [[ICCalenderReminderController alloc] init];
            [self.navigationController pushViewController:calendarVC animated:true];
        }
    } else {
        ICIAPViewController *iapVC = [[UIStoryboard storyboardWithName:@"Main" bundle: nil] instantiateViewControllerWithIdentifier:@"ICIAPViewController"];
        ICNavigationController *navc = [[ICNavigationController alloc] initWithRootViewController:iapVC];
        [self presentViewController:navc animated:true completion:nil];
    }
}


@end
