<?php

namespace openkm\impl;


use Httpful\Exception\ConnectionErrorException;
use Httpful\Mime;
use Httpful\Request;
use openkm\bean\MapParam;
use openkm\definition\BasePlugin;
use openkm\exception\UnknowException;
use openkm\util\Status;
use openkm\util\UriHelper;
use openkm\bean\Plugin;

class PluginImpl extends ClientImpl implements BasePlugin {

    public function executePluginPost($className, $parameters, $content, $type = Plugin::TYPE_STRING) {
        try {
            $uri = UriHelper::getUri($this->host, UriHelper::PLUGIN_POST_EXECUTE_PLUGIN);

            $params = [];

            $param1 = [];
            $param1['name'] = 'className';
            $param1['body'] = $className;
            $params[] = $param1;

            foreach ($parameters as $key => $value) {
                $param = new MapParam();
                $param->setKey($key);
                $param->setValue($value);

                $param2 = [];
                $param2['name'] = 'parameter';
                $param2['body'] = json_encode($param);
                $params[] = $param2;
            }

            $files = [];
            if ($content != null && $content !== '') {
                $file = [];
                $file['name'] = 'content';
                $file['body'] = $content;
                $file['fileName'] = '';
                $files[] = $file;
            }

            $body = [];
            $body['params'] = $params;
            $body['files'] = $files;

            $client = Request::post($uri);
            $client->setCurlPostFile(true);
            $client->sends(Mime::UPLOAD);
            $client->body($body);
            $client->addHeader('Authorization', $this->authorizationToken);
            if ($type === Plugin::TYPE_STRING) {
                $client->expects(Mime::HTML);
            } else {
                $client->expects(Mime::JSON);
            }
            $response = $client->send();
            if ($response->code == Status::OK) {
                return $response->body;
            } else {
                $restException = new \openkm\bean\RestException();
                if ($type === Plugin::TYPE_STRING) {
                    $restException = $this->phpRestException(json_decode($response->body));
                } else {
                    $restException = $this->phpRestException($response->body);
                }

                if ($restException->getSimpleName() != '') {
                    throw new UnknowException($restException->getMessage());
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    public function executePluginPostReturnFile($className, $parameters, $content) {
        try {
            $uri = UriHelper::getUri($this->host, UriHelper::PLUGIN_POST_EXECUTE_PLUGIN_RETURN_FILE);
            $params = [];

            $param1 = [];
            $param1['name'] = 'className';
            $param1['body'] = $className;
            $params[] = $param1;

            foreach ($parameters as $key => $value) {
                $param = new MapParam();
                $param->setKey($key);
                $param->setValue($value);

                $param2 = [];
                $param2['name'] = 'parameter';
                $param2['body'] = json_encode($param);
                $params[] = $param2;
            }

            $files = [];
            if ($content != null && $content !== '') {
                $file = [];
                $file['name'] = 'content';
                $file['body'] = $content;
                $file['fileName'] = '';
                $files[] = $file;
            }

            $body = [];
            $body['params'] = $params;
            $body['files'] = $files;

            $client = Request::post($uri);
            $client->setCurlPostFile(true);
            $client->sends(Mime::UPLOAD);
            $client->body($body);
            $client->addHeader('Authorization', $this->authorizationToken);
            $client->expects(Mime::UPLOAD);
            $response = $client->send();
            if ($response->code == Status::OK) {
                return $response->body;
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException(json_decode($response->body));
                if ($restException->getSimpleName() != '') {
                    throw new UnknowException($restException->getMessage());
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    public function executePluginGet($className, $parameters = [], $type = Plugin::TYPE_STRING) {
        try {
            $uri = UriHelper::getUri($this->host, UriHelper::PLUGIN_GET_EXECUTE_PLUGIN);
            $uri .= '?className=' . urlencode($className);
            foreach ($parameters as $key => $value) {
                $param = new MapParam();
                $param->setKey($key);
                $param->setValue($value);
                $uri .= '&param=' . urlencode(json_encode($param));
            }
            $client = Request::get($uri);
            $client->sends(Mime::JSON);
            $client->addHeader('Authorization', $this->authorizationToken);
            if ($type === Plugin::TYPE_STRING) {
                $client->expects(Mime::HTML);
            } else {
                $client->expects(Mime::JSON);
            }
            $response = $client->send();
            if ($response->code == Status::OK) {
                return $response->body;
            } else {
                $restException = new \openkm\bean\RestException();
                if ($type === Plugin::TYPE_STRING) {
                    $restException = $this->phpRestException(json_decode($response->body));
                } else {
                    $restException = $this->phpRestException($response->body);
                }
                if ($restException->getSimpleName() != '') {
                    throw new UnknowException($restException->getMessage());
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    public function executePluginGetReturnFile($className, $parameters) {
        try {
            $uri = UriHelper::getUri($this->host, UriHelper::PLUGIN_GET_EXECUTE_PLUGIN_RETURN_FILE);
            $uri .= '?className=' . urlencode($className);
            foreach ($parameters as $key => $value) {
                $param = new MapParam();
                $param->setKey($key);
                $param->setValue($value);
                $uri .= '&param=' . urlencode(json_encode($param));
            }
            $client = Request::get($uri);
            $response = $this->getClientWithUploadResponse($client);
            if ($response->code == Status::OK) {
                return $response->body;
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException(json_decode($response->body));
                if ($restException->getSimpleName() != '') {
                    throw new UnknowException($restException->getMessage());
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }
}