
//
//  Navigation.m
//  rainbow
//
//  Created by Doha Tubaileh on 9/17/19.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTLog.h>
#import "Rainbow/Rainbow.h"
#import "IEventEmmiter.h"
#import "Contact+DictRepresentation.h"
#import <React/RCTBridgeDelegate.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <React/RCTView.h>
#import "Navigation.h"
#import "RNRainbowRn.h"

@interface Navigation ()
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic) BOOL isRainbowActive;
@end

@implementation Navigation

+(id)sharedManager {
    static Navigation *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _isCallActive = NO;
    }
    return self;
}


-(void)moveViewToCall {
    NSLog(@" [Navigation] moveViewToCall");
    
    // This is a new, modern approach to get the key window and root view controller.
       UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
       UIViewController *rootViewController = keyWindow.rootViewController;

       if (!rootViewController) {
           NSLog(@"[CallNavigation] Error: Root view controller not found.");
           return;
       }
       
       // Check if a navigation controller is available
       UINavigationController *navController = nil;
       if ([rootViewController isKindOfClass:[UINavigationController class]]) {
           navController = (UINavigationController *)rootViewController;
       } else if ([rootViewController isKindOfClass:[UITabBarController class]]) {
           UITabBarController *tabController = (UITabBarController *)rootViewController;
           if ([tabController.selectedViewController isKindOfClass:[UINavigationController class]]) {
               navController = (UINavigationController *)tabController.selectedViewController;
           }
       }

       if (!navController) {
           NSLog(@"[CallNavigation] Error: Navigation controller not found.");
           return;
       }
       
       NSLog(@"[CallNavigation] moveViewToCall");
       
       // Use the existing bridge from the app delegate
       RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:[self bundleURL] moduleProvider:nil launchOptions:nil];

       RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                        moduleName:@"call_activity"
                                                 initialProperties:nil];
       rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

       UIViewController *callViewController = [UIViewController new];
       callViewController.view = rootView;
       
       [navController pushViewController:callViewController animated:YES];
    
}
-(void)moveViewToRainbow {
    if(![NSThread isMainThread]){
        dispatch_async(dispatch_get_main_queue(), ^{
            [self moveViewToRainbow];
        });
        return;
    }
    [[[RNRainbowRn sharedManager] navigationController] popViewControllerAnimated:NO];
    _isCallActive = NO;
}
// Helper method to get the correct bundle URL
- (NSURL *)bundleURL
{
#if DEBUG
    return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
    return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

@end
