var Jimp = require('jimp');
var Promise = require('promise');
var path = require('path');
var fs = require("fs");
var config = require("./config");
var util = require("./util")

var iconInfos = [
    {w:1024, name: "icon-1024.png"},
    {w: 40, name: "icon-40.png"},
    {w: 80, name: "icon-40@2x.png"},
    {w: 50, name: "icon-50.png"},
    {w: 100, name: "icon-50@2x.png"},
    {w: 120, name: "icon-60@2x.png"},
    {w: 180, name: "icon-60@3x.png"},
    {w: 72, name: "icon-72.png"},
    {w: 144, name: "icon-72@2x.png"},
    {w: 76, name: "icon-76.png"},
    {w: 152, name: "icon-76@2x.png"},
    {w: 167, name: "icon-83.5@2x.png"},
    {w: 29, name: "icon-small.png"},
    {w: 58, name: "icon-small@2x.png"},
    {w: 87, name: "icon-small@3x.png"},
    {w: 57, name: "icon.png"},
    {w: 114, name: "icon@2x.png"}
]

var iconsRoot = path.join(process.cwd(), "icons");
//var sourceIcon = iconsRoot + "/icon.png";
var sourceIcon = iconsRoot + "/icon.jpg";

var distFolder0, distFolder1, distFolder2;
var distFolder;

function genAll() {

    distFolder0 = path.join(process.cwd(), "publish/ios/" + config.compileOptions.pureGameId + "/Images.xcassets/AppIcon.appiconset");
    distFolder1 = path.join(process.cwd(), "publish/ios_shell/" + config.compileOptions.pureGameId + "/Images.xcassets/AppIcon.appiconset");
    distFolder2 = path.join(process.cwd(), "publish/ios_shell1/" + config.compileOptions.pureGameId + "/Images.xcassets/AppIcon.appiconset");

    if(!fs.existsSync(sourceIcon)) {
        console.error("请确保icon文件存在: " + sourceIcon + ", 尺寸最好1024*1024以上");
        return;
    }
    try{
        if(fs.existsSync(distFolder0)) {
            distFolder = distFolder0;
            genIcons(iconInfos).then(function(){
                console.log("---------gen iOS icons over----------");
                if(fs.existsSync(distFolder1)) {
                    util.copyDir(distFolder0, distFolder1);
                }
                if(fs.existsSync(distFolder2)) {
                    util.copyDir(distFolder0, distFolder2);
                }
            }).catch(console.log);
        }
    } catch (e) {
        console.log(e)
    }


}

function genIcons(iconInfos) {
    var p = Promise.resolve();
    iconInfos.forEach(function(iconInfo){
        p = p.then(function(){
            return genIcon(iconInfo);
        })
    })
    return p;
}

function genIcon(iconInfo) {
    console.log("Generate " + iconInfo.name + "...")
    var isJpg = iconInfo.name.toLowerCase().indexOf(".jpg") > 0 || iconInfo.name.toLowerCase().indexOf(".jpeg") > 0
    return Jimp.read(sourceIcon).then(function(sImage) {
        if(isJpg) {
            return sImage.resize(iconInfo.w, iconInfo.w)     // resize
                .quality(60)                               // set JPEG quality
                //.greyscale()                               //set greyscale
                .write(distFolder + "/" + iconInfo.name) // save
        } else {
            return sImage.resize(iconInfo.w, iconInfo.w)     // resize
                //.greyscale()                               //set greyscale
                .write(distFolder + "/" + iconInfo.name) // save
        }
    })
}

module.exports = genAll;