//
//  MyPaperViewController.m
//  ChinaTheoryNetwork
//
//  Created by 张毅成 on 2019/4/3.
//  Copyright © 2019 张毅成. All rights reserved.
//

#import "MyPaperViewController.h"
#import "MyPaperTableViewCell.h"

@interface MyPaperViewController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *viewLine;
@property (weak, nonatomic) IBOutlet UIView *viewBackground;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *arrayButtonTop;
@property (weak, nonatomic) IBOutlet UIView *viewTop;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTopToTop;
@property (strong, nonatomic) UIScrollView *scrollViewBackground;

/**
 tableview数组
 */
@property (strong, nonatomic) NSMutableArray *arrayTableView;

/**
 正常显示的数据的数组
 */
@property (strong, nonatomic) NSMutableArray *arrayData;

/**
 分页数组
 */
@property (strong, nonatomic) NSMutableArray *arrayPageNo;
@end

@implementation MyPaperViewController

- (NSMutableArray *)arrayData {
    if (!_arrayData) {
        _arrayData = @[
                       @[@"",@"",@"",@""],
                       @[@"",@"",@""],
                       @[@"",@""]
                       ].mutableCopy;
    }
    return _arrayData;
}

- (NSMutableArray *)arrayTableView {
    if (!_arrayTableView) {
        _arrayTableView = @[].mutableCopy;
    }
    return _arrayTableView;
}

- (NSMutableArray *)arrayPageNo {
    if (!_arrayPageNo) {
        _arrayPageNo = @[].mutableCopy;
    }
    return _arrayPageNo;
}

- (UIScrollView *)scrollViewBackground {
    if (!_scrollViewBackground) {
        _scrollViewBackground = [[UIScrollView alloc] initWithFrame:(CGRect){0, 0, kGXScreenWidth, self.viewBackground.height}];
        _scrollViewBackground.delegate = self;
        _scrollViewBackground.pagingEnabled = true;
        _scrollViewBackground.userInteractionEnabled = true;
    }
    return _scrollViewBackground;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的论文";
    [self.viewBackground addSubview:self.scrollViewBackground];
    [self makeTableView];
}

- (void)makeTableView {
    [self.arrayButtonTop enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = obj;
        button.tag = idx;
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:(CGRect){ idx * kGXScreenWidth,0,kGXScreenWidth, self.viewBackground.height} style:(UITableViewStylePlain)];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        tableView.tag = idx;
        tableView.tableFooterView = [UIView new];
        WeakSelf(self)
        tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            StrongSelf(weakself)
            [strongself.arrayData replaceObjectAtIndex:tableView.tag withObject:@[].mutableCopy];
            [strongself.arrayPageNo replaceObjectAtIndex:tableView.tag withObject:@(1)];
        }];
        tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
            StrongSelf(weakself)
            NSInteger number = [strongself.arrayPageNo[tableView.tag] integerValue];
            number++;
            [strongself.arrayPageNo replaceObjectAtIndex:tableView.tag withObject:@(number)];
        }];
        [self.scrollViewBackground addSubview:tableView];
        [self.arrayTableView addObject:tableView];
        [self.arrayPageNo addObject:@(1)];
        [self.arrayData addObject:@[].mutableCopy];
    }];
    self.scrollViewBackground.contentSize = CGSizeMake(kGXScreenWidth * self.arrayTableView.count, 0);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.scrollViewBackground) {
        UIButton *button;
        [self.arrayButtonTop enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *button = obj;
            button.userInteractionEnabled = false;
        }];
        NSString *str = [NSString stringWithFormat:@"%0.0f",self.scrollViewBackground.contentOffset.x/kGXScreenWidth];
        if (self.scrollViewBackground.contentOffset.x == kGXScreenWidth * str.integerValue) {
            button = self.arrayButtonTop[str.integerValue];
            [self didTouchButtonTop:button];
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyPaperTableViewCell *cell0 = [MyPaperTableViewCell cellWithTableView:tableView AndIndex:0];
    MyPaperTableViewCell *cell1 = [MyPaperTableViewCell cellWithTableView:tableView AndIndex:1];
    if (tableView.tag == 1) {
        [cell1.buttonBuy addEventHandler:^(id sender) {
            SHOWSUCCESS(@"")
        } forControlEvents:(UIControlEventTouchUpInside)];
        return cell1;
    }
    return cell0;
}

- (IBAction)didTouchButtonTop:(UIButton *)sender {
    [self.arrayButtonTop enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = obj;
        button.selected = false;
        button.userInteractionEnabled = true;
    }];
    sender.selected = true;
    [UIView animateWithDuration:0.2 animations:^{
        self.viewLine.centerX = sender.centerX;
        self.scrollViewBackground.contentOffset = CGPointMake(kGXScreenWidth * sender.tag, 0);
    }];
}


@end