//
//  ICBatteryCareViewController.m
//  superCleaner
//
//  Created by Mazy on 2020/7/31.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "ICBatteryCareViewController.h"
#import "ICBatteryCareListViewCell.h"
#import "ICBatteryCareListHeaderViewCell.h"
#import "ICBatteryTipDetailController.h"

@interface ICBatteryCareViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *tipItems;

@end

@implementation ICBatteryCareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = ICLocalized(@"BatteryLife");
    
    self.tipItems = [NSMutableArray array];
    [self.tipItems addObject:@[@"battery_care_low_power", ICLocalized(@"LowPowerMode"), ICLocalized(@"SaveEnergy")]];
    [self.tipItems addObject:@[@"battery_care_bluetooth", ICLocalized(@"ManagingConnections"), ICLocalized(@"TurnOffConnections")]];
    [self.tipItems addObject:@[@"battery_care_low_temperature",  ICLocalized(@"Overheating"),  ICLocalized(@"KeepingYourIPhoneCool")]];
    [self.tipItems addObject:@[@"battery_care_location", ICLocalized(@"LocationServices"), ICLocalized(@"LimitYourLocation")]];
    [self.tipItems addObject:@[@"battery_care_notification", ICLocalized(@"LimitNotifications"), ICLocalized(@"TurnOffScreenAlerts")]];
    [self.tipItems addObject:@[@"battery_care_use_guide", ICLocalized(@"BatteryUsage"), ICLocalized(@"KeepChargeControl")]];
    [self.tipItems addObject:@[@"battery_care_screen_refresh", ICLocalized(@"BackgroundRefresh"), ICLocalized(@"SwitchOffRefreshing")]];
    [self.tipItems addObject:@[@"battery_care_lightbright", ICLocalized(@"Brightness"), ICLocalized(@"DimmingScreen")]];
    [self.tipItems addObject:@[@"battery_care_wifi",ICLocalized(@"Wi-FiRefresh"), ICLocalized(@"TurnOffBackgroundServices")]];
    
    self.tableView = [[UITableView alloc] initWithFrame: self.view.bounds style: UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 90;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerNib: [UINib nibWithNibName:@"ICBatteryCareListViewCell" bundle:nil] forCellReuseIdentifier:@"ICBatteryCareListViewCell"];
    [self.tableView registerNib: [UINib nibWithNibName:@"ICBatteryCareListHeaderViewCell" bundle:nil] forCellReuseIdentifier:@"ICBatteryCareListHeaderViewCell"];
    [self.tableView reloadData];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 1;
            break;
        default:
            return self.tipItems.count;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            return 178;
            break;
        default:
            return UITableViewAutomaticDimension;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ICBatteryCareListHeaderViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ICBatteryCareListHeaderViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    ICBatteryCareListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ICBatteryCareListViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *arr = self.tipItems[indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:arr.firstObject];
    cell.mainTitleLabel.text = arr[1];
    cell.descLabel.text = arr[2];
    return cell;
}

/*
 battery_care_low_power,
 battery_care_bluetooth,
 battery_care_low_temperature,
 battery_care_location,
 battery_care_notification,
 battery_care_use_guide,
 battery_care_screen_refresh,
 battery_care_lightbright,
 battery_care_wifi
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    ICBatteryTipDetailController *detailVC = [[ICBatteryTipDetailController alloc] init];
    
    NSArray *arr = self.tipItems[indexPath.row];
    detailVC.title = arr[1];
    
    switch (indexPath.row) {
        case 0:
            detailVC.careType = battery_care_low_power;
            break;
        case 1:
            detailVC.careType = battery_care_bluetooth;
            break;
        case 2:
            detailVC.careType = battery_care_low_temperature;
            break;
        case 3:
            detailVC.careType = battery_care_location;
            break;
        case 4:
            detailVC.careType = battery_care_notification;
            break;
        case 5:
            detailVC.careType = battery_care_use_guide;
            break;
        case 6:
            detailVC.careType = battery_care_screen_refresh;
            break;
        case 7:
            detailVC.careType = battery_care_lightbright;
            break;
        case 8:
            detailVC.careType = battery_care_wifi;
            break;
        default:
            break;
    }
    [self.navigationController pushViewController:detailVC animated:true];
}

@end
