let fs = require('fs');
let path = require('path');
let sys = require('sys');
let exec = require('child_process').exec;

let filename = "/tmp/tst.json";
let dirname = "/home/apps/shufa/top/";
let regex = /\.jpg$|\.txt$|\.json$|\.tif$/
let configSet = [];
let fileTypes = [ 'imgFile', 'cfgFile', 'unkFile', 'cpyName' ];
let jsonKeys = [ "朝代", "作者", "尺寸", "品名" ];
let IDEOGRAPHIC_SPACE_MATCHER = /\u3000/g;

function readConfigFile(filename) {
    try {
        let content = fs.readFileSync(filename, 'utf8');
        let json = JSON.parse(content);
        let name = json[jsonKeys[1]].trim();
        name.replace(IDEOGRAPHIC_SPACE_MATCHER, '+');
        let dynasty = json[jsonKeys[0]].trim();
        dynasty.replace(IDEOGRAPHIC_SPACE_MATCHER, '+');
        let work = json[jsonKeys[3]].trim();
        work.replace(IDEOGRAPHIC_SPACE_MATCHER, '+');
        let size = json[jsonKeys[2]].trim();
        size.replace(IDEOGRAPHIC_SPACE_MATCHER, '+');
        name = dynasty + '_' + name + '_' + work + '_' + size;
        if (name !== '')
            return name;
        else
            return null;
    } catch (error) {
        console.log(error);
        return null;
    }
}

function getFileExtension(filename) {
    let lastChar = filename[filename.length - 1];

    switch (lastChar) {
        case 'f':
            return  'tif';
        case 'g':
            return 'jpg';
        case 't':
            return 'txt';
        case 'n':
            return 'json';
        default:
            return null;
    }
}

function getFileType(filename) {
    let lastChar = filename[filename.length - 1];
    let typeIndex = 2;

    switch (lastChar) {
        case 'f':
        case 'g':
            typeIndex = 0;
            break;
        case 't':
        case 'n':
            typeIndex = 1;
            break;
        default:
            break;
    }

    return typeIndex;
}

function dirTraversal(dirname) {
    let files = fs.readdirSync(dirname);
    let config = {};

    for (let file of files) {
        let pathname = dirname + file;
        try {
            let type = fs.lstatSync(pathname);
            if (type.isDirectory())
                dirTraversal(dirname + file + "/");
            else if (type.isFile() && file.match(regex)) {
                let typeIndex = getFileType(file)
                config[fileTypes[typeIndex]] = dirname + file;
            }
        } catch (error) {
            console.log(error);
            continue;
        }
    }
    if (config.hasOwnProperty(fileTypes[0]) && config.hasOwnProperty(fileTypes[1]))
        configSet.push(config);
}

dirTraversal(dirname);

let i = 0;
for (let cfg of configSet) {
    if (cfg[fileTypes[1]] !== '') {
        let copyName = readConfigFile(cfg[fileTypes[1]]);
        let ext = getFileExtension(cfg[fileTypes[0]])
        if  (ext !== null && copyName !== null) {
            cfg[fileTypes[3]] = copyName + '.' + ext;
            let cmd = 'cp ' + cfg[fileTypes[0]] + ' ' + cfg[fileTypes[3]];
            exec(cmd , (error, stdout, stderr) => { sys.puts(stdout) });
        }
    }
}