<?php
namespace Lycoreco\Apps\Admin\Controllers;

use Lycoreco\Includes\Routing\HttpExceptions;
use Lycoreco\Apps\Users\Models\UserModel;

/**
 * Controller to delete every object model using className and id
 */
class AdminDeleteController extends Abstract\AdminBaseController
{
    const DELETE_MODELS = [
        [
            'url_name'  => 'users',
            'model'     => 'Lycoreco\Apps\Users\Models\UserModel',
            'field_display' => 'field_username',
            'back_to' => 'admin:user',
            'success_to' => 'admin:user-list'
        ],
        [
            'url_name'  => 'user-banlist',
            'model'     => 'Lycoreco\Apps\Users\Models\BanlistModel',
            'field_display' => 'field_reason',
            'back_to' => 'admin:ban'
        ],
        [
            'url_name'  => 'recipes',
            'model'     => 'Lycoreco\Apps\Recipes\Models\RecipeModel',
            'field_display' => 'field_title',
            'back_to' => 'admin:recipe',
            'success_to' => 'admin:recipe-list'
        ],
        [
            'url_name'  => 'ingredients',
            'model'     => 'Lycoreco\Apps\Recipes\Models\IngredientModel',
            'field_display' => 'field_name',
            'back_to' => 'admin:ingredient',
            'success_to' => 'admin:ingredient-list'
        ],
        [
            'url_name'  => 'categories',
            'model'     => 'Lycoreco\Apps\Recipes\Models\CategoryModel',
            'field_display' => 'field_name',
            'back_to' => 'admin:category',
            'success_to' => 'admin:category-list'
        ],
        [
            'url_name'  => 'recipe-ingredients',
            'model'     => 'Lycoreco\Apps\Recipes\Models\IngredientInRecipeModel',
            'field_display' => 'ingredient_name',
            'back_to' => 'admin:ing-cat-rel',
        ],
    ];

    protected $template_name = APPS_PATH . '/Admin/Templates/delete.php';

    /**
     * @return null|UserModel
     */
    protected function get_model()
    {
        if (isset($this->__model))
            return $this->__model;

        $id = $this->url_context['url_2'];
        $model_class = '';

        foreach (self::DELETE_MODELS as $delete_model) {
            if($this->url_context['url_1'] == $delete_model['url_name']) {
                $model_class = $delete_model['model'];
                break;
            }
        }
        if(empty($model_class))
            return null;

        return $model_class::get(array(
            [
                'name' => 'obj.id',
                'type' => '=',
                'value' => $id
            ]
        ));
    }
    public function get_context_data()
    {
        $context = parent::get_context_data();

        $model = $this->get_model();
        if (empty($model))
            throw new HttpExceptions\NotFound404();

        // Display field to show what's model
        $field = '';
        $back_url = '';

        foreach (self::DELETE_MODELS as $delete_model) {
            if($this->url_context['url_1'] == $delete_model['url_name']) {
                $back_url = get_permalink($delete_model['back_to'], [$model->get_id()]);
                $field = $delete_model['field_display'];
            }
        }

        $context['back_url'] = $back_url;
        $context['model'] = $model;
        $context['field'] = $field;

        return $context;
    }
    protected function post()
    {
        $model = $this->get_model();

        if (empty($model))
            throw new HttpExceptions\NotFound404();

        $model->delete();

        // Redirect after delete
        $link = get_permalink('admin:home');
        foreach (self::DELETE_MODELS as $delete_model) {
            if($this->url_context['url_1'] == $delete_model['url_name']) {
                if(isset($delete_model['success_to'])) {
                    $link = get_permalink($delete_model['success_to']);
                }
                break;
            }
        }
        redirect_to($link);
    }
}
