<?php

class CompareApi extends Api
{
    public $apiName = 'compare';
    const COMPARISONS = array(
        "youVsOthersOnCategory" => 1
    );

    public function indexAction()
    {
        return $this->response(
            array(
                'error' => 'Data not found'
            ), 404);
    }

    /**
     * Method GET
     * Get single record (by id)
     * http://DOMAIN/address/1
     * @return string
     */
    public function viewAction()
    {
        //id must be the first parameter after /address/x
        $id = array_shift($this->requestUri);

        if($id && (int)$id>0){
            /*$db = new Db();
            $address = Address::getAddressById($db->getConnect(), (int)$id);
            if(is_array($address) && count($address)>0){
                return $this->response($address, 200);
            }*/
        }
        return $this->response(
            array(
                'error'=> 'Data not found'
            ), 404);
    }

    public function createAction()
    {
        $message = "Data not found";
        $type = (string)$this->requestParams["type"] ?? "";
        $data = $this->requestParams["data"] ?? array();
        if ($type!='' && is_array($data) && self::COMPARISONS[$type]==1) {
            $db = new Db();
            $results = call_user_func("Compare::${type}", 
                $db->getConnect(), $db->getConnectGPS(), $data);
            if($results!=NULL && is_array($results) && count($results)>0){
                return $this->response(
                    array(
                        'type' => $type,
                        'data' => $data,
                        'comparison' => $results
                    ), 200);
            } else {
                $message = "Comparison data is not found";
            }
        } else {
            $message = "Invalid input";
        }
        return $this->response(
            array(
                "error" => $message
            ), 404);
    }

    public function updateAction()
    {
        return $this->response(
            array(
                "error" => "Update error"
            ), 400);
    }

    public function deleteAction()
    {
        return $this->response(
            array(
                "error" => "Delete error"
            ), 500);
    }
}
