const gulp = require('gulp');
const gulpClean = require('gulp-clean');
const Config = require('./config.json');
const packageInfo = require('./package.json');
const {System, Sdk, Haxe, Project} = require('gulp-haxetool');
const dateformat = require('dateformat');
const argv = require('yargs').argv;

if (Config.SdkDir) {
    Sdk.dir = Config.SdkDir;
}

if (Config.BuildDir) {
    Haxe.buildDir = Config.BuildDir;
}

exports.clean = function clean() {
    return gulp.src('target/*', {read: false}).pipe(gulpClean());
};

const config = new Project.Config({
    meta: {
        title: 'Puzzle\'z',
        filename: 'puzzlez',
        icon: 'src/client/resources/icon.png',
        pack: 'ru.m.puzzlez',
        author: 'shmyga <shmyga.z@gmail.com>',
        company: 'MegaLoMania',
        version: packageInfo.version + (Config.Develop ? '-SNAPSHOT' : ''),
    },
    key: Config.Key,
    libs: packageInfo.haxeDependencies,
    macros: [
        `CompilationOption.set('build','${dateformat(new Date(), 'yyyy-mm-dd HH:MM:ss')}')`,
    ],
});

const app = new Project(
    Project.BuildSystem.OPENFL,
    [
        Project.Platform.FLASH,
        Project.Platform.HTML5,
        Project.Platform.LINUX,
        Project.Platform.WINDOWS,
        Project.Platform.ANDROID,
    ],
    config.branch({
        name: 'app',
        sources: [
            'src/haxe',
        ],
        assets: [
            'src/resources',
        ],
        main: 'ru.m.puzzlez.PuzzlezApp',
        meta: {
            width: 1024,
            height: 768,
        },
    }),
).bind(module, gulp);

const defaultSeries = [
    exports.clean,
    module.exports['app:flash:build'],
    module.exports['app:flash:html'],
    module.exports['app:html5:build'],
];

if (System.isLinux) {
    defaultSeries.push(
        module.exports['app:linux:build'],
        module.exports['app:linux:archive'],
        module.exports['app:linux:deb'],

        module.exports['app:android:build'],
    );
}

if (System.isWindows) {
    defaultSeries.push(
        module.exports['app:windows:build'],
        module.exports['app:windows:archive'],
        module.exports['app:windows:installer'],
    );
}

module.exports.default = gulp.series(defaultSeries);
