//
//  MainViewController.m
//  kneet2
//
//  Created by Jason Lee on 10/2/15.
//  Copyright © 2015 ntels. All rights reserved.
//

#import "JDObject.h"

#import "RequestHandler.h"

#import "ThingsViewController.h"
#import "RulesViewController.h"
#import "HomeMemberViewController.h"

#import "MainViewController.h"

@interface MainViewController () {
    ThingsViewController *_tvc;
    RulesViewController *_rvc;
    HomeMemberViewController *_mvc;
    
}

@property (strong, nonatomic) UIView *thingsContainerView;
@property (strong, nonatomic) UIView *rulesContainerView;
@property (strong, nonatomic) UIView *membersContainerView;

@end


#pragma mark - Class Definition

@implementation MainViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initUI];
    [self prepareViewDidLoad];
}


- (void)initUI {
    //1. init tabBar
    //1-1. scroll top bottom
    
    //2. init scrollView
    CGFloat height = IPHONE_HEIGHT - _tabBar.height;
    [_scrollView setContentSize:CGSizeMake(IPHONE_WIDTH * 3, height)];
    _scrollView.pagingEnabled = YES;
    _scrollView.scrollEnabled = NO;
    
    
    CGRect sr = CGRectMake(0, 0, IPHONE_WIDTH, height);
    
    //init child view for childViewController
    _thingsContainerView = [[UIView alloc] initWithFrame:sr];
    _rulesContainerView = [[UIView alloc] initWithFrame:sr];
    _membersContainerView = [[UIView alloc] initWithFrame:sr];
    
    [_scrollView addSubview:_thingsContainerView];
    
    _rulesContainerView.x = IPHONE_WIDTH;
    [_scrollView addSubview:_rulesContainerView];
    
    _membersContainerView.x = IPHONE_WIDTH * 2;
    [_scrollView addSubview:_membersContainerView];
    
    //when first loading this view.
    [self initThingsViewController];
}

- (void)initThingsViewController {
    
    if (!_tvc) {
        _tvc = (ThingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsViewController" storyboardName:@"Things"];
    }
    
    [self addChildViewController:_tvc];
    [_tvc didMoveToParentViewController:self];
    
    if ([self isViewLoaded]) {
        [_tvc beginAppearanceTransition:YES animated:NO];
        [_thingsContainerView addSubview:_tvc.view];
        [_tvc endAppearanceTransition];
    }
}


- (void)initRulesViewController {
    
    if (!_rvc) {
        _rvc = (RulesViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesViewController" storyboardName:@"Rules"];
    }
    
    [self addChildViewController:_rvc];
    [_rvc didMoveToParentViewController:self];
    
    if ([self isViewLoaded]) {
        [_rvc beginAppearanceTransition:YES animated:NO];
        [_rulesContainerView addSubview:_rvc.view];
        [_rvc endAppearanceTransition];
    }
}

- (void)initMembersViewController {
    if (!_mvc) {
        _mvc = (HomeMemberViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberViewController" storyboardName:@"HomeMember"];
    }
    
    [self addChildViewController:_mvc];
    [_mvc didMoveToParentViewController:self];
    
    if ([self isViewLoaded]) {
        [_mvc beginAppearanceTransition:YES animated:NO];
        [_membersContainerView addSubview:_mvc.view];
        [_mvc endAppearanceTransition];
    }
}

- (void)prepareViewDidLoad {
    
}

#pragma mark - Main Logic



#pragma mark - UI Events
- (IBAction)btnThingsTouched:(id)sender {
    
    [[JDFacade facade] showLoadingWhileExecutingBlock:^{
        if (!_tvc) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self initThingsViewController];
            });
        }
    } completionHandler:^{
        CGPoint cpoint = CGPointMake(0, 0);
        [_scrollView setContentOffset:cpoint animated:NO];
    }];
}

- (IBAction)btnRuleTouched:(id)sender {
    
    [[JDFacade facade] showLoadingWhileExecutingBlock:^{
        if (!_rvc) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self initRulesViewController];
            });
        }
    } completionHandler:^{
        CGPoint cpoint = CGPointMake(IPHONE_WIDTH, 0);
        [_scrollView setContentOffset:cpoint animated:NO];
    }];
}

- (IBAction)btnMemberTouched:(id)sender {
    [[JDFacade facade] showLoadingWhileExecutingBlock:^{
        if (!_mvc) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self initMembersViewController];
            });
        }
    } completionHandler:^{
        CGPoint cpoint = CGPointMake(IPHONE_WIDTH * 2, 0);
        [_scrollView setContentOffset:cpoint animated:NO];
    }];
}

- (IBAction)btnExtendTouched:(id)sender {
    
    _constraintExpandViewTop.constant = -IPHONE_HEIGHT;
    
    [UIView animateWithDuration:ANIMATION_DUR animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (IBAction)btnCollapseTouched:(id)sender {
    
    _constraintExpandViewTop.constant = 0;
    
    [UIView animateWithDuration:ANIMATION_DUR animations:^{
        [self.view layoutIfNeeded];
    }];
}


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

@end