"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const files_1 = require("@iotize/common/files");
const command_1 = require("@oclif/command");
const fs_1 = require("fs");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const lib_1 = require("../../lib");
const config_generator_1 = require("../../lib/config-generator");
const utility_1 = require("../utility");
function showInfo(v) {
    console.log(v);
}
const DEFAULT_APP_SCHEMA = {
    version: {
        generator: '1.0.0'
    },
    app: {
        id: 'my.domain.generatedapp.blankapp',
        version: '1.0.0',
        domain: 'https://mydomain.com',
        name: 'Blank Application',
        platforms: {
            android: {
                enabled: true
            },
            browser: {
                enabled: true,
                PWA: true
            },
            ios: {
                enabled: true
            }
        },
        menu: [],
        pages: {},
        defaultPage: 'connect'
    }
};
function createBlankConfigGenerator() {
    return new config_generator_1.ConfigGenerator(DEFAULT_APP_SCHEMA, {
        meta: {
            version: '1.0.0'
        },
        config: {
            version: '1.0.0',
            application: {
                name: 'Blank app'
            }
        }
    }, undefined);
}
class CreateCommand extends command_1.Command {
    async run() {
        const parsedInput = this.parse(CreateCommand);
        const appPath = parsedInput.args.appPath;
        const options = parsedInput.flags;
        // const testSchema: AppJsonSchema = {} as any;
        showInfo(`Working directory: ${process.cwd()}`);
        const absolutePath = path_1.resolve(process.cwd(), appPath);
        const parentDir = path_1.dirname(absolutePath);
        const baseName = path_1.basename(absolutePath);
        const appName = options.name || baseName;
        if (!fs_1.existsSync(absolutePath)) {
            fs_extra_1.mkdirpSync(absolutePath);
        }
        if (!files_1.isDirectory(absolutePath)) {
            throw new Error(`"${absolutePath}" must be a directory`);
        }
        const appConfigBuilder = createBlankConfigGenerator();
        const aboutPage = {
            pageTitle: 'About',
            enabled: true,
            path: 'about',
            projectPath: 'pages/about',
            type: 'AboutPage',
            items: [
                {
                    title: 'Version',
                    value: '{{environment.version}}'
                }
            ]
        };
        appConfigBuilder.addPage(aboutPage);
        appConfigBuilder.addMenuForPage(aboutPage);
        const connectionPage = {
            enabled: true,
            pageTitle: 'Tap connect',
            path: 'connect',
            projectPath: 'pages/tap-connect',
            type: 'TapConnectionPage',
            protocols: {
                ble: {
                    enabled: true
                },
                mqtt: {
                    enabled: false
                },
                nfc: {
                    enabled: true
                },
                socket: {
                    enabled: true
                }
            }
        };
        appConfigBuilder.addPage(connectionPage);
        // appConfigBuilder.addMenuForPage(connectionPage);
        const devicePage = {
            enabled: true,
            pageTitle: 'Device',
            path: 'device',
            projectPath: 'pages/device',
            type: 'TapDevicePage',
            children: {}
        };
        appConfigBuilder.addPage(devicePage);
        const tapInfoPage = {
            enabled: true,
            pageTitle: 'Tap info',
            path: 'device/info',
            projectPath: 'pages/device/tap-info',
            type: 'TapInfoPage',
            items: [
                {
                    key: 'serialNumber'
                },
                {
                    key: "tapFirmwareVersion"
                },
                {
                    key: 'configVersion'
                },
                {
                    key: "appName"
                }
            ]
        };
        appConfigBuilder.getOrCreateSubPage(tapInfoPage, devicePage.type);
        appConfigBuilder.addMenuForSubPage(tapInfoPage, 'info', devicePage.type);
        appConfigBuilder.setAppId('com.schneiderelectric.masterpactmtz');
        // const tapConfigFile = createTmpFile('tapconfig.json', appConfigBuilder.tapConfig);
        const generatorConfigFile = createTmpFile('appconfig.json', {
            appConfig: appConfigBuilder.build(),
            tapConfig: appConfigBuilder.tapConfig
        });
        console.log(`Created temporary app config: ${generatorConfigFile.filepath}`);
        process.chdir(absolutePath);
        showInfo(`Working directory: ${process.cwd()}`);
        await lib_1.execIonicToolkitSchematics('new-app', {
            overwrite: true,
            name: appName,
            config: generatorConfigFile.filepath,
            dryRun: options.dryRun
        }, {
            dryRun: options.dryRun
        });
    }
}
exports.default = CreateCommand;
CreateCommand.description = 'Create ';
CreateCommand.examples = [
    `create ./app-dir`
];
CreateCommand.flags = {
    help: command_1.flags.help({ char: 'h' })
};
CreateCommand.args = [
    {
        name: 'appPath',
        required: true
    }
];
function createTmpFile(path, data) {
    const tmpPath = utility_1.getTmpFilePath('tapconfig.json');
    fs_1.mkdirSync(path_1.dirname(tmpPath), {
        recursive: true
    });
    const jsonFile = new files_1.JsonFile(tmpPath);
    jsonFile.write(data);
    return jsonFile;
}
//# sourceMappingURL=create.js.map