//
//  NXWhiteboard.h
//  NXKit
//
//  Created by 徐家骏 on 2020/7/4.
//  Copyright © 2020 徐家骏. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "NXWhiteboardCommand.h"
#import "NXWhiteboardElement.h"
#import "NXWhiteboardActionReport.h"
#import "NXWhiteboardElementPopMenusProtocol.h"

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(int, NXWhiteboardMode)
{
    NXWhiteboardModeDraw    = 1,  //画元素模式
    NXWhiteboardModeEraser  = 2,  //橡皮擦模式，手势与笔画相交即擦除整个笔画
    
    NXWhiteboardModeEdit   = 10,  //编辑模式下，可选中，缩放，旋转，更改颜色粗细等属性
    
    NXWhiteboardModeSync   = 99,  //当前是同步模式，同步模式不能操作，收到的收据全部缓存。同步包括本地同步和远程同步，本地同步从文件读，远程同步从其他参与者同步
                                   //这个废弃掉，转在sdk内部控制，因为app ui的需要要来改这个值，会引起冲突。
};

typedef NS_ENUM(int, NXWhiteboardLoadMode)
{
    /// 无操作，不清除现有元素
    NXWhiteboardLoadModeNone      = 1,
    /// 清除自己所有元素
    NXWhiteboardLoadModeClearSelf = 2,
    /// 清除白板所有元素
    NXWhiteboardLoadModeClearAll  = 3,
    /// 只加载本地不发送数据
    NXWhiteboardLoadModeLocal     = 4,
};

typedef NS_ENUM(int, NXWhiteboardUserActivityTrackMode)
{
    /// 不跟踪用户操作
    NXWhiteboardUserActivityTrackModeNone      = 0,
    /// sdk缺省跟踪显示方案，需要app实现回调方法：- (NSString *)whiteboard:(NXWhiteboard *)whiteboard nickNameOfUid:(int64_t)uid;
    NXWhiteboardUserActivityTrackModeDefault   = 1,
    /// 由app决定显示方式，对方用户有操作行为时将回调给app，
    NXWhiteboardUserActivityTrackModeCustom    = 2,
};

typedef NS_ENUM(int, NXWhiteboardBuildFileType)
{
    /// 图片 格式
    NXWhiteboardBuildFileTypeImage    = 1,
    /// PDF 格式
    NXWhiteboardBuildFileTypePDF      = 2,
};

typedef NS_ENUM(int, NXWhiteboardBuildFileCode)
{
    /// 开始
    NXWhiteboardBuildFileCodeStart    = 1,
    /// 结束
    NXWhiteboardBuildFileCodeEnd      = 2,
    /// 停止
    NXWhiteboardBuildFileCodeStoped   = 10,
    /// 失败
    NXWhiteboardBuildFileCodeError    = 20,
};



@class NXWhiteboard;

@protocol NXWhiteboardDelegate <NSObject>

@optional
- (void)whiteboard:(NXWhiteboard *)whiteboard sendCommand:(NXWhiteboardCommand *)command;
- (void)whiteboard:(NXWhiteboard *)whiteboard sendCommand:(NXWhiteboardCommand *)command to:(int64_t)uid;

@end

//这个用于通知app关闭笔画选择panel等。
@protocol NXWhiteboardAppDelegate <NSObject>
@optional
- (void)whiteboardTouchBegan:(NXWhiteboard*)whiteboard;
- (void)whiteboard:(NXWhiteboard *)whiteboard scrollToPageNumber:(float)pageNumber;
- (void)whiteboardHasElementsSelected:(NXWhiteboard *)whiteboard;  //有元素选中的时候，让橡皮擦变成垃圾桶
- (void)whiteboardAllElementsUnselected:(NXWhiteboard *)whiteboard;
- (void)whiteboard:(NXWhiteboard *)whiteboard textViewDidChange:(UITextView *)textView;
- (void)whiteboard:(NXWhiteboard *)whiteboard textViewDidBeginEditing:(UITextView *)textView;
- (void)whiteboard:(NXWhiteboard *)whiteboard textViewDidEndEditing:(UITextView *)textView;
- (NSString *)whiteboard:(NXWhiteboard *)whiteboard nickNameOfUid:(int64_t)uid;

@end
/**
 画板功能
 支持背景图，pdf（同一块板仅一种支持）。
 背景图使用：
    初始化时设置
    1.需要设置内容的高宽比 -pageHeightRatio-，多种比例的背景，只设置一次；
    2.设置总页数-numPages-；
    3.设置每一页的背景图- (void)setBackground:(UIImage *)image forPage:(int)page。
 PDF使用：
    设置pdf文件的本地路径：- (void)setPDFPath:(NSString *)filePath;
 */
@interface NXWhiteboard: UIView

@property (nonatomic)           int64_t  sessionId;
@property (nonatomic)           int      wbid;
@property (nonatomic)           int64_t  uid;
///画板总页数
@property (nonatomic)           int      numPages; //虚拟的白板一共有多少页
/// H/V:背景图比例
@property (nonatomic)           CGFloat  pageHeightRatio; //高度/宽度
@property (nonatomic, strong)   NSMutableDictionary *pagedBackground;
@property (nonatomic, strong)   NSMutableDictionary *drawCaches;
@property (nonatomic, strong)   NSMutableArray<NXWhiteboardElement *>  *elements;
@property (nonatomic, strong)   NSMutableArray<NXWhiteboardCommand *>  *commands;
@property (nonatomic, strong)   NSMutableArray  *redoCommands;

@property (nonatomic, strong)   UICollectionView      *collectionView;
@property (nonatomic, strong)   NXWhiteboardElement  *myActiveElement;
@property (nonatomic, strong)   NSMutableArray<NXWhiteboardElement *> *peersActiveElements;
@property (nonatomic, strong)   NSMutableArray<NXWhiteboardElement *> *mySelectedElements;
//@property (nonatomic, strong)   NSMutableArray        *peersSelectedElements;
@property (nonatomic, strong)   NSMutableArray<NXWhiteboardElement *> *myCopiedElements; //copy/paste时用
@property (nonatomic)           CGRect                 selectRect; //多选时画临时的选择区域

@property (nonatomic)          BOOL                       drawEnable;
@property (nonatomic)          NXWhiteboardMode          currentMode;
@property (nonatomic)          NXWhiteboardElementType   currentElementType;
@property (nonatomic)          uint32_t                   currentPenColor;
//@property (nonatomic, strong)  UIColor                   *currentPenUIColor;
@property (nonatomic)          uint32_t                   currentPenSize;

///1:连续线；2:虚线; 3: 笔锋,只有在doodle下生效
@property (nonatomic)          uint32_t                   currentPenStyle;
    //笔画尺寸用一个统一数字s=1-255，表示s/1000的白板宽度
@property (nonatomic, strong)  NSString                  *currentTextFont;
@property (nonatomic)          uint32_t                   currentTextColor;
@property (nonatomic)          uint32_t                   currentTextSize;  //文本的size也是归一化的数字，要根据屏幕尺寸进行换算

@property (nonatomic)          int                        currentEid;
@property (nonatomic, weak)    id<NXWhiteboardDelegate>  delegate;
@property (nonatomic, weak)    id<NXWhiteboardAppDelegate>  appDelegate;
@property (nonatomic, readonly) NXWhiteboardActionReport *actionReport;

/*
 1.激光笔发出者图片要求：使用顺序laserImageNameLocal > myLaserImage
 图片宽高要求54*84
 指示点位置与图片中心位置22*37
 2.激光笔接收者图片要求： 使用顺序 laserImageNamePeer > laserImage
 正方形
 */
@property (nonatomic, strong)  UIImage                   *myLaserImage;
@property (nonatomic, strong)  UIImage                   *laserImage;
@property (nonatomic, strong)  NSString                  *laserImageNameLocal;
@property (nonatomic, strong)  NSString                  *laserImageNamePeer;
@property (nonatomic, strong)  UIImageView               *myLaser;
@property (nonatomic, strong)  NSMutableDictionary       *peersLaser;

@property (nonatomic)          float     currentPageNumber;
@property (nonatomic)          BOOL      scrollEnabled;
@property (nonatomic, strong)  NSString *tmpPath;

@property (nonatomic)          BOOL      canUndo;
@property (nonatomic)          BOOL      canRedo;
@property (nonatomic)          int       maxUndoSteps;

    ///返回当前有效的贴图有多少张，这个用于app层可限制最大数量，以免生成备课文件等的时候数据量过大。
@property (nonatomic)          int       currentValidImageNumber;
    ///用于设置对方有动作的时候，是否显示？三种模式：none不显示，default由sdk缺省显示，custom回调给app显示
@property (nonatomic)          NXWhiteboardUserActivityTrackMode   userActivityTrackMode;

@property (nonatomic)  UIView<NXWhiteboardElementPopMenusProtocol> *popMenu;

/// sync whiteboard offset when scrolled, default YES.
@property (nonatomic) BOOL enableSyncOffset;
/// 画板滚动结束后的动画开关，默认开启
@property (nonatomic) BOOL scrollAnimate;

//以下为手势
@property (nonatomic) UIPinchGestureRecognizer *pinchGesture;

+ (instancetype)NXWhiteboard:(int)wbid;
- (int)undo;  //撤销自己最后的动作
- (int)redo;  //重做自己被撤销的最后动作
- (void)reset;  //回到初始化状态
- (void)receiveCommand:(NXWhiteboardCommand *)command; //收到其他方的指令
- (void)sendCommand:(NXWhiteboardCommand *)command; //发送指令

- (void)drawImage:(UIImage *)image; //找一个rect画图并进入编辑模式来定位
- (void)drawImage:(UIImage *)image atRect:(CGRect)rect;
//- (void)drawImage:(UIImage *)image atPage:(int)page contentMode:(UIViewContentMode)mode editable:(BOOL)editable;
- (void)redrawCacheForPage:(int)page;

///以下用来在子白板上设置课件，可以分页设置，也可以整个设置，由白板来切分成多页。
//- (void)setBackground:(UIImage *)image;
- (void)setBackground:(UIImage *)image forPage:(int)page;
///要展示的PDF文件本地路径
- (void)setPDFPath:(NSString *)filePath;
- (void)scrollToPage:(int)page animation:(BOOL)animation; //这个课件白板用
- (void)scrollToOffsetPage:(float)page animation:(BOOL)animation; //这个缺省白板用

- (CGFloat)textSizeOfSize:(int)size;
- (CGFloat)lineWidthOfSize:(int)size;
- (CGFloat)lineWidthOfCurrentSize;
- (CGRect)unNormRect:(CGRect)norm;
- (CGPoint)unNormPoint:(CGPoint)norm;

- (NSData *)dataToSave;
- (void)restoreFromData:(NSData *)data;

/// 保存板书
- (NSData *)elementsToStore;

/// 加载板书
/// @param obj 数据
/// @param mode 加载模式
- (BOOL)loadElements:(id)obj withLoadMode:(NXWhiteboardLoadMode)mode;

/// 白板内容生成PDF/Image。图片类型每十页白板归档一页。
/// @param fileType 文件类型
/// @param fileName 文件名称，默认 nv_sessionid_wbid
/// @param filePath 文件路径，默认存储 tmp 路径
/// @param fileWidth 文件宽度，默认宽度为白板宽度，1～10000
/// @param color 背景颜色
/// @param block 回调
- (void)buildBoardToFileWithType:(NXWhiteboardBuildFileType)fileType
                        fileName:(NSString *)fileName
                        filePath:(NSString *)filePath
                       fileWidth:(CGFloat)fileWidth
                 backgroundColor:(UIColor *)color
                           block:(void(^)(NXWhiteboardBuildFileCode state, NSString *fileName, NSArray *filePaths))block;

/// 停止生成
- (void)stopBuildBoard;
/**
 画板不会主动同步数据，需要使用者指定某人同步。
 */
- (void)syncFromUid:(int64_t)uid;
- (void)syncFromUid:(int64_t)uid callback:(nullable void(^)(BOOL success, BOOL timeout))callback;

- (void)syncDataOfEid:(int)eid withUid:(int64_t)uid times:(int)times;

- (void)eraseSelectedElements;

- (void)memberLeaved:(int64_t)uid;

- (NSString *)nickNameOfUid:(int64_t)uid;

- (void)lockSelectedElements;
- (void)unlockSelectedElements;
- (void)sendSelectedElementsToBack;
- (void)bringSelectedElementsToFront;
- (void)combineSelectedElements;
- (void)decombineSelectedElement;
- (void)rotateRightSelectedElement;
- (void)rotateLeftSelectedElement;
- (void)copySelectedElement;
- (void)pasteCopiedElement;

- (void)eraseVisibleElements;

@end

NS_ASSUME_NONNULL_END
