<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/

/**
 * Class TB_Server_Utils_Cloudinary
 *
 * Wrapper for cloudinary communication
 *
 */
class TB_Server_Utils_Cloudinary {

    const CLOUD_NAME = 'dalcs9tjq';
    const API_KEY = '496287488668145';
    const API_SECRET = '25lJoehys4w7sWPCSlW_I77-5YE';

    // Privates
    private static $inst = NULL;

    /**
     * Get default logger
     *
     * @return TB_Server_Utils_Cloudinary|null
     */
    public static function get() {
        if ( is_null( self::$inst ) ) {
            // Include Cloudinary
            $dependencyRoot = Francis_Utils_Config::get( 'path.dependencies' );
            require_once $dependencyRoot . '/cloudinary/src/Cloudinary.php';
            require_once $dependencyRoot . '/cloudinary/src/Uploader.php';
            require_once $dependencyRoot . '/cloudinary/src/Api.php';

            \Cloudinary::config(array(
                "cloud_name" => self::CLOUD_NAME,
                "api_key" => self::API_KEY,
                "api_secret" => self::API_SECRET
            ));

            self::$inst = new self();
        }
        return self::$inst;
    }

    /**
     * Uploads image
     * @param string $imgData Raw image data
     * @return array $tags - List of tags
     */
    public function upload( $imgData, array $tags = array() ) {
        // Upload to cloud
        $res = \Cloudinary\Uploader::upload(
            $imgData,
            array( "invalidate" => TRUE, 'tags' => $tags )
        );
        return $res;
    }

    /**
     * Deletes given image
     * @param string $imgName - Image identifier, ie. public id
     */
    public function delete( $imgName ) {
        $res = \Cloudinary\Uploader::destroy(
            $imgName,
            array( "invalidate" => TRUE )
        );
        return $res;
    }
}