//
//  ThingsAddCompleteViewController.m
//  kneet
//
//  Created by Jason Lee on 5/26/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"
#import "CustomLabel.h"
#import "CustomButton.h"

#import "ThingsAddCompleteViewController.h"
#import "ThingsAddViewController.h"
#import "QRCodeViewController.h"
#import "SignUpCompleteViewController.h"

#import "ThingsViewController.h"
#import "ThingsAddWallpadSubViewController.h"
#import "ThingsAddWallpadViewController.h"


@interface ThingsAddCompleteViewController () {

}
@end


#pragma mark - Class Definition

@implementation ThingsAddCompleteViewController


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

    [self initUI];
    [self prepareViewDidLoad];
}


- (void)initUI {
    

    if (IPHONE_HEIGHT == 460.0f) {
        _constraintImgvAvatarTop.constant -= 75.0f;
    }
    
    _lblTitle.text = NSLocalizedString(@"축하합니다!", @"축하합니다!");
    _lblDesc.text = NSLocalizedString(@"이제부터 '내 장치' 메뉴에서\n사용할 수 있습니다", @"이제부터 '내 장치' 메뉴에서\n사용할 수 있습니다");
    
    [_btnNext setTitle:NSLocalizedString(@"보러가기", @"보러가기") forState:UIControlStateNormal];
    [_btnAddMore setTitle:NSLocalizedString(@"장치 추가", @"장치 추가") forState:UIControlStateNormal];
}

- (void)prepareViewDidLoad {

}

#pragma mark - Main Logic



#pragma mark - UI Events
- (IBAction)btnNextTouched:(id)sender {
    [[JDFacade facade] gotoWishMenu:KNMenuIdThings]; //장치리스트로 이동.
}

- (IBAction)btnAddMoreTouched:(id)sender {

    ThingsViewController *tvc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
    if (tvc) {//장치 리스트를 갱신함.
        [tvc prepareViewDidLoad];
    }

    if ([self.presentingViewController.presentingViewController isKindOfClass:[QRCodeViewController class]]) {//QR코드로 등록한 경우,
        UIViewController *vc = self.presentingViewController.presentingViewController.presentingViewController.presentingViewController; //signup Complete
        if (vc) {//회원가입
            [[JDFacade facade] dismissViewControllerByPush:vc];
            
        } else {//디바이스 메뉴
            vc = self.presentingViewController.presentingViewController.presentingViewController;
            [[JDFacade facade] dismissViewControllerByPush:vc completion:^{
                ThingsAddViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsAddViewController class]];
                [[JDFacade facade].currentViewController.navigationController popToViewController:vc animated:NO];
            }];
        }
    } else {//cam, smartphone, wallpadsubtype

        if (self.presentingViewController.presentingViewController) {//회원가입, 디바이스 메뉴 - 월패드 하위디바이스            
            UIViewController *vc = self.presentingViewController.presentingViewController; //signup Complete
            
            BOOL isWallpadSub = NO;
            if ([vc isKindOfClass:[UINavigationController class]]) {
                UINavigationController *nc = (UINavigationController *)vc;
                isWallpadSub = [nc.viewControllers.firstObject isKindOfClass:[ThingsAddWallpadSubViewController class]];
            }
            
            [[JDFacade facade] dismissViewControllerByPush:vc completion:^{
                
                if (tvc) {//월패드 하위 디바이스
                    ThingsAddViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsAddViewController class]];
                    [[JDFacade facade].currentViewController.navigationController popToViewController:vc animated:NO];
                } else if (isWallpadSub) {
                    UIViewController *pvc = [JDFacade facade].currentViewController.presentedViewController.presentedViewController.presentedViewController;
                    [pvc dismissViewControllerAnimated:NO completion:nil];
                }
            }];

        } else {//디바이스 메뉴
            [[JDFacade facade] dismissViewControllerByPush:self completion:^{
                ThingsAddViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsAddViewController class]];
                [[JDFacade facade].currentViewController.navigationController popToViewController:vc animated:NO];
            }];
        }
    }
}


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


@end
