<?php

namespace Modules\PaymentWay\Services;

use Illuminate\Http\Request;
use Modules\PaymentWay\Interfaces\PaymentWayRepositoryInterface;
use Modules\PaymentWay\Models\PaymentWay;

class PaymentWayService
{
    private PaymentWayRepositoryInterface $paymentWayRepository ;

    public function __construct(PaymentWayRepositoryInterface $paymentWayRepository)
    {
        $this->paymentWayRepository = $paymentWayRepository;
    }

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

        if (@$request['title'])
        {
            $builder->where('sd.title', 'LIKE', '%'.$request['title'].'%');
        }
        if (@$request['date_from'])
        {
            $builder->whereDate('product_types.created_at', '>=', $request['date_from']);
        }

        if (@$request['date_to'])
        {
            $builder->whereDate('product_types.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']) {
            $product_types= $builder->paginate(50);
        }else{
            $product_types=$builder->cursor();
        }
        return $product_types;
    }
    public function checkItem($id)
    {
        $product_type = $this->paymentWayRepository->checkItem($id);
        return $product_type;
    }

    public function getItem($id)
    {
        $product_type = $this->checkItem($id);
        $product_typeDescription = $this->paymentWayRepository->getItem($id);
        foreach ($product_typeDescription as $row) {
            $product_type[$row->local]= $row;
        }
        return $product_type;
    }

    public function getItemTrashed($id)
    {
        $product_type = $this->paymentWayRepository->getItemTrashed($id);
        return $product_type;
    }

    public function create(array $request): PaymentWay
    {
        $product_type = $this->paymentWayRepository->create($request);
        $this->paymentWayRepository->saveData($request,$product_type->id);
        return $product_type;
    }
    public function update(array $request,int $id): PaymentWay
    {
        $product_type = $this->checkItem($id);


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