<?php 
class agile_ticketing_location_services {
    static function agile_collect_locationData()
    {
        $domain = get_option('agile_field_domain');
        $appKey = "2b33e881-9a22-4963-8373-23acc6ca9d85";/*get_option('agile_field_appKey');*/
        
        if ($appKey == false || $domain == false) {
            return false;
        }

        $url = '';

        //$hostURL = get_site_url();
        //agile_tix::agile_tix_log_event('agile_collect_locationData $hostURL' . $hostURL);
        if ( str_contains($domain, "QA/")   /*$hostURL == "https://wp-qa.agiletix.com"*/) {
            $domain = str_replace("QA/", "",$domain );
            $url = "https://qa.agiletix.com/ATSLocation/Domain.svc/JSON/GetDomainServices?appkey=" . urlencode($appKey) . "&domain=" . urlencode($domain);
        } else {
            $url = "https://prod1.agileticketing.net/ATSLocation/Domain.svc/JSON/GetDomainServices?appkey=" . urlencode($appKey) . "&domain=" . urlencode($domain);
        }

        //$url = "https://dev.agiletix.net/Location/Domain.svc/JSON/GetDomainServices?appkey=" . urlencode($appKey) . "&domain=" . urlencode($domain);
        //$url = "https://qa.agiletix.com/ATSLocation/Domain.svc/JSON/GetDomainServices?appkey=" . urlencode($appKey) . "&domain=" . urlencode($domain);
        
            
        $response = wp_remote_get($url);
        if (is_wp_error($response)) {
            agile_tix::agile_tix_log_event('agile_collect_locationData $response error: ' . $response);
        } else {
            //agile_tix::agile_tix_log_event('agile_collect_locationData $response sucess: ' . json_encode($response));
        }

        if ($response || is_array($response)) {
            if (array_key_exists('body', $response)) {
                $body = json_decode($response['body'], true);
                if (is_wp_error($body)) {
                    return false;
                } else {
                    //agile_tix::agile_tix_log_event('$body: ' . $body);
                    $locData = [];
                    if (array_key_exists('APIURL', $body)) {
                        $locData['APIURL'] = $body['APIURL'];
                    }
                    if (array_key_exists('WebURL', $body)) {
                        $locData['WebURL'] = $body['WebURL'];
                    }
                    //agile_tix::agile_tix_log_event('$locData: ' . json_encode($locData));
                    return $locData;
                }
            }
        }
        return false;
    }
}