<?php
/**
 * Created by PhpStorm.
 * User: Olyanren
 * Date: 30.10.2016
 * Time: 18:20
 */

namespace AppBundle\Utils;


use AppBundle\Entity\Delivery;
use AppBundle\Entity\Location;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;


class LocationUtil
{
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /**
     * @param $locationId
     * @return array
     * @internal param Delivery $delivery
     */
    public function getIdsFromLocationId($locationId)
    {
        $location=$this->getLocation($locationId);
        $ownId = $location->getId();
        if ($location->getLocation() == null) {
            $type = "sube";
            $parentId = 0;
            $parentParentId = 0;
            return array($ownId, $type, $parentId, $parentParentId);
        } else if ($location->getLocation()->getLocation()==null) {
            $type = "bolge";
            $parentId = $location->getLocation()->getId();
            $parentParentId = 0;
            return array($ownId, $type, $parentId, $parentParentId);
        } else {
            $type = "ekip";
            $parentId = $location->getLocation()->getId();
            $parentParentId = $location->getLocation()->getLocation()->getId();
            return array($ownId, $type, $parentId, $parentParentId);
        }
    }

    /**
     * @param $locationId
     * @return Location
     */
    public function getLocation($locationId)
    {
        return $this->entityManager->find("AppBundle:Location", $locationId);

    }

    /**
     * @param Delivery $delivery
     * @return array
     */
    public function getIds(Delivery $delivery)
    {
        $ownId = $delivery->getLocation()->getId();
        if ($this->isSube($delivery)) {
            $type = "sube";
            $parentId = 0;
            $parentParentId = 0;
            return array($ownId, $type, $parentId, $parentParentId);
        } else if ($this->isBolge($delivery)) {
            $type = "bolge";
            $parentId = $delivery->getLocation()->getLocation()->getId();
            $parentParentId = 0;
            return array($ownId, $type, $parentId, $parentParentId);
        } else {
            $type = "ekip";
            $parentId = $delivery->getLocation()->getLocation()->getId();
            $parentParentId = $delivery->getLocation()->getLocation()->getLocation()->getId();
            return array($ownId, $type, $parentId, $parentParentId);
        }
    }

    public function getLocationId(Request $request)
    {
        $locationSube = $request->request->get("sube");
        $locationBolge = $request->request->get("bolge");
        $locationEkip = $request->request->get("ekip");

        if (empty($locationEkip)) {
            if (empty($locationBolge)) $location = $locationSube;
            else $location = $locationBolge;
        } else $location = $locationEkip;
        return $location;
    }

    /**
     * @param Delivery $delivery
     * @return bool
     */
    public function isSube(Delivery $delivery)
    {
        return $delivery->getLocation()->getLocation() == null;
    }

    /**
     * @param Delivery $delivery
     * @return bool
     */
    public function isBolge(Delivery $delivery)
    {
        return $delivery->getLocation()->getLocation()->getLocation() == null;
    }
}