<?php

namespace App\Services;

use App\Repositories\SupplierRepositoryInterface;

class SupplierService extends ServiceBase
{
    public function __construct(SupplierRepositoryInterface $Repository)
    {
        parent::__construct();
        $this->Repository = $Repository;
        // $this->setRelation('contact','productGroup');
    }

    public function addProductGroup($supplierId, $productGroupIds = []): bool
    {
        return $this->Repository->addProductGroup($supplierId, $productGroupIds);
    }

    public function addContact($supplierId, $contactIds = []): bool
    {
        return $this->Repository->addContact($supplierId, $contactIds);
    }

    public function removeContact($supplierId, $contactIds = []): bool
    {
        return $this->Repository->removeContact($supplierId, $contactIds);
    }

    public function removeProductGroup($supplierId, $productGroupIds = []): bool
    {
        return $this->Repository->removeProductGroup($supplierId, $productGroupIds);
    }

    public function syncContact($supplierId, $contactIds = []): bool
    {
        return $this->Repository->syncContact($supplierId, $contactIds);
    }

    public function syncProductGroup($supplierId, $productGroupIds = []): bool
    {
        return $this->Repository->syncProductGroup($supplierId, $productGroupIds);
    }

    public function syncService($supplierId, $serviceId = []): bool
    {
        return $this->Repository->syncService($supplierId, $serviceId);
    }

    public function syncProfitCenter($supplierId, $profitCenterIds = []): bool
    {
        return $this->Repository->syncProfitCenter($supplierId, $profitCenterIds);
    }

    public function add($data): ?int
    {
        $contactIds = $data['contactId'];
        $productGroupId = $data['productGroupId'];
        $serviceId = $data['serviceId'];
        $profitCenterId = $data['profitCenterId'];
        unset($data['contactId'],$data['productGroupId']);
        if (! $supplierId = parent::add($data)) {
            return false;
        }
        $this->addContact($supplierId, $contactIds);
        $this->addProductGroup($supplierId, $productGroupId);
        $this->syncService($supplierId, $serviceId);
        $this->syncProfitCenter($supplierId, $profitCenterId);

        return $supplierId;
    }

    /**
     * Updates a post.
     *
     * @param int
     * @param array
     */
    public function update($id, array $data): bool
    {
        $contactIds = $data['contactId'];
        $productGroupId = $data['productGroupId'];
        $serviceId = $data['serviceId'];
        $profitCenterId = $data['profitCenterId'];
        unset($data['contactId'],$data['productGroupId']);
        if ($this->Repository->update($id, $data)) {
            $this->syncContact($id, $contactIds);
            $this->syncProductGroup($id, $productGroupId);
            $this->syncService($id, $serviceId);
            $this->syncProfitCenter($id, $profitCenterId);

            return true;
        }

        return false;
    }
}
