// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

import * as path from "path";
import * as Q from "q";

import {ErrorHelper} from "../../common/error/errorHelper";
import {PlistBuddy} from "./plistBuddy";
import {Log} from "../../common/log/log";
import {FileSystem} from "../../common/node/fileSystem";
import {ChildProcess} from "../../common/node/childProcess";

import {TelemetryHelper} from "../../common/telemetryHelper";

export class SimulatorPlist {
    private projectRoot: string;

    private nodeFileSystem: FileSystem;
    private plistBuddy: PlistBuddy;
    private nodeChildProcess: ChildProcess;

    constructor(projectRoot: string, {
        nodeFileSystem = new FileSystem(),
        plistBuddy = new PlistBuddy(),
        nodeChildProcess = new ChildProcess()
    } = {}) {
        this.projectRoot = projectRoot;

        this.nodeFileSystem = nodeFileSystem;
        this.plistBuddy = plistBuddy;
        this.nodeChildProcess = nodeChildProcess;
    }

    public findPlistFile(): Q.Promise<string> {

        return Q.all<any>([
            this.plistBuddy.getBundleId(this.projectRoot), // Find the name of the application
            this.nodeChildProcess.exec("xcrun simctl getenv booted HOME").outcome]) // Find the path of the simulator we are running
            .spread((bundleId: string, pathBuffer: Buffer) => {
                const pathBefore = path.join(pathBuffer.toString().trim(), "Containers", "Data", "Application");
                const pathAfter = path.join("Library", "Preferences", `${bundleId}.plist`);

                // Look through $SIMULATOR_HOME/Containers/Data/Application/*/Library/Preferences to find $BUNDLEID.plist
                return this.nodeFileSystem.readDir(pathBefore).then((apps: string[]) => {
                    const plistCandidates = apps.map((app: string) => path.join(pathBefore, app, pathAfter)).filter(this.nodeFileSystem.existsSync);
                    if (plistCandidates.length === 0) {
                        throw new Error(`Unable to find plist file for ${bundleId}`);
                    } else if (plistCandidates.length > 1) {
                        TelemetryHelper.sendSimpleEvent("multipleDebugPlistFound");
                        Log.logWarning(ErrorHelper.getWarning("Multiple plist candidates found. Application may not be in debug mode."));
                    }

                    return plistCandidates[0];
                });
            });
    }
}