






#import "PickEnsureViewController.h"
#import "SubRingEyeConfig.h"
#import "NSObject+MixModel.h"
#import "UIColor+EyeColor.h"
#import "ItsToast.h"

@interface PickEnsureViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<NSDictionary *> *bigItalicsEndpointsHandleTension; 
@property (nonatomic, strong) NSArray<NSArray<NSString *> *> *pubSaltSnowRed; 
@property (nonatomic, strong) NSMutableArray<NSString *> *sectionTitles; 

@end

@implementation PickEnsureViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _bigItalicsEndpointsHandleTension = [NSMutableArray array];
    _pubSaltSnowRed = @[];
    _sectionTitles = [NSMutableArray array];
    
    [self greaterEyeView];
}

- (void)viewWillAppear:(BOOL)animated {
    
    UIEdgeInsets oneCursive = [[InlandDidManager shared] meanSecurelyWindow].safeAreaInsets;
    
    oneCursive.top    += 10;
    oneCursive.left   += 10;
    oneCursive.bottom += 10;
    oneCursive.right  += 10;

    [self.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(oneCursive);
    }];
}


- (void)greaterEyeView {
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.yahooFoldButton.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass(self.class)];
}


- (NSArray<NSString *> *)unwrapPromisedDictionary:(NSDictionary *)cell {
    return [[cell allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
}

- (void)knowAuthorityInfo:(NSDictionary *)info withTitle:(NSString *)title {
    if (!info || ![info isKindOfClass:[NSDictionary class]]) {
        return;
    }
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        @synchronized (self) {
            
            [self->_bigItalicsEndpointsHandleTension addObject:[info copy]];
            NSArray *bridgeDrop = [self unwrapPromisedDictionary:info];
            self->_pubSaltSnowRed = [self->_pubSaltSnowRed arrayByAddingObject:bridgeDrop];
            [self->_sectionTitles addObject:title];
            
            
            [self.tableView reloadData];
        }
    });
}


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _pubSaltSnowRed[section].count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return _sectionTitles[section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self.class) forIndexPath:indexPath];
    
    NSString *key;
    id value;
    NSInteger butUsedIndex = indexPath.section;
    key = _pubSaltSnowRed[butUsedIndex][indexPath.row];
    value = _bigItalicsEndpointsHandleTension[butUsedIndex][key];
    BOOL produced = [value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSArray class]];
    cell.backgroundColor = [UIColor clearColor];
    
    
    for (UIView *enhance in cell.contentView.subviews) {
        [enhance removeFromSuperview];
    }
    
    
    UILabel *askLabel = [[UILabel alloc] init];
    askLabel.font = [UIFont monospacedSystemFontOfSize:14 weight:UIFontWeightMedium];
    askLabel.textColor = [UIColor darkGrayColor];
    askLabel.text = key;
    askLabel.numberOfLines = 0;
    [cell.contentView addSubview:askLabel];
    
    
    UILabel *abortLabel = [[UILabel alloc] init];
    abortLabel.font = [UIFont monospacedSystemFontOfSize:14 weight:UIFontWeightRegular];
    abortLabel.textColor = [UIColor blackColor];
    abortLabel.numberOfLines = 0;
    abortLabel.textAlignment = NSTextAlignmentRight;
    [cell.contentView addSubview:abortLabel];
    
    
    [askLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(SafariFloor.wakeTrustDay.getTitleJump);
        make.top.equalTo(cell.contentView).offset(SafariFloor.wakeTrustDay.restEuropean);
        make.bottom.equalTo(cell.contentView).offset(-SafariFloor.wakeTrustDay.restEuropean);
        make.width.equalTo(cell.contentView.mas_width).multipliedBy(produced?SafariFloor.wakeTrustDay.senseKirghiz:SafariFloor.wakeTrustDay.planArtsMicro);
    }];
    
    [abortLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cell.contentView).offset(-SafariFloor.wakeTrustDay.getTitleJump);
        make.top.equalTo(cell.contentView).offset(SafariFloor.wakeTrustDay.restEuropean);
        make.bottom.equalTo(cell.contentView).offset(-SafariFloor.wakeTrustDay.restEuropean);
        make.left.equalTo(askLabel.mas_right).offset(SafariFloor.wakeTrustDay.restEuropean);
    }];
    
    
    if (produced) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else {
        abortLabel.text = [value description];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id value;
    NSString *key;
    
    NSInteger butUsedIndex = indexPath.section;
    key = _pubSaltSnowRed[butUsedIndex][indexPath.row];
    value = _bigItalicsEndpointsHandleTension[butUsedIndex][key];
    
    
    if ([value isKindOfClass:[NSDictionary class]]) {
        [self assignNineDictionary:value withTitle:key];
    } else if ([value isKindOfClass:[NSArray class]]) {
        [self threadSwapArray:value withTitle:key];
    } else {
        
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        [pasteboard setString:[value description]];
        [ItsToast tagalog:SafariFloor.wakeTrustDay.organizeIgnoreAlbumMenAnchoringBegin];
    }
}


- (void)assignNineDictionary:(NSDictionary *)cell withTitle:(NSString *)title {
    PickEnsureViewController *shortSob = [[PickEnsureViewController alloc] init];
    [self.navigationController pushViewController:shortSob animated:NO];
    [shortSob knowAuthorityInfo:cell withTitle:title];
}

- (void)threadSwapArray:(NSArray *)array withTitle:(NSString *)title {
    
    NSMutableDictionary *matchDict = [NSMutableDictionary dictionary];
    for (NSInteger i = 0; i < array.count; i++) {
        matchDict[[NSString stringWithFormat:@"[%ld]", (long)i]] = array[i];
    }
    
    PickEnsureViewController *shortSob = [[PickEnsureViewController alloc] init];
    [self.navigationController pushViewController:shortSob animated:NO];
    [shortSob knowAuthorityInfo:matchDict withTitle:[NSString stringWithFormat:@"%@ (Array)", title]];
}

@end
