<?php // vim:set ts=4 sw=4 et:

namespace Mall\Framework\SearchClient\Transport;

/**
 * This file is part of the ElasticSearch PHP client
 *
 * (c) Raymond Julin <raymond.julin@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

abstract class Base
{

    /**
     * What host to connect to for server
     * @var string
     */
    protected $host = "";

    /**
     * Port to connect on
     * @var int
     */
    protected $port = 9200;

    /**
     * ElasticSearch index
     * @var string
     */
    protected $index;

    /**
     * ElasticSearch document type
     * @var string
     */
    protected $type;

    /**
     * Default constructor, just set host and port
     * @param string $host
     * @param int $port
     */
    public function __construct($host, $port)
    {
        $this->host = $host;
        $this->port = $port;
    }

    /**
     * Method for indexing a new document
     *
     * @param array|object $document
     * @param mixed $id
     * @param array $options
     */
    abstract public function index($document, $id = false, array $options = array());

    /**
     * Perform a request against the given path/method/payload combination
     * Example:
     * $es->request('/_status');
     *
     * @param string|array $path
     * @param string $method
     * @param array|bool $payload
     * @return
     */
    abstract public function request($path, $method = "GET", $payload = false, $buildPath = true);

    /**
     * Delete a document by its id
     * @param mixed $id
     * @param array $options
     */
    abstract public function delete($id = false, array $options = array());

    /**
     * Perform a search based on query
     * @param array|string $query
     * @param array|string $options
     */
    abstract public function search($query, array $options = array());

    abstract public function scrollSearch($query, $scroll = '10s', $size = 2000, array $options = array());

    abstract public function batchUpdateFieldVaule($data, $query);

    abstract public function createBase($index, $mappings);

    abstract public function deleteBase($index);

    /**
     * Search
     *
     * @return array
     * @param mixed $query String or array to use as criteria for delete
     * @param array $options Parameters to pass to delete action
     * @throws \Mall\Framework\Search
     */
    public function deleteByQuery($query, array $options = array())
    {
        throw new \Mall\Framework\SearchClient\Exception(__FUNCTION__ . ' not implemented for ' . __CLASS__);
    }

    /**
     * Set what index to act against
     * @param string $index
     */
    public function setIndex($index)
    {
        $this->index = $index;
    }

    /**
     * Set what document types to act against
     * @param string $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * Build a callable url
     *
     * @return string
     * @param array|bool $path
     * @param array $options Query parameter options to pass
     */
    protected function buildUrl($path = false, array $options = array())
    {
        $isAbsolute = (is_array($path) ? isset($path[0][0])?$path[0][0] : '' : $path[0]) === '/';
        $url = $isAbsolute ? '' : "/" . $this->index;

        if ($path && is_array($path) && count($path) > 0)
            $url .= "/" . implode("/", array_filter($path));
        if (substr($url, -1) == "/")
            $url = substr($url, 0, -1);
        if (count($options) > 0)
            $url .= "?" . http_build_query($options, '', '&');

        return $url;
    }

}
