# board/board.js

    ModuleBase.define("Board", ["Annotation", "RestServer", "Error"], function(Annotation, RestServer, Error) {
    	/**
    	 *
    	 * @desc Board构造函数。
    	 * 当白板的渲染宽(高)度与输出的宽(高)度不同时，输出的对应的x,y等坐标也是基于这个输出的宽(高)度重新计算的。
    	 * @constructor
    	 * @alias Board
    	 * @param {Object} roomHandle - 房间对象
    	 * @param {String} userId - 创建者userId
    	 * @param {int} width -  白板的渲染宽度
    	 * @param {int} height - 白板的渲染高度
    	 * @param {Object} backgroundColor - 白板的背景色
    	 * @param {String} backgroundImage - 白板的背景图访问路径
    	 * @param {String} title - 白标标题
    	 * @param {int} outputWidth - 白板的输出宽度
    	 * @param {int} outputHeight - 白板的输出高度
    	 * @param {String} description - 描述
    	 * @param {String} extendData - 扩展内容
    	 * @example
    	 * var board = new Board();
    	 */
    	var Board = function(roomHandle, userId, width, height, backgroundColor, backgroundImage, title,outputWidth,outputHeight, description, extendData) {
    		  this.roomHandle = roomHandle;
    		  this.id = Math.uuid();
    		  this.status = boardStatusEnum.close;
    		  this.title = title?title:'';
    		  this.description = description?description:'';
    		  this.extendData = extendData?extendData:'';
    		  this.userId = userId;
    		  this.width =  width;
    		  this.height = height;
    		  this.backgroundColor = backgroundColor;
    		  this.backgroundImage = backgroundImage;
    		  this.annotation;

    		  this.outputWidth = width;
    		  if(outputWidth){
    		  	 this.outputWidth = outputWidth;
    		  }

    		  this.outputHeight = height;
    		  if(outputHeight){
    		  	 this.outputHeight = outputHeight;
    		  }

    		  this.inputWidth = width;
    		  this.inputHeight = height;

    		  this.roomHandle.boardOutputXRatio = this.outputWidth/this.width;    //白板输出宽度/渲染宽度
    		  this.roomHandle.boardOutputYRatio = this.outputHeight/this.height;  //白板输出高度/渲染高度

    		  this.roomHandle.boardInputXRatio =  this.width/this.outputWidth;    //渲染宽度/白板输出宽度
    		  this.roomHandle.boardInputYRatio = this.height/this.outputHeight;  //渲染高度/白板输出高度
    	}

    	/**
    	 * @desc  设置当前白板的渲染宽(高)度，输入的对应的x,y等坐标也是基于这个输入的宽(高)度重新计算的。
    	 * @param {Object} width - 白板的渲染宽度
    	 * @param {Object} height - 白板的渲染高度
    	 */
    	Board.prototype.setRenderWidthAndHeight = function(width, height) {
             log.debug('===Board.setRenderWidthAndHeight, width: ', width, ',height: ', height);
    		 this.width = width;
    		 this.height = height;
    		 this.roomHandle.boardInputXRatio = this.width/this.inputWidth;    //渲染宽度/白板输入宽度
    		 this.roomHandle.boardInputYRatio = this.height/this.inputHeight;  //渲染高度/白板输出高度

    		 this.roomHandle.boardOutputXRatio = this.inputWidth/this.width;    //白板输入宽度/渲染宽度
    		 this.roomHandle.boardOutputYRatio = this.inputHeight/this.height;  //白板输入高度/渲染高度
    	}

    	Board.prototype.setBoardId = function(boardId) {
    		  this.id = boardId;
        }

        /**
         * @desc 上传图片到服务器
    	 * @async
         * @param {String} accessToken token
         * @param {Object} fileTarget 文件对象，input控件选择的图片文件，支持jpeg/jpg/png格式
         * @returns Promise
         */
        Board.prototype.uploadImg2Server = function(accessToken, fileTarget){
    		var deferred = when.defer();
    		var file = fileTarget.files[0];

    		if (!file){
    			var error = new Error(ErrorConstant.upload_filer_required);
    			deferred.reject(error);
    		}

    		// 最大上传文件大小
    		var MAX_SIZE = 15 * 1024 * 1024;

    		// 上传文件类型
    		var FILE_TYPES = ['image/jpeg', 'image/png'];

    		var fileSize = file.size,
                fileType = file.type;

    		log.info('===Board.uploadImg2Server(),fileSize:'+ fileSize+", fileType"+ fileType);

    		if (!FILE_TYPES.includes(fileType)) {
    			fileTarget.value = '';
    		    var error = new Error(ErrorConstant.upload_filer_fileType);
    			error.message = error.message+",Support jpeg/jpg/png only ";
    		    deferred.reject(error);

    		}else if(fileSize > MAX_SIZE) {
    		   fileTarget.value = '';
    		   var error = new Error(ErrorConstant.upload_filer_fileSize);
    		   error.message = error.message+",Within 15M only";
    		   deferred.reject(error);

    		}else {
    			var restServer = new RestServer();
                    restServer.uploadFile(accessToken, file).then(function(fileUrl){
                        fileTarget.value = '';
                        log.info("===Board.uploadImg2Server(),fileUrl:" + fileUrl);
                        deferred.resolve(fileUrl);
                    }).otherwise(function(error){
                       console.info("===Board.uploadImg2Server(),error:",error);
                       deferred.reject(error);
                    });
    		}

    		return deferred.promise;
    	}

    	/**
    	 * @desc 白板的背景图更改
    	 * @param {Object} backgroundImage - 更新白板的背景图
    	 */
    	Board.prototype.updateBackgroundImage = function(backgroundImage) {
            var self = this;
            self.backgroundImage = backgroundImage;
            self.annotation.updateBackgroundImage(backgroundImage).then(function(){
                log.debug('===Board.updateBackgroundImage, backgroundImage: ', backgroundImage);
                if(self.userId == self.roomHandle.selfUser.id){
                    self.roomHandle.selfUser.updateBoard(self);
                }
            }).otherwise(function(err){
                log.debug('===Board.updateBackgroundImage, backgroundImage error:', err);
            });
        }

    	/**
    	 * @desc 白板的背景色更改
    	 * @param {Object} backgroundColor - 更新白板的背景色
    	 */
    	Board.prototype.updateBackgroundColor = function(backgroundColor) {
            // 更改背景色时要清除背景色
            this.backgroundImage = "";

            this.backgroundColor = backgroundColor;
            this.annotation.updateBackgroundColor(backgroundColor);
            if(this.userId == this.roomHandle.selfUser.id){
                this.roomHandle.selfUser.updateBoard(this);
            }
        }

    	/**
    	 * @desc 白板的宽度及高度更改
    	 * @param {int} width -  白板的宽度
    	 * @param {int} height - 白板的高度
    	 */
    	Board.prototype.updateWidthAndHeight = function(width,height) {
    		  this.width = width;
    		  this.height = height;
         }

    	/**
    	 * @desc 创建批注
    	 * @async
    	 */
    	Board.prototype.createAnnotation = function() {
    		var self = this;
    		var deferred = when.defer();
    		if(!self.annotation){
    			var annotation = new Annotation(self.id, self.roomHandle, self);
    			self.annotation = annotation;
    			deferred.resolve(self.annotation);
    		}else{
    			deferred.resolve(self.annotation);
    		}
    		return deferred.promise;
    	}

    	Board.prototype.updateAnnotation = function() {
    	}

    	/**
    	 * @desc 删除当前白板所属的全部批注
    	 */
    	Board.prototype.removeAnnotation = function() {
    		 this.annotation = null;
    	}

    	/**
    	 * @desc 获取当前白板所属的批注
    	 */
    	Board.prototype.getAnnotation = function() {
    		return this.annotation;
    	}

    	return Board;
    });