<?php

class PopulardirectApi extends Api
{
    public $apiName = 'populardirect';

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

    /**
     * Method GET
     * Get single record (by id)
     * http://DOMAIN/trips/1
     * @return string
     */
    public function viewAction()
    {
        return $this->response(
            array(
                'error'=> 'Data not found'
            ), 404);
    }

    public function createAction()
    {
        syslog(LOG_WARNING,'PopulardirectApi::createAction()');
        $message = 'Data not found';
        $code = 404;
        $time = $this->requestParams['time'] ?? '';
        $location = $this->requestParams['location'] ?? [];
        $country = $this->requestParams['country'] ?? '';
        $member_id = $this->requestParams['member_id'] ?? 0;
        try {
            $db = new Db();

            $country = Geocode::getCountryByGPS($db->getConnect(), $location, $country, Activity::DEFAULT_COUNTRY);
            syslog(LOG_WARNING,'country='.$country);

            // DEBUG
            //$country ='US';
            $country = Geocode::mockGPSCountry($db->getConnect(), $member_id, $country, 'default');
            
            list($location["lat"],$location["lng"]) = Geocode::mockGPSLocation(
                $db->getConnect(), $member_id, $location["lat"], $location["lng"], 'default');

            syslog(LOG_WARNING,"lat=".$location["lat"].",lng=".$location["lng"]);

            if ($country!='US' && $country!='SG') {
                throw new RuntimeException('Trips has not yet launched in your country. You can still track your travel activity and access exclusive deals. Start exploring!',500);
            }
            if ($member_id<1) {
                throw new RuntimeException('Invalid member ID',500);
            }
    
            if (!is_array($location) || !array_key_exists('lat',$location) || !array_key_exists('lng',$location)) {
                throw new RuntimeException('You did not provide you GPS location',500);
            }
            if ($location["lat"]==NULL || $location["lng"]==NULL || ($location["lat"]==0 && $location["lng"]==0)) {
                throw new RuntimeException('Invalid GPS location provided',500);
            }
            $address = NULL;
            $street_address = "";

            // Step 1. Get activities
            list($res,$err) = Populardirect::getPopularDirect(
                $db->getConnect(), $db->getConnectGPS(),
                $member_id, $location["lat"], $location["lng"], 
                $time, Populardirect::TIME_DELTA, /* last 2 parameters are not used at the moment */
                Populardirect::POPULAR_RADIUS, $country
            );
       
            return $this->response(
                array(
                    "trips" => $res,
                    "street_address" => '1'
                ), 200);
        } catch (RuntimeException $e) {
            $message = $e->getMessage();
            $code = $e->getCode();
            syslog(LOG_WARNING,$message);
        }
        return $this->response(
            array(
                "error" => $message
            ), $code);
    }

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

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

}
