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

#import "ICBatteryTipDetailController.h"
#import "ICBatteryCareImageViewCell.h"
#import "ICBatteryCareSubTextViewCell.h"
#import "ICBatteryCareMainTextViewCell.h"

@interface ICBatteryTipDetailController ()<UITableViewDataSource, UITableViewDelegate>

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

@end

@implementation ICBatteryTipDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
//    self.navigationItem.title = @"电池寿命";//ICLocalized(@"ManagementContact");
    /*
     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
     */
    
    NSString *plistName = @"";
    switch (self.careType) {
        case battery_care_low_power:
            plistName = @"BatteryCareLowPowerMode";
            break;
        case battery_care_bluetooth:
            plistName = @"BatteryCareBluetooth";
            break;
        case battery_care_low_temperature:
            plistName = @"BatteryCareLowTemperature";
            break;
        case battery_care_location:
            plistName = @"BatteryCareLocation";
            break;
        case battery_care_notification:
            plistName = @"BatteryCareNotification";
            break;
        case battery_care_use_guide:
            plistName = @"BatteryCareUse";
            break;
        case battery_care_screen_refresh:
            plistName = @"BatteryCareUseBackgroundRefresh";
            break;
        case battery_care_lightbright:
            plistName = @"BatteryCareUseBrightness";
            break;
        case battery_care_wifi:
            plistName = @"BatteryCareUseWifi";
            break;
        default:
            break;
    }
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource: plistName ofType:@"plist"];
    NSMutableArray *dictArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
    NSLog(@"%@", dictArray);
    
    self.tipItems = dictArray;
    
    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 = 90;
    self.tableView.sectionFooterHeight = 0.01;
    self.tableView.sectionHeaderHeight = 0.01;
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerNib: [UINib nibWithNibName:@"ICBatteryCareImageViewCell" bundle:nil] forCellReuseIdentifier:@"ICBatteryCareImageViewCell"];
    [self.tableView registerNib: [UINib nibWithNibName:@"ICBatteryCareSubTextViewCell" bundle:nil] forCellReuseIdentifier:@"ICBatteryCareSubTextViewCell"];
    [self.tableView registerNib: [UINib nibWithNibName:@"ICBatteryCareMainTextViewCell" bundle:nil] forCellReuseIdentifier:@"ICBatteryCareMainTextViewCell"];
    
    [self.tableView reloadData];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tipItems.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dict = self.tipItems[indexPath.row];
    NSInteger type = [dict[@"type"] integerValue];
    
    switch (type) {
        case 2:
        {
            ICBatteryCareImageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ICBatteryCareImageViewCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.careImageView.image = [UIImage imageNamed: dict[@"image"]];
            return cell;
        }
            break;
        case 1:
        {
            ICBatteryCareMainTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ICBatteryCareMainTextViewCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            NSString *language = [[NSLocale preferredLanguages] firstObject];
            if ([language containsString: @"zh"]) {
                cell.mainTextLabel.text = dict[@"text"];
            } else {
                cell.mainTextLabel.text = dict[@"text_en"];
            }
            return cell;
        }
            break;
        default:
        {
            ICBatteryCareSubTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ICBatteryCareSubTextViewCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            NSString *language = [[NSLocale preferredLanguages] firstObject];
            if ([language containsString: @"zh"]) {
                [cell subText:dict[@"text"]];
//                cell.subTextLabel.text = dict[@"text"];
            } else {
                [cell subText:dict[@"text_en"]];
//                cell.subTextLabel.text = dict[@"text_en"];
            }
            return cell;
        }
            break;
    }
}

@end
