var fs     = require("fs"),
    path   = require("path"),
    mkdirp = require('mkdirp');

const lattenFile = (src, dest, {r, g, b}) => {
    return new Promise((resolve, reject) => {
        fs.createReadStream(src)
        .pipe(new PNG({
            colorType: 2,
            bgColor: {
                red: r,
                green: g,
                blue: b
            }
        }))
        .on('parsed', function() {
            var writeStream = fs.createWriteStream(dest).on('finish', resolve);
            this.pack().pipe(writeStream);
        });
    })
}

const writeFile = (src, dest) => {
    return new Promise((resolve, reject) => {
        let writeStream = fs.createWriteStream(dest).on('finish', resolve);
        fs.createReadStream(src).pipe(writeStream);
    })
}

class ImageFlatten {
    constructor(bgColor) {
        // super(...args);
        this.apply = this.apply.bind(this)
    }
    
    apply(data, callback) {

        let tmpDir = path.join(global.tmpDir, 'flatten', data.ratio);
        let {
            frames,
            animations
        } = data;

        mkdirp(tmpDir, function (err) {

            let pr = [];

            for (let key in animations) {
                let p = animations[key].map((fIndex, i) => {

                    let src = frames[fIndex]['src'],
                    	filepath = path.join(tmpDir, path.basename(src));

                    	// 将名字进行修改成 folder_filename
                    	// filepath = path.join(tmpDir, (key !== '' ? key + '_' + path.basename(src) : path.basename(src)));
                    
                    frames[fIndex]['src'] = filepath;
                    return writeFile(src, filepath)
                })
                pr = pr.concat(...p)
            }
			Promise.all(pr).then(() => {
				console.log(' ✔ flatten done');
				callback && callback(data);
			});
        })
    }
}

module.exports = ImageFlatten;

@@ -88,487 +88,8 @@
 );%0A%0A
-const lattenFile = (src, dest, %7Br, g, b%7D) =%3E %7B%0A    return new Promise((resolve, reject) =%3E %7B%0A        fs.createReadStream(src)%0A        .pipe(new PNG(%7B%0A            colorType: 2,%0A            bgColor: %7B%0A                red: r,%0A                green: g,%0A                blue: b%0A            %7D%0A        %7D))%0A        .on('parsed', function() %7B%0A            var writeStream = fs.createWriteStream(dest).on('finish', resolve);%0A            this.pack().pipe(writeStream);%0A        %7D);%0A    %7D)%0A%7D%0A%0A
 cons
@@ -926,16 +926,21 @@
      %09//
+ TODO
  %E5%B0%86%E5%90%8D%E5%AD%97%E8%BF%9B%E8%A1%8C%E4%BF%AE%E6%94%B9
@@ -956,16 +956,84 @@
 filename
+, %E5%90%A6%E5%88%99%E5%A4%9A%E6%96%87%E4%BB%B6%E5%A4%B9%E7%9A%84%E5%90%8C%E5%90%8D%E6%96%87%E4%BB%B6%E4%BC%9A%E5%87%BA%E7%8E%B0%E8%A6%86%E7%9B%96%0A                        // %E4%B8%8B%E9%9D%A2%E6%96%B9%E5%BC%8F%E4%BC%9A%E5%AF%BC%E8%87%B4%E6%96%87%E4%BB%B6%E5%90%8D%E8%A2%AB%E4%BF%AE%E6%94%B9%EF%BC%8C%E9%9C%80%E6%94%B9%E6%88%90%E6%96%87%E4%BB%B6%E5%A4%B9%E5%BD%A2%E5%BC%8F
 %0A       

add TODO info