#!/usr/bin/env php
<?php

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

require __DIR__.'/bootstrap/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';

$app->command('initialize [--context=] relative_feature_file_path', function (
    OutputInterface $output,
    GD\GherkinToDusk $gd, $context, $relative_feature_file_path) {

    //get source path of file
    $output->write("\nCreating File for you\n");

    try {
        $gd->setContext($context)
            ->setPathToFeature($relative_feature_file_path)
            ->initializeFeature();

        $path = $gd->fullPathToDestinationFile();
        $output->write(sprintf("File now in %s for you to work on", $path));

        $output->write("\n****done*****\n");
    } catch (\Exception $e) {
        $output->write(sprintf("Error initializing \nMESSAGE: %s\n", $e->getMessage()));
    }

})->defaults([
    'context' => "domain"
])->descriptions("This will stuff out your feature into a Dusk Test file",
    [
        '--context' => "domain or ui this will help us to know what Dusk test to run domain is the inside code ui is the outside",
        "relative_feature_file_path" => "The path to the file for example tests/features/foo.feature"
    ]
);


$app->command('append [--context=] relative_feature_file_path', function (
    OutputInterface $output,
    GD\GherkinToDusk $gd, $context, $relative_feature_file_path) {

//get source path of file
    $output->write("\nAppending File for you\n");

    try {
        $gd->setContext($context)
            ->setPathToFeature($relative_feature_file_path)
            ->appendFeatures();

        $path = $gd->fullPathToDestinationFile();
        $output->writeln(sprintf("File now updated in %s for you to work on", $path));
    } catch (\Exception $e) {
        $output->write(sprintf("Error initializing \nMESSAGE: %s\n", $e->getMessage()));
    }

})->defaults([
    'context' => "domain"
])->descriptions("This will append new Scenarios and Steps from the feature file",
    [
        '--context' => "domain or ui this will help us to know what Dusk test to run domain is the inside code ui is the outside",
        "relative_feature_file_path" => "The path to the file for example tests/features/foo.feature"
    ]
);

$app->command('run [--context=] relative_feature_file_path', function (
    OutputInterface $output,
    \Illuminate\Filesystem\Filesystem $files, $context, $relative_feature_file_path) use ($app) {

    if($context == 'domain') {
        $run = new \GD\RunTest($app);

        $run->handleDomain($relative_feature_file_path);

    } else {
        $run = new \GD\RunTest($app);

        $run->handleBrowser($relative_feature_file_path);
    }

})->defaults([
    'context' => "domain"
])->descriptions("This will stuff out your feature into a Dusk Test file",
    [
        '--context' => "domain or ui this will help us to know what Dusk test to run domain is the inside code ui is the outside",
        "relative_feature_file_path" => "The path to the file for example tests/features/foo.feature"
    ]
);


$app->run();