//
//  ReplayMeVC.m
//  smartRhino
//
//  Created by niuzhen on 2020/6/11.
//  Copyright © 2020 tederen. All rights reserved.
//

#import "ReplayMeVC.h"
#import "MyTDGroupView.h"
#import "ReplayMeCell.h"
#import "ShowBtn.h"
#import "YCMenuView.h"
#import "ReplayMeLikeVC.h"
#import "ReplayMeSearchVC.h"
#import "ReplyModel.h"

@interface ReplayMeVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *navBar;
@property (weak, nonatomic) IBOutlet UIView *HeadView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) ShowBtn            *titleBtn;
@property (strong, nonatomic) MyTDGroupView      *SearchView;
@property (strong, nonatomic) NSMutableArray     *taskActionArray;
@property (strong, nonatomic) NSMutableArray     *dataArray;
@property (assign, nonatomic) BOOL                replayMe;
@property (assign, nonatomic) NSInteger           PraiseCount;

@end

@implementation ReplayMeVC
+(ReplayMeVC *)initReplayMeVC{
    ReplayMeVC *controller = [StoryboardManager.shared.Source instantiateViewControllerWithIdentifier:@"ReplayMeVC"];
    return controller;
}
- (ShowBtn *)titleBtn
{
    if (!_titleBtn) {
        _titleBtn = [ShowBtn new];
    }
    return _titleBtn;
}
- (MyTDGroupView *)SearchView
{
    if (!_SearchView) {
        _SearchView = [[MyTDGroupView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 36)];
    }
    return _SearchView;
}
- (void)autoSizeBtn:(NSString *)title
{
    [self.titleBtn setLabelTitle:title];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getData];
}
- (void)getData
{
    [self.dataArray removeAllObjects];
    NSDictionary * paraDict = @{@"KeyWord":@"",
                                @"TypeValue":self.replayMe ? @(0) : @(1),
    };
    WS(weakSelf);
    SHOWLOADING
    [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_Reply_List) parameters:paraDict responseStyle:JOSN success:^(id  _Nonnull responseObject) {
        REMOVESHOW
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            for (NSDictionary * dict in responseObject[@"Item"]) {
                ReplyModel * model = [ReplyModel modelWithDictionary:dict];
                [weakSelf.dataArray addObject:model];
            }
            weakSelf.PraiseCount = [responseObject[@"PraiseCount"] integerValue];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableView reloadData];
        });
    } failure:^(NSError * _Nonnull error) {
        REMOVESHOW
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.HeadView addSubview:self.SearchView];
    [self.SearchView  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(6);
        make.left.right.mas_equalTo(self.HeadView);
        make.height.mas_offset(36);
    }];
    WS(weakSelf);
    [self.SearchView.button setAction:^{
        ReplayMeSearchVC * vc = [[ReplayMeSearchVC alloc] init];
        vc.IsReplyMe = weakSelf.replayMe;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    [self.navBar addSubview:self.titleBtn];
    [self.titleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.navBar);
    }];
    if (self.replayMe) {
        [self autoSizeBtn:@"我回复的"];
    }else{
        [self autoSizeBtn:@"回复我的"];
    }
    [self.titleBtn setAction:^{
        [weakSelf.titleBtn show];
        NSMutableArray *menuDataSourceArray = [weakSelf getMenuDataSource:weakSelf.taskActionArray];
        YCMenuView *view = [YCMenuView menuWithActions:menuDataSourceArray width:105 relyonView:weakSelf.titleBtn];
        view.menuColor = UIColorHex(0xFFFFFF);
        view.separatorColor = UIColorHex(0xEAEAEA);
        view.textColor = UIColorHex(0x666666);
        view.textFont = [UIFont systemFontOfSize:16.0];
        view.menuCellHeight = 45;
        view.maxDisplayCount = 10;
        view.offset = - 6;
        [view show];
    }];
}
-(NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSMutableArray *)taskActionArray{
    if(!_taskActionArray){
        _taskActionArray = [[NSMutableArray alloc] initWithObjects:@"回复我的",@"我回复的", nil];
    }
    return _taskActionArray;
}
-(NSMutableArray *)getMenuDataSource:(NSMutableArray *)titleArray{
    NSMutableArray *menuDataSourceArray = [[NSMutableArray alloc] init];
    for(int i = 0;i < titleArray.count;i ++){
        NSString *titleStr = [titleArray objectAtIndex:i];
        WS(weakSelf);
        YCMenuAction *actionMenu = [YCMenuAction actionWithTitle:titleStr image:nil handler:^(YCMenuAction *action) {
            [weakSelf.titleBtn dismiss];
            [weakSelf autoSizeBtn:titleStr];
            if([@"回复我的" isEqualToString:action.title]){
                weakSelf.replayMe = NO;
            }else{
                weakSelf.replayMe = YES;
            }
            [weakSelf getData];
        }];
        [menuDataSourceArray addObject:actionMenu];
    }
    return menuDataSourceArray;
}
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (!self.replayMe) {
        return 45.f;
    }else{
        return 0.01f;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [UIView new];
    if (!self.replayMe) {
        UILabel * label = [UILabel new];
        label.font = Kfont(16);
        label.textColor = UIColorHex(0x0a0a0a);
        label.text = @"收到的赞";
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.centerY.mas_equalTo(view);
        }];
        UIImageView * imgv = [UIImageView new];
        imgv.image = IMG(@"right_img");
        [view addSubview:imgv];
        [imgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            make.centerY.mas_equalTo(view);
        }];
        UILabel * redL = [UILabel new];
        redL.font = Kfont(11);
        redL.textColor = UIColorHex(0xFFFFFF);
        redL.backgroundColor = [UIColor redColor];
        redL.hidden = self.PraiseCount == 0 ? YES : NO;
        redL.text = [NSString stringWithFormat:@"%ld",self.PraiseCount];
        redL.textAlignment = NSTextAlignmentCenter;
        [view addSubview:redL];
        [redL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(imgv.mas_left).offset(-8);
            make.centerY.mas_equalTo(view);
            make.size.mas_offset(CGSizeMake(16, 16));
        }];
        redL.layer.cornerRadius = 8.f;
        redL.layer.masksToBounds = YES;
        UIView * lineV = [UIView new];
        lineV.backgroundColor = LINEBGCOLOR;
        [view addSubview:lineV];
        [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.mas_equalTo(view);
            make.height.mas_offset(0.5);
        }];
        WS(weakSelf);
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            ReplayMeLikeVC * vc = [ReplayMeLikeVC initReplayMeLikeVC];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
        [view addGestureRecognizer:tap];
    }
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReplyModel * model = [self.dataArray objectAtIndex:indexPath.row];
    if (model.IsComment) {
        ReplayMeCell * cell = [ReplayMeCell configCell0:tableView indexPath:indexPath];
        cell.redV.hidden = model.IsRead;
        [cell setDataCommentModel:model];
        return cell;
    }else{
        ReplayMeCell * cell = [ReplayMeCell configCell1:tableView indexPath:indexPath];
        cell.redV.hidden = model.IsRead;
        [cell setDataModel:model];
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ReplyModel * model = [self.dataArray objectAtIndex:indexPath.row];
    MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
    model.IsRead = YES;
    vc.type = model.TypeValue;
    vc.Id = model.SourceId;
    [self.navigationController pushViewController:vc animated:YES];
}
@end