//
//  JYWhiteBoardManager.m
//  WhiteBoard
//
//  Created by yrx on 2021/3/17.
//  Copyright © 2021 Facebook. All rights reserved.
//

#import "JYWhiteBoardManager.h"
#import "JYWhiteBoardModule.h"

static JYWhiteBoardManager *whiteBoardManager = nil;

@interface JYWhiteBoardManager ()<WhiteCommonCallbackDelegate, WhiteRoomCallbackDelegate>

@property (nonatomic, strong) WhiteSDK *sdk;
@property (nonatomic, strong) WhiteRoom *room;
@property (nonatomic, strong) JYWhiteBoardModule *whiteBoardModule;

@end

@implementation JYWhiteBoardManager

+(JYWhiteBoardManager *)shareManager{
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        whiteBoardManager = [[[self class]alloc]init];
        
    });
    
    
    return whiteBoardManager;
}


-(void)initWithOptions:(NSDictionary *)options whiteBoardModule:(JYWhiteBoardModule *)whiteBoardModule{
    
    self.whiteBoardModule = whiteBoardModule;
    NSString *appId = options[@"appId"];
    
    // 4. 初始化 SDK 配置类，根据需求设置配置
        WhiteSdkConfiguration *config = [[WhiteSdkConfiguration alloc] initWithApp:appId];
    [config setUserCursor:YES];

        // 5. 初始化 WhiteSDK，并传入callback，可以在 Example 中查看 callback 实现
        self.sdk = [[WhiteSDK alloc] initWithWhiteBoardView:self.boardView config:config commonCallbackDelegate:self];
}
-(void)joinRoomWithOptions:(NSDictionary *)options{
    
    NSString *roomId = options[@"roomId"];
    NSString *roomToken = options[@"roomToken"];
    WhiteRoomConfig *roomConfig = [[WhiteRoomConfig alloc] initWithUuid:roomId roomToken:roomToken];

      [self.sdk joinRoomWithConfig:roomConfig callbacks:self completionHandler:^(BOOL success, WhiteRoom * _Nonnull room, NSError * _Nonnull error) {
          if (success) {
              self.room = room;
              [self.room disableDeviceInputs:YES];
          } else {
              // 错误处理
          }
      }];
}

-(void)setStrokeColorWithOptions:(NSDictionary *)options{
    
    self.rgb = options;
         if (!self.room){
           return;
         }
    
         int r = [options[@"r"] intValue];
    int g =[options[@"g"] intValue];
         int b = [options[@"b"] intValue];
    WhiteMemberState *state = [[WhiteMemberState alloc] init];
    NSArray *strokeColor = [NSArray arrayWithObjects:[NSNumber numberWithInt:r],[NSNumber numberWithInt:g],[NSNumber numberWithInt:b],nil];
    state.strokeColor= strokeColor;
    [self.room setMemberState:state];

}

-(void)destroy{
 
}


-(void)fireRoomStateChanged:(WhiteRoomState *)modifyState{
    
    [self.whiteBoardModule sendEventWithName:@"onRoomStateChanged" body:@{@"zoomScale": @"111"}];
}

@end
