<?php
namespace SabaiApps\Directories\Component\WordPressContent\LocationApi;

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Exception\RuntimeException;
use SabaiApps\Directories\Component\Location\Api\IGeolocationApi;

class GeoIpGeolocationApi implements IGeolocationApi {

	protected $_application, $_name, $_info;

	public function __construct( Application $application, $name ) {
		$this->_application = $application;
		$this->_name        = $name;
		$this->_info        = array(
			'label'            => __( 'WordPress Geolocation IP Detection plugin', 'directories-pro' ),
			'default_settings' => array(),
			'can_detect_ip'    => true,
		);
	}

	public function locationApiInfo( $key = null ) {
		return isset( $key ) ? ( isset( $this->_info[ $key ] ) ? $this->_info[ $key ] : null ) : $this->_info;
	}

	public function locationApiSettingsForm( array $settings, array $parents ) {
	}

	public function locationApiLoad( array $settings ) {
	}

	public function locationApiGeolocateIp( array $settings, $ip = null ) {
		if ( isset( $ip ) ) {
			if ( ! function_exists( 'geoip_detect2_get_info_from_ip' ) ) {
				throw new RuntimeException( 'Geolocation function geoip_detect2_get_info_from_ip not found.' );
			}
			$result = geoip_detect2_get_info_from_ip( $ip );
		} else {
			if ( ! function_exists( 'geoip_detect2_get_info_from_current_ip' ) ) {
				throw new RuntimeException( 'Geolocation function geoip_detect2_get_info_from_current_ip not found.' );
			}
			$result = geoip_detect2_get_info_from_current_ip();
		}
		if ( ! $result->location->latitude
			|| ! $result->location->longitude
		) {
			if ( ! isset( $ip ) ) {
				$ip = geoip_detect2_get_client_ip();
			}
			throw new RuntimeException( 'Failed fetching geo info from IP: ' . $ip );
		}
		$lat = $result->location->latitude;
		$lng = $result->location->longitude;
		$this->_application->logDebug( 'Geolocation result: ' . $lat . ',' . $lng );
		return array(
			'lat' => $lat,
			'lng' => $lng,
		);
	}
}
