<?php
/**
 * For the full copyright and license information, please view the
 * docs/licenses/LICENSE.txt file that was distributed with this source code.
 */

namespace PrestaShopBundle\Entity\Repository;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Query\Parameter;

/**
 * AttributeRepository.
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class AttributeRepository extends \Doctrine\ORM\EntityRepository
{
    public function findByLangAndShop($idLang, $idShop)
    {
        $attributeGroups = [];

        $qb = $this->createQueryBuilder('a')
            ->addSelect('ag.id AS attributeGroupId')
            ->addSelect('ag.position AS attributeGroupPosition')
            ->addSelect('agl.name AS attributeGroupName')
            ->addSelect('agl.publicName AS attributeGroupPublicName')
            ->addSelect('a.id')
            ->addSelect('a.color')
            ->addSelect('a.position as attributePosition')
            ->addSelect('al.name AS attributeName')
            ->join('a.attributeGroup', 'ag')
            ->join('a.shops', 's')
            ->join('a.attributeLangs', 'al')
            ->join('ag.attributeGroupLangs', 'agl')
            ->where('al.lang = :idLang')
            ->andWhere('agl.lang = :idLang')
            ->andWhere('s.id = :idShop')
            ->orderBy('attributePosition')
            ->addOrderBy('attributeGroupPosition')
            ->setParameters(new ArrayCollection([
                new Parameter('idShop', $idShop),
                new Parameter('idLang', $idLang),
            ]));

        $result = $qb->getQuery()->getArrayResult();

        foreach ($result as $attribute) {
            if (isset($attributeGroups[$attribute['attributeGroupPosition']])) {
                $attributeGroups[$attribute['attributeGroupPosition']]['attributes'][$attribute['attributePosition']] = $this->getAttributeRow($attribute);
            } else {
                $attributeGroups[$attribute['attributeGroupPosition']] = [
                    'id' => $attribute['attributeGroupId'],
                    'name' => $attribute['attributeGroupName'],
                    'publicName' => $attribute['attributeGroupPublicName'],
                    'position' => $attribute['attributeGroupPosition'],
                    'attributes' => [
                        $attribute['attributePosition'] => $this->getAttributeRow($attribute),
                    ],
                ];
            }
        }

        return $attributeGroups;
    }

    private function getAttributeRow($attribute)
    {
        $attributes = [
            'id' => $attribute['id'],
            'color' => $attribute['color'],
            'position' => $attribute['attributePosition'],
            'name' => $attribute['attributeName'],
            'texture' => '',
        ];
        if (@file_exists(_PS_COL_IMG_DIR_ . $attribute['id'] . '.jpg')) {
            $attributes['texture'] = _THEME_COL_DIR_ . $attribute['id'] . '.jpg';
        }

        return $attributes;
    }
}
