//
//  SettingViewController.m
//  DSH
//
//  Created by 张毅成 on 2018/9/26.
//  Copyright © 2018 WZX. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingModel.h"
#import "MyDetailTableViewCell.h"
#import "LoginViewController.h"
#import "FeedbackViewController.h"
#import <SDWebImage/SDImageCache.h>
#import "AboutViewController.h"
//#import "JPUSHService.h"
#import "AccountAndSecurityViewController.h"
#import "BlacklistViewController.h"

@interface SettingViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *buttonLogOut;
@property (assign, nonatomic) BOOL haveCache;

@end

@implementation SettingViewController

- (void)Network {
    SHOWLOADING
    NSString *URL = @"my/logOut";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
//    [NetWorkRequest postJSONWithUrl:URL parameters:parameters success:^(id result) {
//        NSLog(@"%@",result);
//        REMOVESHOW
//        if ([result[@"code"] integerValue] == 0) {
//            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"nickname"];
//            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"headimgurl"];
//            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userId"];
//            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"LOGINTOKEN"];
//            [[NSUserDefaults standardUserDefaults] setBool:false forKey:@"isLogin"];
//            [[NSUserDefaults standardUserDefaults] synchronize];
//            [self.navigationController popToRootViewControllerAnimated:false];
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToLogin" object:nil];
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadBookCase" object:nil];
//            [JPUSHService setAlias:@"" completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {} seq:0];
//        }
//    } fail:^(NSError *error) {
//        REMOVESHOW
//    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    self.view.backgroundColor = kBackgroundColor;
    [self.buttonLogOut setCorner];
    [self.buttonLogOut addEventHandler:^(id sender) {
        [ZYCTool alertControllerTwoButtonWithTitle:@"确认退出?" message:@"" target:self notarizeButtonTitle:nil cancelButtonTitle:nil notarizeAction:^{
            [self Network];
        } cancelAction:^{}];
    } forControlEvents:(UIControlEventTouchUpInside)];
    [self createTableView];
}

- (void)createTableView {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 60;
    self.tableView.backgroundColor = kBackgroundColor;
    self.tableView.scrollEnabled = false;
}

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

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    view.backgroundColor = UIColorHex(0xF5F5F5);
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    view.backgroundColor = UIColorHex(0xF5F5F5);
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return SettingModel.arrayData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = SettingModel.arrayData[section];
    return array.count;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyDetailTableViewCell *cell = [MyDetailTableViewCell cellWithTableView:tableView];
    [cell setDataWithModel:SettingModel.arrayData[indexPath.section][indexPath.row]];
    cell.labelDetail.text = @"";
    if (indexPath.section == 1) {
        cell.labelDetail.text = [self checkCache];
        [cell.labelDetail setTextColor:[UIColor hexStringToColor:@"999999"]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {//账号与安全
        AccountAndSecurityViewController *controller = [AccountAndSecurityViewController new];
        controller.modelMy = self.modelMy;
        [self.navigationController pushViewController:controller animated:true];
    }else if (indexPath.section == 1) {//清理缓存
        if (self.haveCache) {
            [ZYCTool alertControllerTwoButtonWithTitle:@"是否清理缓存?" message:@"" target:self notarizeButtonTitle:nil cancelButtonTitle:nil notarizeAction:^{
                [[SDImageCache sharedImageCache] clearMemory];
                [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
                    
                }];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
            } cancelAction:^{}];
        }else
            SHOWSUCCESS(@"暂无缓存")
    }else if (indexPath.section == 2) {//黑名单
        BlacklistViewController *controller = [BlacklistViewController new];
        [self.navigationController pushViewController:controller animated:true];
    }else if (indexPath.section == 3) {
        if (indexPath.row == 0) {//意见反馈
            FeedbackViewController *controller = [FeedbackViewController new];
            controller.controllerType = FeedbackViewControllerTypeFeedback;
            controller.URL = @"my/sendSuggestion";
            [self.navigationController pushViewController:controller animated:true];
        }else{//关于我们
            AboutViewController *controller = [AboutViewController new];
            [self.navigationController pushViewController:controller animated:true];
        }
    }
}

- (NSString *)checkCache {
    float tmpSize = [[SDImageCache sharedImageCache] totalDiskSize];
    NSLog(@"%f",tmpSize);
    if (tmpSize > 0) {
        self.haveCache = true;
    }else
        self.haveCache = false;
    if (tmpSize >= 1024*1024*1024) {
        return [NSString stringWithFormat:@"(%0.2fG)",tmpSize /(1024.f*1024.f*1024.f)];
    }else if (tmpSize >= 1024*1024) {
        return [NSString stringWithFormat:@"%0.2fM",tmpSize /(1024.f*1024.f)];
    }else{
        return [NSString stringWithFormat:@"%0.2fK",tmpSize / 1024.f];
    }
}

@end