//
//  SNSViewController.m
//  LifeLog
//
//  Created by Nguyen Van Phong on 7/25/17.
//  Copyright © 2017 PhongNV. All rights reserved.
//

#import "SNSViewController.h"

#import "ServerAPI.h"
#import "Utilities.h"

#import "SNSRecentTopicTableViewCell.h"
#import "MyGroupViewController.h"
#import "SearchGroupViewController.h"
#import "GroupDetailViewController.h"

@interface SNSViewController ()

@end

@implementation SNSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title = NSLocalizedString(@"lifelog.tapbar.sns", nil);
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.viewHeader attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    
    [self callRequestToUpdateData];

    //register nib for table view
    [self.tableBase registerNib:[UINib nibWithNibName:@"SNSRecentTopicTableViewCell" bundle:nil] forCellReuseIdentifier:@"RecentTopicCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark IBAction
- (IBAction)clickRecommendGroup:(id)sender {
    SearchGroupViewController * search = [[SearchGroupViewController alloc] init];
    [self.navigationController pushViewController:search animated:true];
}

- (IBAction)clickMyGroup:(id)sender {
    MyGroupViewController * myGrp = [[MyGroupViewController alloc] init];
    [self.navigationController pushViewController:myGrp animated:true];
}

#pragma mark UITableView Delegate
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SNSRecentTopicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RecentTopicCell"];
    TweetObject *object = [_curDataList objectAtIndex:indexPath.row];
    [cell setTweetsData:object];
    return cell;
}

#pragma mark Private Function

-(void) callRequestToUpdateData {
    [super callRequestToUpdateData];

    NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
    MBProgressHUD *hudView = nil;
    if(_curPage == 1 && !self.refreshControl.isRefreshing) {
        hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
    }
    [[ServerAPI server] requestTweetsList:token groupID:-1 withPage:_curPage CompletionHandler:^(NSArray *array, NSError *error){
        dispatch_async(dispatch_get_main_queue(), ^{
            if(hudView != nil) {
                [hudView hideAnimated:true];
            }
        });
        SNSViewController __weak *weakSelf = self;
        [weakSelf updateTableData:array error:error];
    }];
}

@end
