<?php
namespace Avirtum\GutenDraw\Rest\Controllers;

defined( 'ABSPATH' ) || exit;

use Avirtum\GutenDraw\Models\DataModel;
use Avirtum\GutenDraw\Models\AccessModel;

class DataController {
    public function __construct() {
    }

    public function registerRestRoutes() {
        register_rest_route(
            GUTENDRAW_PLUGIN_REST_URL,
            '/items',
            [
                [
                    'methods' => \WP_REST_Server::READABLE,
                    'callback' => [ $this, 'getItems' ],
                    'permission_callback' => function() {
                        return AccessModel::currentUserCan(AccessModel::ACCESS_RIGHT_VIEW);
                    }
                ],
                [
                    'methods' => \WP_REST_Server::CREATABLE,
                    'callback' => [ $this, 'createItem' ],
                    'permission_callback' => function() {
                        return AccessModel::currentUserCan(AccessModel::ACCESS_RIGHT_CREATE);
                    }
                ]
            ]
        );

        register_rest_route(
            GUTENDRAW_PLUGIN_REST_URL,
            '/items/(?P<id>\d+)',
            [
                [
                    'methods' => \WP_REST_Server::EDITABLE,
                    'callback' => [ $this, 'updateItem' ],
                    'permission_callback' => function() {
                        return AccessModel::currentUserCan(AccessModel::ACCESS_RIGHT_EDIT);
                    }
                ]
            ]
        );

        register_rest_route(
            GUTENDRAW_PLUGIN_REST_URL,
            '/items/(?P<id>\d+)/status/(?P<status>(?i)draft|publish)',
            [
                [
                    'methods' => \WP_REST_Server::EDITABLE,
                    'callback' => [ $this, 'updateItemStatus' ],
                    'permission_callback' => function() {
                        return AccessModel::currentUserCan(AccessModel::ACCESS_RIGHT_PUBLISH);
                    }
                ]
            ]
        );

        register_rest_route(
            GUTENDRAW_PLUGIN_REST_URL,
            '/items/count',
            [
                [
                    'methods' => \WP_REST_Server::READABLE,
                    'callback' => [ $this, 'getItemsCount' ],
                    'permission_callback' => function() {
                        return AccessModel::currentUserCan(AccessModel::ACCESS_RIGHT_VIEW);
                    }
                ]
            ]
        );

        register_rest_route(
            GUTENDRAW_PLUGIN_PUBLIC_REST_URL,
            '/images/(?P<id>\d+)',
            [
                [
                    'methods' => \WP_REST_Server::READABLE,
                    'callback' => [ $this, 'getImage' ],
                    'permission_callback' => '__return_true'
                ]
            ]
        );
    }

    public function getItems( \WP_REST_Request $request ) {
        $data = DataModel::getCurrentUserItems();
        $response = isset( $data ) ? [ 'success' => true, 'data' => $data ] : [ 'success' => false ];

        return new \WP_REST_Response( $response );
    }

    public function createItem( \WP_REST_Request $request ) {
        $data = $request->get_json_params();

        $id = DataModel::createItem( $data );
        $response = $id ? [ 'success' => true, 'data' => [ 'id' => $id ] ] : [ 'success' => false ];

        return new \WP_REST_Response( $response );
    }

    public function updateItem( \WP_REST_Request $request ) {
        $id = intval( $request->get_param( 'id' ), 10 );
        $data = $request->get_json_params();

        $response = [ 'success' => DataModel::updateItem( $id, $data ) ];

        return new \WP_REST_Response( $response );
    }

    public function getItemsCount( \WP_REST_Request $request ) {
        $data = DataModel::getItemsCount();
        $response = isset( $data ) ? [ 'success' => true, 'data' => [ 'count' => $data ] ] : [ 'success' => false ];

        return new \WP_REST_Response( $response );
    }

    public function updateItemStatus( \WP_REST_Request $request ) {
        $id = intval( $request->get_param( 'id' ), 10 );
        $status = sanitize_key( $request->get_param( 'status' ) );

        $response = [ 'success' => DataModel::updateItemStatus( $id, $status ) ];

        return new \WP_REST_Response( $response );
    }

    public function getImage( \WP_REST_Request $request ) {
        $id = intval( $request->get_param( 'id' ), 10 );

        $image = DataModel::getItemImage( $id );
        $response = isset( $image ) ? [ 'success' => true, 'image' => $image ] : [ 'success' => false ];

        return new \WP_REST_Response( $response );
    }
}
