<?php

namespace App\Actions\Config;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use App\Services\ApiService;
use App\View\Pages;

class ConfigAction
{

    private $config;
    private $path;
    private ApiService $rest;
    private $call_status;
    private Pages $view;
    
    public function __construct($config, ApiService $api, Pages $view)
    {
	$this->config = $config;
	$this->path = $config['config_path'];
	$this->rest = $api;
	$this->call_status = $config['call_status'];
	$this->view = $view;
    }

    public function get(ServerRequestInterface $request,
			ResponseInterface $response, array $args): ResponseInterface
    {
	$config = $this->rest->config();
	return $this->view->render($request, $response, 'config.html', [
	    'data' => $this->config,
	    'config' => $config
	]);
    }

    public function save(ServerRequestInterface $request,
			 ResponseInterface $response, array $args): ResponseInterface
    {
	$params = $request->getParsedBody();
	if(isset($params['local_config'])){
	    $config_file = fopen("$this->path/config.php", "w") or die("Unable to open file!");
	    fwrite($config_file, "<?php\n");
	    $params = $request->getParsedBody();
	    fwrite($config_file, '$'."config['ldap']['host'] = \"" . $params['host']. "\";\n");
	    fwrite($config_file, '$'."config['ldap']['rdn'] = \"" . $params['rdn'] . "\";\n");
	    fwrite($config_file, '$'."config['ldap']['pass'] = \"" . $params['pass'] . "\";\n");
	    fwrite($config_file, '$'."config['ldap']['base'] = \"" . $params['base'] . "\";\n");
	    fwrite($config_file, '$'."config['media']['folder'] = \"" . $params['folder'] . "\";\n");
	    fwrite($config_file, '$'."config['rest']['server'] = \"" . $params['server'] . "\";\n");
	    fwrite($config_file, '$'."config['defaults']['parallel'] = \"" . $params['parallel'] . "\";\n");
	    fwrite($config_file, '$'."config['defaults']['repeate'] = \"" . $params['repeate'] . "\";\n");
	    fwrite($config_file, '$'."config['defaults']['wait'] = \"" . $params['wait'] . "\";\n");
	    fwrite($config_file, '$'."config['defaults']['number_from'] = \"" . $params['number_from'] . "\";\n");
	    fwrite($config_file, '$'."config['defaults']['number_record'] = \"" . $params['number_record'] . "\";\n");
	    fwrite($config_file, '$'."config['defaults']['phrase'] = \"" . $params['phrase'] . "\";\n");
	    if (isset($params['pattern'])){
		fwrite($config_file, '$'."config['defaults']['pattern'] = \"" . $params['pattern'] . "\";\n");
	    } else {
		fwrite($config_file, '$'."config['defaults']['pattern'] = \"\";\n");
	    }
	    foreach($this->call_status as $code => $status){
		fwrite($config_file, '$'."config['call_status']['$code'] = \""
				   . $status . "\";\n");
	    }
	    $data['ldap']['host'] = $params['host'];
	    $data['ldap']['rdn'] = $params['rdn'];
	    $data['ldap']['pass'] = $params['pass'];
	    $data['ldap']['base'] = $params['base'];

	    $data['media']['folder'] = $params['folder'];

	    $data['rest']['server'] = $params['server'];

	    fclose($config_file);
	} elseif (isset($params['fsapi_config'])){
	    $data['media_folder'] = $params['media_folder'];
	    $data['host_in'] = $params['host_in'];
	    $data['port_in'] = $params['port_in'];
	    $data['pass'] = $params['pass'];
	    $data['port'] = $params['port'];
	    $data['host'] = $params['host'];
	    $data['gw'] = $params['gw'];
	    $data['pattern'] = $params['pattern'];
	    
	    $config = $this->rest->set_config($data);
	}
	return $response->withStatus(302)->withHeader('Location', '/ui/config');
	/* return $view->render($response, 'config.html', [
	   'data' => $data
	   ]); */
    }

    public function getAnswerCode(ServerRequestInterface $request,
				  ResponseInterface $response, array $args): ResponseInterface
    {
	/* $this->set_message("sakdjhk kjshadkjh"); */
	/* echo var_dump($this->config); */
	return $this->view->render($request, $response, 'answer_code.html', [
	    'call_status' => $this->config['call_status'],
	]);
    }
    
    public function setAnswerCode(ServerRequestInterface $request,
				  ResponseInterface $response, array $args): ResponseInterface
    
    {
	$params = $request->getParsedBody();
	/* echo var_dump($this->path); */
	$config_file = fopen("$this->path/config.php", "w") or die("Unable to open file!");
	fwrite($config_file, "<?php\n");
	fwrite($config_file, '$'."config['ldap']['host'] = \"" . $this->config['ldap']['host']. "\";\n");
	fwrite($config_file, '$'."config['ldap']['rdn'] = \"" . $this->config['ldap']['rdn'] . "\";\n");
	fwrite($config_file, '$'."config['ldap']['pass'] = \"" . $this->config['ldap']['pass'] . "\";\n");
	fwrite($config_file, '$'."config['ldap']['base'] = \"" . $this->config['ldap']['base'] . "\";\n");
	fwrite($config_file, '$'."config['media']['folder'] = \"" . $this->config['media']['folder']
			   . "\";\n");
	fwrite($config_file, '$'."config['rest']['server'] = \"" . $this->config['rest']['server']
			   . "\";\n");
	fwrite($config_file, '$'."config['defaults']['parallel'] = \"" . $this->config['defaults']['parallel']
			   . "\";\n");
	fwrite($config_file, '$'."config['defaults']['repeate'] = \""
			   . $this->config['defaults']['repeate'] . "\";\n");
	fwrite($config_file, '$'."config['defaults']['wait'] = \""
			   . $this->config['defaults']['wait'] . "\";\n");
	fwrite($config_file, '$'."config['defaults']['number_from'] = \""
			   . $this->config['defaults']['number_from'] . "\";\n");
	fwrite($config_file, '$'."config['defaults']['number_record'] = \""
			   . $this->config['defaults']['number_record'] . "\";\n");
	fwrite($config_file, '$'."config['defaults']['phrase'] = \""
			   . $this->config['defaults']['phrase'] . "\";\n");

	unset($params['key']);
	unset($params['value']);
	unset($params['call_status']);
	foreach($params as $code => $status){
	    fwrite($config_file, '$'."config['call_status']['$code'] = \""
			       . $status . "\";\n");
	}
	
	fclose($config_file);
	/* echo var_dump($params); */
	return $response->withStatus(302)->withHeader('Location', '/ui/config/answer_code');
    }
}
