<?php

namespace CloudreveInstaller;

use Composer\Script\Event;

class Installer{

	public static function startInstall(Event $event){
		$version = json_decode(file_get_contents("application/version.json"),true)["version"];
		$ioContext = $event->getIO();
		$welcomMsg = "
   ___ _                 _                    
  / __\ | ___  _   _  __| |_ __ _____   _____ 
 / /  | |/ _ \| | | |/ _` | '__/ _ \ \ / / _ \
/ /___| | (_) | |_| | (_| | | |  __/\ V /  __/
\____/|_|\___/ \__,_|\__,_|_|  \___| \_/ \___|
		
                 Ver $version
================================================
";
		$ioContext->write($welcomMsg);
		self::prepareStatic($event);
		$sqlInfo = self::getSqlInformation($event);
		$ioContext->write("");
		$siteUrl=$ioContext->ask("The full-url to access to your Cloudreve (e.g. https://pan.aoaoao.me/ , 'http' must be included in the front and '/' must be included at the end):");
		$ioContext->write("");
		if(!file_exists('mysql.sql')){
			$ioContext->writeError("[Error] The file mysql.sql not exist.\nInstaller will exit.To retry, run 'composer install'");
			exit();
		}
		$sqlSource = file_get_contents('mysql.sql');
		$sqlSource = str_replace("https://cloudreve.org/", $siteUrl, $sqlSource);
		$mysqli = @new \mysqli($sqlInfo["hostname"], $sqlInfo["username"], $sqlInfo["password"],  $sqlInfo["database"],  $sqlInfo["hostport"]);
		$ioContext->write("=======================");
		$ioContext->write("Starting import sql file...");
		if ($mysqli->multi_query($sqlSource)) {
			$ioContext->write("Writing complete.");
			$ioContext->write("Writing database.php...");
			if(file_exists('application/database.php')){
				$ioContext->writeError("[Error] The file database.php already exist.\nInstaller will exit.To retry, run 'composer install'");
				$ioContext->write("=======================");
				exit();
			}
			self::writrConfig($event,$sqlInfo);
			$ioContext->write("=======================");
		}else{
			$ioContext->writeError("[Error] Writing failed.Installer will exit. To retry, run 'composer install'");
			$ioContext->write("=======================");
		}
		$ioContext->write("");
		$ioContext->write("Congratulations! Cloudreve has been installed successfully.");
		$ioContext->write("");
		$ioContext->write("Here's some informatioin about yor Cloudreve:");
		$ioContext->write("Homepage: $siteUrl");
		$ioContext->write("Admin Panel: ".$siteUrl."Admin");
		$ioContext->write("Default username: admin@cloudreve.org");
		$ioContext->write("Default password: admin");
		$ioContext->write("");
		$ioContext->write("=======================");
		$ioContext->write("IMPORTANT! You may still have to configure the URL Rewrite to set everthing to work.");
		$ioContext->write("Refer to the install manual for more informatioin.");
		$ioContext->write("=======================");
		self::sendFeedBack($siteUrl);
	}

	public static function prepareStatic(Event $event){
		$ioContext = $event->getIO();
		$ioContext->write("Fetching static files from cloudreve.org...");
		if(!is_writable("runtime") || !is_writable("static")){
			$ioContext->writeError("[Error] 'runtime' or 'static' directory is not writable. To retry, run 'composer install'");
			exit();
		}
		if(file_exists("runtime/static.zip")){
			$ioContext->writeError("[Error] 'runtime/static.zip' already exist. To retry, run 'composer install'");
			exit();
		}
		if(file_exists("runtime/static")){
			$ioContext->writeError("[Error] directory 'runtime/static' already exist. To retry, run 'composer install'");
			exit();
		}
		try{
			file_put_contents("runtime/static.zip",file_get_contents("https://cloudreve.org/download.php?newestStatic"));
		}catch (Exception $e) {
			$ioContext->writeError("[Error] Failed to download static file. To retry, run 'composer install'");
			exit();
		}
		$ioContext->write("Unzip static files...");
		$zip = new \ZipArchive;
		$res = $zip->open('runtime/static.zip');
		if ($res === TRUE) {
		$zip->extractTo('runtime/static/');
		$zip->close();
		}else {
			$ioContext->writeError("[Error] Failed to open runtime/static.zip. To retry, run 'composer install'");
			exit();
		}
		self::recurse_copy("runtime/static/build/static", "static");
	}

	public static function recurse_copy($src,$dst) {
		$dir = opendir($src);
		@mkdir($dst);
		while(false !== ( $file = readdir($dir)) ) {
			if (( $file != '.' ) && ( $file != '..' )) {
				if ( is_dir($src . '/' . $file) ) {
					self::recurse_copy($src . '/' . $file,$dst . '/' . $file);
				}else {
					copy($src . '/' . $file,$dst . '/' . $file);
				}
			}
		}
		closedir($dir);
	}

	 public static function writrConfig(Event $event,$sqlInfo){
		$ioContext = $event->getIO();
		 try {
			 $fileContent = file_get_contents("CloudreveInstaller/database_sample.php");
			 $replacement = array(
				'{hostname}' => $sqlInfo["hostname"],
				'{database}' => $sqlInfo["database"],
				'{username}' => $sqlInfo["username"],
				'{password}' => $sqlInfo["password"],
				'{hostport}' => $sqlInfo["hostport"],
				);
			$fileContent = strtr($fileContent,$replacement);
			file_put_contents('application/database.php',$fileContent);
		}catch (Exception $e) {
			$ioContext->writeError("[Error] Writing failed.Installer will exit. To retry, run 'composer install'");
		}
		$ioContext->write("Writing complete.");
	 }

	public static function getSqlInformation(Event $event){
		$ioContext = $event->getIO();
		$hostname=$ioContext->ask("Input the hostname of your MySQL server (Default:127.0.0.1):","127.0.0.1");
		$database=$ioContext->ask("The database name:","127.0.0.1");
		$username=$ioContext->ask("The username of your MySQL server (Default:root):","root");
		$password=$ioContext->ask("The password of your MySQL server:");
		$hostport=$ioContext->ask("The hostport of your MySQL server (Default:3306):","3306");
		$mysqli = @new \mysqli($hostname, $username,  $password,  $database,  $hostport);
		if ($mysqli->connect_error) {
			$ioContext->writeError("[Error] Cannot connect to MySQL server, Message:".$mysqli->connect_error);
			$ioContext->write("");
			$ioContext->write("Please confirm your connection informatioin:");
			@$mysqli->close();
			return self::getSqlInformation($event);
		}
		return [
			"hostname" =>  $hostname,
			"database" =>  $database,
			"username" =>  $username,
			"password" =>  $password,
			"hostport" =>  $hostport,
		];
	}

	public static function sendFeedBack($url){
		@file_get_contents("http://toy.aoaoao.me/feedback.php?url=".urlencode($url));
	}

}
?>