//
//  IndexAllVC.m
//  smartRhino
//
//  Created by niuzhen on 2020/9/17.
//  Copyright © 2020 tederen. All rights reserved.
//

#import "IndexAllVC.h"
#import "IndexAllCell.h"
#import "IndexAllModel.h"
#import "HomeSubItemModel.h"
#import "BookTeacherDetailVC.h"
#import "BookWCDetailVC.h"
#import "BookListenVC.h"
#import "BookListDetailVC.h"
#import "IndexMoreVC.h"
#import "HomeSCDetailVC.h"

@interface IndexAllVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView    * tableView;
@property (nonatomic, strong) NSMutableArray * dataArray;
@end

@implementation IndexAllVC
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorHex(0xF8F8F8);
    [self.view addSubview:self.tableView];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(20,15,(IS_IPHONEX ? 40 : 20) + EMVIEWBOTTOMMARGIN + 50,15));
    }];
}
- (void)getData
{
    WS(weakSelf);
    [self.dataArray removeAllObjects];
    SHOWLOADING
    [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_Page_Search) parameters:@{@"keyword":self.searchText} responseStyle:JOSN success:^(id  _Nonnull responseObject) {
        REMOVESHOW
        NSLog(@"%@",responseObject);
        if ([responseObject isKindOfClass:[NSArray class]]) {
            for (NSDictionary * dict in responseObject) {
                IndexAllModel * model = [IndexAllModel modelWithDictionary:dict];
                NSMutableArray * array = [NSMutableArray array];
                for (NSDictionary * subDict in model.Items) {
                    HomeSubItemModel * subModel = [HomeSubItemModel modelWithDictionary:subDict];
                    [array addObject:subModel];
                }
                model.Items = array.mutableCopy;
                [weakSelf.dataArray addObject:model];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableView reloadData];
        });
    } failure:^(NSError * _Nonnull error) {
        REMOVESHOW
    }];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 14.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [UIView new];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 14)];
    view.backgroundColor = UIColorHex(0xF8F8F8);
    return view;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WS(weakSelf);
    IndexAllModel * model = [self.dataArray objectAtIndex:indexPath.section];
    IndexAllCell * cell = [IndexAllCell configCell0:tableView indexPath:indexPath];
    [cell setDataWithModel:model searchText:self.searchText];
    [cell.topBtn setAction:^{
        [weakSelf top:model.LableId];
    }];
    [cell.moreBtn setAction:^{
        [weakSelf moreWithName:model.LableName Id:model.LableId ];
    }];
    cell.ClickRowBlock = ^(CollectModelType type, NSInteger Id) {
        [weakSelf pushVC:type Id:Id];
    };

    return cell;
}

- (void)top:(NSInteger)Id
{
    WS(weakSelf);
    [[HttpManager sharedHttpManager] PUTUrl:Host(API_APP_Page_System_Lable_User) parameters:@{@"SystemLableId":@(Id)} responseStyle:DATA success:^(id  _Nonnull responseObject) {
        [weakSelf getData];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
- (void)moreWithName:(NSString *)name Id:(NSInteger)Id
{
    IndexMoreVC * vc = [IndexMoreVC initIndexMoreVC];
    vc.Id = Id;
    vc.titleStr = name;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)pushVC:(CollectModelType)type Id:(NSInteger)Id
{
    switch (type) {
        case CollectModel_StoreBook:
        {
            BookListDetailVC * vc = [BookListDetailVC initBookListDetailVC];
            vc.Id = Id;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CollectModel_StoreVideo:
        {
            BookWCDetailVC * vc = [BookWCDetailVC initBookWCDetailVC];
            vc.Id = Id;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CollectModel_StoreSound:
        {
            BookListenVC * vc = [BookListenVC initBookListenVC];
            vc.Id = Id;
            vc.MediaType = MediaMusicType;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CollectModel_Teacher:
        {
            BookTeacherDetailVC * vc = [BookTeacherDetailVC initBookTeacherDetailVC];
            vc.Id = Id;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CollectModel_Organization:
        {
            HomeSCDetailVC * vc = [[HomeSCDetailVC alloc] init];
            vc.Id = Id;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
        {
            MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
            vc.type = type;
            vc.Id = Id;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
    }
}
@end