//简单多张图片排列显示模块
var image_builder = function (option, success) {
    var object = new ImageBuilder(option);
    success();
    return object;
}

function ImageBuilder(option) {
    this.markControl = option.markControl;
    this.splitConfig = Array.isArray(option.splitConfig) ? option.splitConfig : [];
    this.init();
}

ImageBuilder.prototype.init = function () {
    this.container = getDom(this.canvas_dom, this.markControl).appendTo(this.markControl.container);
    this.container.hide();
    this.canvas = document.getElementById('image-builder-canvas');
    this.ctx = this.canvas.getContext("2d");
}

ImageBuilder.prototype.build = function (task, callback) {
    new ImageLoader({
        server: this.markControl.context.imageServer,
        flush: true,
        strict: false,
        split: this.splitConfig
    }).merge(task.pictureUrls, this.canvas, task.pictureConfig, function (image) {
        task.imageData = image
        task.imageLayout = image.layout
        callback()
    }, function (error) {
        callback('image load error: ' + error)
    })
}

ImageBuilder.prototype.buildBak = function (task, callback) {
    var self = this;
    //初始化图片拼接配置，过滤实际需要加载的图片
    var config = task.pictureConfig;
    var indexSet = {};
    for (var i = 0; i < config.length; i++) {
        indexSet[config[i].i - 1] = true;
    }
    //调用图片预加载函数，实现回调函数
    var imageObjects = [];
    this.loadImages(imageObjects, indexSet, task.pictureUrls, 0, function (images) {
        var maxWidth = 0;
        var totalHeight = 0;
        for (var i = 0; i < config.length; i++) {
            //计算最大宽度与合计高度
            if (config[i].w <= 0) {
                config[i].w = images[config[i].i - 1].width || 0;
            }
            if (config[i].h <= 0) {
                config[i].h = images[config[i].i - 1].height || 0;
            }
            maxWidth = Math.max(maxWidth, config[i].w);
            totalHeight += config[i].h;
        }
        if (maxWidth > 0 && totalHeight > 0) {
            //设置画布大小及背景颜色
            self.canvas.width = maxWidth;
            self.canvas.height = totalHeight;
            self.ctx.fillStyle = "#FFFFFF";
            self.ctx.fillRect(0, 0, maxWidth, totalHeight);
            //绘画到画布
            var height = 0;
            for (var i = 0; i < config.length; i++) {
                var image = images[config[i].i - 1];
                if (image.loaded === true) {
                    self.ctx.drawImage(image, config[i].x, config[i].y, config[i].w, config[i].h, 0, height, config[i].w, config[i].h);
                    height += config[i].h;
                }
            }
            //生成合并后的图像数据
            task.imageData = this.canvas.toDataURL("image/jpeg");
            self.ctx.clearRect(0, 0, self.canvas.width, self.canvas.height);
        }
        callback();
    }, function (error) {
        if (error) {
            console.log(error);
        }
        callback('image load error');
    });
}

/*
实现一系列图片的预加载
参数indexSet:实际需要的图片序号集合
参数urls:图片URL数组
参数number:array数组遍历下标
参数callback:回调函数——所有图片加载完成后才执行此函数
参数exception:回调函数——任一图片加载失败后立即执行此函数
*/
ImageBuilder.prototype.loadImages = function (images, indexSet, urls, number, callback, exception) {
    var self = this;
    var imageServer = this.markControl.context.imageServer;
    //var defaultServer = this.markControl.option.imageServer;
    if (urls != undefined && number < urls.length) {
        if (indexSet[number] == true) {
            var img = new Image();
            img.onload = function () {
                img.loaded = true;
                images.push(img);
                self.loadImages(images, indexSet, urls, number + 1, callback, exception);
            }
            img.onerror = function () {
                img.onerror = undefined;
                //exception('load error for: ' + img.src);
                images.push({
                    loaded: false,
                    width: 0,
                    height: 0
                });
                self.loadImages(images, indexSet, urls, number + 1, callback, exception);
            }
            img.crossOrigin = '';
            img.src = imageServer + urls[number] + '?' + new Date().getTime();
        } else {
            images.push({
                loaded: false,
                width: 0,
                height: 0
            });
            self.loadImages(images, indexSet, urls, number + 1, callback, exception);
        }
    } else {
        callback.call(this, images);
    }
}

ImageBuilder.prototype.canvas_dom = '<div><canvas id="image-builder-canvas"></canvas></div>';