<?php

namespace Krik\Composer;

use Composer\Script\Event;

class KrikComposer
{
	/**
	 * Init project
	 */
	public static function init(Event $event)
	{
		$io = $event->getIO();
		$dev = $event->isDevMode();
		$new = self::isNew();

		if ($new) {
			self::isInteractive($io);
			self::environment($io);
			self::renaming($io);
		}

		self::copying($io, $dev);
		self::done($io, $new);
	}

	/**
	 * Check if project is new
	 */
	private static function isNew()
	{
		$krikwp = strpos(file_get_contents('package.json'), 'krikwp');
		if ($krikwp != false) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Check if terminal is interactive
	 */
	private static function isInteractive($io)
	{
		if (!$io->isInteractive()) {
			$io->write('<info>Please use different terminal</info>');
			die;
		}
	}

	/**
	 * Create .env with salts
	 * @see wp-cli.org
	 * @see github.com/aaemnnosttv/wp-cli-dotenv-command
	 */
	private static function environment($io)
	{
		$io->write('<info>Creating .env with salts</info>');
		exec('wp dotenv init --template=vendor/krikstudio/wpkrik/.env.example --with-salts');
	}

	/**
	 * Renaming boilerplate
	 */
	private static function renaming($io)
	{
		$boilerplate = [
			'name' => 'KrikWP',
			'slug' => 'krikwp',
			'desc' => "Krikstudio's WordPress Boilerplate"
		];

		$io->write('<info>Please enter project information</info>');
		$project = [
			'name' => $io->ask('<info>Name [<comment>' . $boilerplate['name'] . '</comment>]:</info> ', $boilerplate['name']),
			'slug' => $io->ask('<info>Slug [<comment>' . $boilerplate['slug'] . '</comment>]:</info> ', $boilerplate['slug']),
			'desc' => $io->ask('<info>Description [<comment>' . $boilerplate['desc'] . '</comment>]:</info> ', $boilerplate['desc'])
		];

		$io->write('<info>Renaming files</info>');

		file_put_contents('.env', str_replace($boilerplate, $project, file_get_contents('.env')));

		file_put_contents('composer.json', str_replace($boilerplate, $project, file_get_contents('composer.json')));

		file_put_contents('package.json', str_replace($boilerplate, $project, file_get_contents('package.json')));

		file_put_contents('README.md', '# ' . $project['name']);
		file_put_contents('README.md', PHP_EOL . $project['desc'], FILE_APPEND);

		file_put_contents('.gitignore', str_replace($boilerplate, $project, file_get_contents('.gitignore')));

		file_put_contents('public/app/themes/krikwp/style.css', str_replace($boilerplate, $project, file_get_contents('public/app/themes/krikwp/style.css')));

		rename('public/app/themes/krikwp', 'public/app/themes/' . $project['slug']);
	}

	/**
	 * Copying files
	 */
	private static function copying($io, $dev)
	{
		$io->write('<info>Copying files</info>');

		if (!file_exists('public/app/mu-plugins/autoloader.php')) {
			copy('vendor/krikstudio/wpkrik/autoloader/autoloader.php', 'public/app/mu-plugins/autoloader.php');
		}

		if (!file_exists('.editorconfig') && $dev) {
			copy('vendor/krikstudio/wpkrik/linters/.editorconfig', '.editorconfig');
		}
		if (!file_exists('.eslintrc') && $dev) {
			copy('vendor/krikstudio/wpkrik/linters/.eslintrc', '.eslintrc');
		}
		if (!file_exists('.stylelintrc') && $dev) {
			copy('vendor/krikstudio/wpkrik/linters/.stylelintrc', '.stylelintrc');
		}
		if (!file_exists('ruleset.xml') && $dev) {
			copy('vendor/krikstudio/wpkrik/linters/ruleset.xml', 'ruleset.xml');
		}

		if (!file_exists('.babelrc') && $dev) {
			copy('vendor/krikstudio/wpkrik/webpack/.babelrc', '.babelrc');
		}
		if (!file_exists('webpack.mix.js') && $dev) {
			copy('vendor/krikstudio/wpkrik/webpack/webpack.mix.js', 'webpack.mix.js');
		}

		if (!file_exists('wp-cli.yml') && $dev) {
			copy('vendor/krikstudio/wpkrik/wp-cli/wp-cli.yml', 'wp-cli.yml');
		}
	}

	/**
	 * Init done
	 */
	private static function done($io, $new)
	{
		if ($new) {
			$io->write('<info>New project successfully created</info>');
		} else {
			$io->write('<info>Project has already been created</info>');
		}
	}

	/**
	 * Set permissions
	 */
	public static function permissions(Event $event)
	{
		$io = $event->getIO();
		$io->write('<info>Setting up permissions</info>');
		exec('find * -type d -print0 | xargs -0 chmod 0755');
		exec('find public/ -type f -print0 | xargs -0 chmod 0644');
		$io->write('<info>Permissions set</info>');
	}
}
