<?php

namespace Modules\Language\Services;

use Illuminate\Http\Request;
use Modules\Language\Interfaces\LanguageRepositoryInterface;
use Modules\Language\Models\Language;

class LanguageService
{
    private LanguageRepositoryInterface $languageRepository ;

    public function __construct(LanguageRepositoryInterface $languageRepository)
    {
        $this->languageRepository = $languageRepository;
    }

    public function get(array $request)
    {
        $builder = $this->languageRepository->get($request);

        if (@$request['name'])
        {
            $builder->where('name', 'LIKE', '%'.$request['name'].'%');
        }
        if (@$request['local'])
        {
            $builder->where('local', $request['local']);
        }
        if (isset($request['type']))
        {
            $builder->where('status', $request['type']);
        }
        if (isset($request['admin_status']))
        {
            $builder->where('admin_status', $request['admin_status']);
        }
        if (@$request['date_from'])
        {
            $builder->whereDate('created_at', '>=', $request['date_from']);
        }

        if (@$request['date_to'])
        {
            $builder->whereDate('created_at', '<=', $request['date_to']);
        }

        if (isset($request['status']))
        {
            if (@$request['status'] == 0)
            {
                $builder->onlyTrashed();
            }
        }

        if (@$request['orderBy'])
        {
            $builder->orderBy($request['orderBy']['key'],$request['orderBy']['value']);
        }
        else
        {
            $builder->orderBy('id','DESC');
        }

        if (@$request['paginate']) {
            $languages= $builder->paginate(50);
        }else{
            $languages=$builder->cursor();
        }
        return $languages;
    }
    public function checkItem($id)
    {
        $language = $this->languageRepository->checkItem($id);
        return $language;
    }

    public function getItem($id)
    {
        $language = $this->checkItem($id);

        return $language;
    }

    public function getItemTrashed($id)
    {
        $language = $this->languageRepository->getItemTrashed($id);
        return $language;
    }

    public function create(array $request): Language
    {
        $language = $this->languageRepository->create($request);
        return $language;
    }
    public function update(array $request,int $id): Language
    {
        $language = $this->checkItem($id);
        $language = $this->languageRepository->update($request,$language);
        return $language;
    }
    public function destroy(int $id): void
    {
        $language = $this->checkItem($id);
        if ($language)
        {
            $this->languageRepository->destroy($language);
        }
        else
        {
            $language = $this->getItemTrashed($id);
            $this->languageRepository->restore($language);
        }
    }
    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1)
        {
            $this->languageRepository->destroyAll($request['ids']);
        }
        else
        {
            $this->languageRepository->restoreAll($request['ids']);
        }
    }
}
