

#import "AccountDetailsViewController.h"

#import "RageShakeManager.h"

@interface AccountDetailsViewController()
{
    NSInteger globalNotificationSettingsRowIndex;
    
    // The "Global Notification Settings" button
    UIButton *globalNotifSettingsButton;
}
@end

@implementation AccountDetailsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // Setup `MXKRoomMemberListViewController` properties
    self.rageShakeManager = [RageShakeManager sharedManager];
}

- (void)destroy
{
    [super destroy];
    
    globalNotifSettingsButton = nil;
}

#pragma mark - TableView data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = [super tableView:tableView numberOfRowsInSection:section];
    
    // Add one button in notification section to edit global notification settings
    if (section == notificationsSection)
    {
        globalNotificationSettingsRowIndex = count++;
    }
    
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    
    if (indexPath.section == notificationsSection && indexPath.row == globalNotificationSettingsRowIndex)
    {
        MXKTableViewCellWithButton *globalNotifSettingsBtnCell = [tableView dequeueReusableCellWithIdentifier:[MXKTableViewCellWithButton defaultReuseIdentifier]];
        if (!globalNotifSettingsBtnCell)
        {
            globalNotifSettingsBtnCell = [[MXKTableViewCellWithButton alloc] init];
        }
        [globalNotifSettingsBtnCell.mxkButton setTitle:NSLocalizedStringFromTable(@"notification_settings_global_notification_settings", @"MatrixConsole", nil) forState:UIControlStateNormal];
        [globalNotifSettingsBtnCell.mxkButton setTitle:NSLocalizedStringFromTable(@"notification_settings_global_notification_settings", @"MatrixConsole", nil) forState:UIControlStateHighlighted];
        [globalNotifSettingsBtnCell.mxkButton addTarget:self action:@selector(onButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        globalNotifSettingsButton = globalNotifSettingsBtnCell.mxkButton;
        
        cell = globalNotifSettingsBtnCell;
        
        
//        MXKTableViewCellWithTextView *userInfoCell = [tableView dequeueReusableCellWithIdentifier:[MXKTableViewCellWithTextView defaultReuseIdentifier]];
//        if (!userInfoCell)
//        {
//            userInfoCell = [[MXKTableViewCellWithTextView alloc] init];
//        }
//        
//        userInfoCell.mxkTextView.text = NSLocalizedStringFromTable(@"settings_webclient_push", @"MatrixConsole", nil);
//        cell = userInfoCell;
    }
    else
    {
        cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }
    
    return cell;
}


#pragma mark - TableView delegate

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (indexPath.section == notificationsSection)
//    {
//        if (indexPath.row == globalNotificationSettingsRowIndex)
//        {
//            UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, MAXFLOAT)];
//            textView.font = [UIFont systemFontOfSize:14];
//            textView.text = NSLocalizedStringFromTable(@"settings_webclient_push", @"MatrixConsole", nil);
//            CGSize contentSize = [textView sizeThatFits:textView.frame.size];
//            return contentSize.height + 1;
//        }
//    }
//    
//    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
//}

- (void)onButtonPressed:(id)sender
{
    if (sender == globalNotifSettingsButton)
    {
        [self performSegueWithIdentifier:@"showGlobalNotificationSettings" sender:self];
    }
    else
    {
        [super onButtonPressed:sender];
    }
}

#pragma mark - Segues

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Keep ref on destinationViewController
    [super prepareForSegue:segue sender:sender];
    
    if ([[segue identifier] isEqualToString:@"showGlobalNotificationSettings"])
    {
        MXKNotificationSettingsViewController *notifSettingsViewController = segue.destinationViewController;
        notifSettingsViewController.mxAccount = self.mxAccount;
    }
}

@end
