//QmsDashboardViewManager.mm
#import <React/RCTViewManager.h>
#import <QmsPluginFramework/QmsPluginFramework.h>
#import <QmsPluginFramework/QmsPlugin.h>

@interface QmsDashboardView : UIView
@property (nonatomic, strong) UIViewController *dashboardVC;
@property (nonatomic, assign) BOOL didSetupVC;

@property (nonatomic, copy) NSString *ClientID;
@property (nonatomic, copy) NSString *ClientCode;
@property (nonatomic, copy) NSString *user_token;

@property (nonatomic, copy) RCTDirectEventBlock onClose;
@end

@implementation QmsDashboardView

- (instancetype)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
    _dashboardVC = [QmsPluginUI makeViewController];   
    _didSetupVC = NO;
    _dashboardVC.view.backgroundColor = [UIColor whiteColor];

    // Listen for native dashboard close notification
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleDashboardClose:)
                                                 name:@"QmsDashboardDidCloseNotification"
                                               object:nil];
  }
  return self;
}

- (void)didMoveToWindow {
    [super didMoveToWindow];
    if (self.window && !_didSetupVC) {
        UIViewController *parentVC = [UIApplication sharedApplication].keyWindow.rootViewController;
        if (parentVC) {

            // 1️⃣ create VC with props
            _dashboardVC = [QmsPluginUI makeViewController];

            [parentVC addChildViewController:_dashboardVC];
            _dashboardVC.view.frame = parentVC.view.bounds;
            _dashboardVC.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            [parentVC.view addSubview:_dashboardVC.view];
            [_dashboardVC didMoveToParentViewController:parentVC];
            _didSetupVC = YES;

            // 2️⃣ forward props again if needed
            [self forwardPropsToVC];
        }
    }
}

- (void)handleDashboardClose:(NSNotification *)note {
    // 1️⃣ Notify JS
    if (self.onClose) {
        self.onClose(@{});
    }

    // 2️⃣ Clean up the native view controller
    [self cleanupDashboard];
}

- (void)cleanupDashboard {
    if (!_dashboardVC) return;

    [_dashboardVC willMoveToParentViewController:nil];
    [_dashboardVC.view removeFromSuperview];
    [_dashboardVC removeFromParentViewController];

    _dashboardVC = nil;
    _didSetupVC = NO;
}

#pragma mark - Prop Setters
- (void)setClientID:(NSString *)ClientID {
    _ClientID = [ClientID copy];
    [self forwardProp:@"ClientID" value:_ClientID];
}

- (void)setClientCode:(NSString *)ClientCode {
    _ClientCode = [ClientCode copy];
    [self forwardProp:@"ClientCode" value:_ClientCode];
}

- (void)setUser_token:(NSString *)user_token {
    _user_token = [user_token copy];
    [self forwardProp:@"user_token" value:_user_token];
}

#pragma mark - Forward Props via KVC
- (void)forwardProp:(NSString *)key value:(NSString *)value {
    if (_dashboardVC && [_dashboardVC respondsToSelector:NSSelectorFromString(key)]) {
        @try {
            [_dashboardVC setValue:value forKey:key];
        } @catch (NSException *exception) {
            NSLog(@"⚠️ Failed to set %@ on dashboardVC: %@", key, exception);
        }
    }
}

- (void)forwardPropsToVC {
    [self forwardProp:@"ClientID" value:_ClientID];
    [self forwardProp:@"ClientCode" value:_ClientCode];
    [self forwardProp:@"user_token" value:_user_token];
}

- (void)removeFromSuperview {
    [self cleanupDashboard];
    [super removeFromSuperview];
}

@end

@interface QmsDashboardViewManager : RCTViewManager
@end

@implementation QmsDashboardViewManager

RCT_EXPORT_MODULE(QmsDashboardView)

- (UIView *)view {
  return [[QmsDashboardView alloc] init];
}

// ✅ Export the props so JS can pass values
RCT_EXPORT_VIEW_PROPERTY(ClientID, NSString)
RCT_EXPORT_VIEW_PROPERTY(ClientCode, NSString)
RCT_EXPORT_VIEW_PROPERTY(user_token, NSString)

@end

#import <React/RCTEventEmitter.h>
#import <React/RCTBridgeModule.h>

@interface QmsDashboardEmitter : RCTEventEmitter <RCTBridgeModule>
@end

@implementation QmsDashboardEmitter

RCT_EXPORT_MODULE();

+ (id)allocWithZone:(NSZone *)zone {
    static QmsDashboardEmitter *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [super allocWithZone:zone];
    });
    return sharedInstance;
}

- (instancetype)init {
    if (self = [super init]) {
        // Listen for the framework notification
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleDashboardClose:)
                                                     name:@"QmsDashboardDidCloseNotification"
                                                   object:nil];
    }
    return self;
}

- (NSArray<NSString *> *)supportedEvents {
    return @[@"onClose"];
}

- (void)handleDashboardClose:(NSNotification *)note {
    // Send event to JS
    [self sendEventWithName:@"onClose" body:note.userInfo];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
