<?php

namespace Dueper;

use Composer\Script\Event;
use Composer\Installer\PackageEvent;

class Install
{

    public static function start()
    {
        echo "Welcome to the Dueper Installer. \n";
        echo "This will install the wp and theme. \n";

        // config
        $name = strtolower(Install::getDir());
        $wd = getcwd();
        $themeReference = "dueper-theme/thor-theme";
        $thNS = "git@gitlab.com:ew2x/wp-sites/theme";
        $wpNS = "git@gitlab.com:ew2x/wp-sites/wp";
        // endconfig

        $greenprint = function ($text) {
            echo ("\033[32m" . $text . "\n \033[39m");
        };
        $redprint = function ($text) {
            echo ("\033[31m" . $text . "\n \033[39m");
        };

        if (!(file_exists($wd . "/web/app/themes/" . $name . "-theme"))) {
            //exexcute everything only if file does not exist yet

            // preliminary functions
            $prepare = function () use ($wd, $themeReference) {
                echo ("Install beginning.... \n");
                chdir($wd);
                echo shell_exec("chmod +x dueper-close.sh");
                echo shell_exec("cp ./.env.example .env");
                echo shell_exec('mkdir ./web/app/themes');
                echo ("Installing the theme \n");
                echo shell_exec("composer create-project " . $themeReference . " ./web/app/themes/dueper-theme --remove-vcs --no-secure-http --repository='http://packagist.dev.dueper.net/'");
            };
            $fixGitignore = function () use ($wd, $greenprint, $redprint) {
                echo ("Fixing the .gitignore file \n");
                if (file_exists($wd . "/.gitignore")) {
                    $gitIgnore = file_get_contents($wd . "/.gitignore");
                    $gitIgnore = $gitIgnore . PHP_EOL . "dueper/*" . PHP_EOL;
                    file_put_contents($wd . "/.gitignore", $gitIgnore);
                    $greenprint("Success");
                } else {
                    $redprint(".gitignore missing");
                }
            };
            $themeRename = function () use ($name, $wd, $greenprint, $redprint) {
                echo ("Fixing the Theme \n");
                if (file_exists($wd . "/web/app/themes/dueper-theme")) {
                    rename($wd . "/web/app/themes/dueper-theme", $wd . "/web/app/themes/" . $name . "-theme");
                    $greenprint("Theme directory successfully renamed with project name");
                } else {
                    $redprint("Theme directory not found");
                }
            };
            $themeFix = function () use ($name, $wd, $greenprint, $redprint) {
                echo ("Fixing the Theme composer.json, styles.css, package.json \n");
                $myFile = $wd . "/web/app/themes/" . $name . "-theme/composer.json";
                $myFile2 = $wd . "/web/app/themes/" . $name . "-theme/package.json";
                $myFile3 = $wd . "/web/app/themes/" . $name . "-theme/resources/style.css";
                if (file_exists($myFile)) {
                    $composerContent = file_get_contents($myFile);
                    $composerContent = str_replace("dueper-theme/thor-theme", "dueper-theme/" . $name . "-theme", $composerContent);
                    file_put_contents($myFile, $composerContent);
                    $greenprint("Theme composer.json successfully updated");
                } else {
                    $redprint("Theme composer.json missing or theme directory not found");
                }
                if (file_exists($myFile2)) {
                    $packageContent = file_get_contents($myFile2);
                    $packageContent = str_replace("thor-theme", $name . "-theme", $packageContent);
                    file_put_contents($myFile2, $packageContent);
                    $greenprint("Theme package.json successfully updated");
                } else {
                    $redprint("Theme package.json missing or theme directory not found");
                }
                if (file_exists($myFile3)) {
                    $styleContent = file_get_contents($myFile3);
                    $styleContent = str_replace("Thor", $name . " Theme", $styleContent);
                    file_put_contents($myFile3, $styleContent);
                    $greenprint("Theme style.css successfully updated");
                } else {
                    $redprint("Theme style.css missing or theme directory not found");
                }
            };
            $themeInitRepo = function () use ($name, $wd, $thNS, $greenprint, $redprint) {
                echo ("Initializing the theme Repository \n");
                if (file_exists($wd . "/web/app/themes/" . $name . "-theme")) {
                    chdir($wd . "/web/app/themes/" . $name . "-theme");
                    echo shell_exec("git init");
                    echo shell_exec("git add .");
                    echo shell_exec("git commit -m 'Automatic Commit'");
                    echo shell_exec("git push -u " . $thNS . "/" . $name . "-theme.git master");
                    echo shell_exec("git remote add origin " . $thNS . "/" . $name . "-theme.git");
                    echo shell_exec("git push -u origin master");
                    echo shell_exec("git checkout -b develop");
                    echo shell_exec("git push -u origin develop");
                    $greenprint("Theme repository correctly initialized");
                    return true;
                } else {
                    $redprint("Theme directory not found");
                    return false;
                }
            };
            $themeComposerConfig = function () use ($name, $wd, $thNS, $greenprint, $redprint) {
                echo ("Removing the theme folder \n");
                chdir($wd);
                if (file_exists($wd . "/web/app/themes/" . $name . "-theme")) {
                    echo shell_exec("rm -rf " . $wd . "/web/app/themes/" . $name . "-theme");
                    echo shell_exec("composer config repositories." . $name . "-theme git " . $thNS . "/" . $name . "-theme.git");
                    echo shell_exec("composer require dueper-theme/" . $name . "-theme dev-master");
                    $greenprint("Theme folder correctly removed");
                } else {
                    $redprint("Error: cannot config composer for theme");
                }
            };
            $wpFix = function () use ($name, $wd, $greenprint, $redprint) {
                echo ("Fixing the wp composer.json \n");
                $myFile = $wd . "/composer.json";
                if (file_exists($myFile)) {
                    $composerContent = file_get_contents($myFile);
                    $composerContent = str_replace("dueper/starter-wp", "dueper/" . $name . "-wp", $composerContent);
                    file_put_contents($myFile, $composerContent);
                    $composerContent = file_get_contents($myFile);
                    $greenprint("WP composer.json successfully updated");
                } else {
                    $redprint("CRITICAL: WP composer.json missing");
                }
            };
            $wpInitRepo = function () use ($name, $wd, $wpNS, $greenprint) {
                echo ("Inizializing the Project Repository \n");
                chdir($wd);
                echo shell_exec("git init");
                echo shell_exec("git add .");
                echo shell_exec("git commit -m 'Automatic Commit'");
                shell_exec("git push -u " . $wpNS . "/" . $name . "-wp.git master");
                echo shell_exec("git remote add origin " . $wpNS . "/" . $name . "-wp.git");
                echo shell_exec("git push -u origin master");
                echo shell_exec("git checkout -b develop");
                echo shell_exec("git push -u origin develop");
                $greenprint("WP repository correctly initialized");
            };

            $activate = function () use ($name, $wd) {
                echo ("WP Cli Starting \n");
                chdir($wd);
                $plugin_to_activate = [
                    'advanced-custom-fields-pro',
                    'svg-support'
                ];
                foreach ($plugin_to_activate as $plugin) {
                    echo shell_exec("php wp-cli.phar plugin activate " . $plugin);
                }
                echo shell_exec("php wp-cli.phar theme activate " . $name . "-theme");
            };

            $prepare(); //preliminary functions
            // $fixGitignore(); //puts the installer into the gitignore
            $themeRename(); //rename dueper-theme into myproject-theme
            $themeFix(); //changes style.css, composer.json and package.json in the theme files
            $themeOk = $themeInitRepo(); //sets the remote repository with current theme
            if ($themeOk) $themeComposerConfig(); //renove the theme directory, composer config, composer require
            $wpFix(); //changes composer.json in the root 
            $wpInitRepo(); //sets the remote repository with current wp
            // $activate(); //sets the remote repository with current wp
        }
    }

    public static function getDir()
    {
        $path = "..";
        $latestTime = 0;
        $dir = dir($path);

        while (false !== ($entry = $dir->read())) {
            $filepath = "{$path}/{$entry}";

            if (is_dir($filepath) && ($entry !== ".") && filectime($filepath) > $latestTime) {
                $latestTime = filectime($filepath);
                $latestDir = $entry;
            }
        }
        return $latestDir;
    }
}
