<?php
/*
Plugin Name: CPR Rescue
Plugin URI:  https://www.carolinapoodlerescue.org
Description: Pulls available and SOS dog listings from RescueGroups.org for Carolina Poodle Rescue
Version:     2.0
Author:      John M Reel
Author URI:  https://www.carolinapoodlerescue.org
*/

require_once plugin_dir_path(__FILE__) . 'cpr_module.php';
require_once plugin_dir_path(__FILE__) . 'templates.php';

// ===========================================================================================================
// Public functions in cpr_module.php:
// ===========================================================================================================
// generatePage( animalID )  - generates a detail page for a dog, routing to CPR or SOS based on status
// getSOSDetail( animalID )  - fetches detail and parses description for SOS dogs
// getCPRDetail( animalID )  - fetches detail and parses description for CPR dogs
// getSOSList()              - get a list of SOS dogs - returns an array of dogs
// getCPRList()              - get a list of CPR dogs for adoption - returns an array
// getSize(maxSize, x, y)    - returns image size based on ratio scaling largest dim to maxSize
// filterSOSName(name)       - strips the SOS prefix from an animal name
// debugObject(variable)     - dumps the contents of a variable to the screen
// ===========================================================================================================

add_shortcode('sos-dogs',   'sosDogs');
add_shortcode('cpr-dogs',   'cprDogs');
add_shortcode('dog-detail', 'dogDetail');

// Enqueue plugin stylesheet and Bootstrap Icons
add_action('wp_enqueue_scripts', 'cprEnqueueAssets');

function cprEnqueueAssets() {
    wp_enqueue_style(
        'bootstrap-icons',
        'https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css'
    );
    wp_enqueue_style(
        'cpr-rescue',
        plugin_dir_url(__FILE__) . 'cpr-rescue.css'
    );
}

// Change the browser tab title on dog detail pages
add_filter('pre_get_document_title', 'cprDogBrowserTitle');

function cprDogBrowserTitle($title) {
    if (!isset($_GET['AnimalID']) || !is_numeric($_GET['AnimalID'])) {
        return $title;
    }

    $animalID = (int) $_GET['AnimalID'];
    $result   = getDetail($animalID);
    if (!$result || empty($result['data'][$animalID]['animalName'])) {
        return $title;
    }

    $dogName  = esc_html($result['data'][$animalID]['animalName']);
    $siteName = get_bloginfo('name');
    return $dogName . ' — ' . $siteName;
}

// ===========================================================================================================
// Friendly message shown when the RescueGroups API is unavailable
// ===========================================================================================================
function apiUnavailableMessage() {
    return "<div class='alert alert-warning' role='alert'>"
         . "<strong>We're sorry</strong> — our dog listings are temporarily unavailable. "
         . "Please check back soon or contact us at "
         . "<a href='mailto:info@carolinapoodlerescue.org'>info@carolinapoodlerescue.org</a>."
         . "</div>\n";
}

// ===========================================================================================================
// Renders the list of SOS dogs
// ===========================================================================================================
function sosDogs() {
    $animalName = isset($_GET['animalName']) ? htmlspecialchars($_GET['animalName']) : '';
    $result     = getSOSList();
    if (!$result) return apiUnavailableMessage();
    return animalList($result, false, $animalName);
}

// ===========================================================================================================
// Renders the list of CPR dogs available for adoption
// ===========================================================================================================
function cprDogs() {
    $animalName = isset($_GET['animalName']) ? htmlspecialchars($_GET['animalName']) : '';
    $result     = getCPRList();
    if (!$result) return apiUnavailableMessage();
    return animalList($result, true, $animalName);
}

// ===========================================================================================================
// Renders the detail page for a single dog
// ===========================================================================================================
function dogDetail() {
    if (!isset($_GET['AnimalID']) || !is_numeric($_GET['AnimalID'])) {
        redirect('index.php/pets.php');
    }

    $animalID = (int) $_GET['AnimalID'];
    $result   = generatePage($animalID);
    if (!$result) return apiUnavailableMessage();
    return animalDetail($result, $animalID);
}

// ===========================================================================================================
// Redirects to the given URL
// ===========================================================================================================
function redirect($url) {
    if (headers_sent()) {
        die('<script type="text/javascript">window.location.href="' . $url . '";</script>');
    } else {
        header('Location: ' . $url);
        die();
    }
}