<?php

namespace Hura8\System\Controller;

use Hura8\Interfaces\AppResponse;
use Hura8\Interfaces\iEntityController;

abstract class aAdminEntityBaseController extends aEntityBaseController implements iEntityController
{

    public function updateFields($id, array $info): AppResponse
    {
        return $this->iEntityModel->updateFields($id, $info);
    }


    public function updateField($id, $field, $value): AppResponse
    {

        $fields_list = [];
        $fields_list[$field] = $value;

        return $this->iEntityModel->updateFields($id, $fields_list);
    }


    public function updateFeatured($id, $new_status): AppResponse
    {
        $status = ($new_status == 'on' || $new_status == 1) ? 1 : 0;

        return $this->iEntityModel->updateFields($id, ['is_featured' => $status]);
    }


    public function updateStatus($id, $new_status): AppResponse
    {
        $status = ($new_status == 'on' || $new_status == 1) ? 1 : 0;

        return $this->iEntityModel->updateFields($id, ['status' => $status]);
    }


    public function create(array $info): AppResponse
    {
        return $this->iEntityModel->create($info);
    }


    public function update($id, array $info): AppResponse
    {
        if($this->iEntityLanguageModel) {
            return $this->iEntityLanguageModel->update($id, $info);
        }

        return $this->iEntityModel->update($id, $info);
    }


    abstract protected function deleteFileBeforeDeleteItem($item_id) : bool;


    public function delete($id): AppResponse
    {
        if($this->deleteFileBeforeDeleteItem($id)) {
            return $this->iEntityModel->delete($id);
        }

        return new AppResponse('error', 'Cannot delete '.$id);
    }


    public function getEmptyInfo(array $additional_fields = []) : array
    {
        return $this->iEntityModel->getEmptyInfo($additional_fields);
    }


}
