//
//  ICSettingTableViewController.m
//  superCleaner
//
//  Created by Mazy on 2020/7/5.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "ICSettingTableViewController.h"
#import "ICWebViewController.h"
#import "superCleaner-Swift.h"

@interface ICSettingTableViewController ()

@end

@implementation ICSettingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = ICLocalized(@"Setting");
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
}

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

#pragma mark - Table view data source

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    NSLog(@"%@", indexPath);
    
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                ICWebViewController *webView = [[ICWebViewController alloc] init];
                NSDictionary * dic = [ICUserManager shared].h5_url;
                NSString *language = [[NSLocale preferredLanguages] firstObject];
                NSString *FeedBackuUrl = [dic objectForKey:@"ProblemSolvingUrl"];
                
                if ([language containsString: @"zh"]) {
                    webView.url = [FeedBackuUrl stringByAppendingString:@"?language=chinese"];
                } else {
                    webView.url = [FeedBackuUrl stringByAppendingString:@"?language=english"];
                }
                webView.webTitle = ICLocalized(@"AskQuestion");
                [self.navigationController pushViewController:webView animated:true];
            }
                break;
            case 1:
            {
                ICWebViewController *webView = [[ICWebViewController alloc] init];
                NSDictionary * dic = [ICUserManager shared].h5_url;
                webView.url = [dic objectForKey:@"privacy"];
                webView.webTitle = ICLocalized(@"PrivacyPolicy");
                [self.navigationController pushViewController:webView animated:true];
            }
                break;
            case 2:
            {
                ICWebViewController *webView = [[ICWebViewController alloc] init];
                NSDictionary * dic = [ICUserManager shared].h5_url;
                webView.url = [dic objectForKey:@"user"];
                webView.webTitle = ICLocalized(@"TermsUse");
                [self.navigationController pushViewController:webView animated:true];
            }
                break;
            case 3:
            {
                ICWebViewController *webView = [[ICWebViewController alloc] init];
                NSString *FeedBackuUrl = [[ICUserManager shared].h5_url objectForKey:@"FeedBackuUrl"];
                
                NSString *token = [ICUserManager shared].token;
                NSString *uid = [ICUserManager shared].uid;
                
                NSString *language = [[NSLocale preferredLanguages] firstObject];
                if ([language containsString: @"zh"]) {
                    webView.url = [FeedBackuUrl stringByAppendingFormat:@"?language=chinese&token=%@&uid=%@", token, uid];;
                } else {
                    webView.url = [FeedBackuUrl stringByAppendingFormat:@"?language=english&token=%@&uid=%@", token, uid];;
                }
                webView.webTitle = ICLocalized(@"Feedback");
                [self.navigationController pushViewController:webView animated:true];
            }
                break;
            default:
                break;
        }
    } else if (indexPath.section == 1) {
        // 恢复购买
        [MobClick event:@"resume_pay"];
        [ICPurchaseManager restorePurchasesWithSuccess:^{
            [[ICUserManager shared] getUserInfo:^(ZTUserInfoModel * _Nonnull model) {
                [self.navigationController popViewControllerAnimated:true];
            } failure:^{
                [self.navigationController popViewControllerAnimated:true];
            }];
        } toView:self.view];
    }
}

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

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

@end
