<?php

namespace OfficeBrain\Bundle\SubShopBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * SubShopRepository.
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class SubShopThemeRepository extends EntityRepository
{
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $cultureId;
    protected $cultureCode;
    protected $userId;

    public function setProjectSetting($projetSetting)
    {
        $this->projectSetting = $projetSetting;
        $this->prepareProjectSettingData();
    }

    public function getProjectSetting()
    {
        return $this->projetSetting;
    }

    public function prepareProjectSettingData()
    {
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->userId = $this->projectSetting['user_id'];
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
    }

    public function set($subShopTheme)
    {
        return $this->commit($subShopTheme);
    }

    public function findAll()
    {
        return $this->createQueryBuilder('s')
        ->select('s')
        ->orderBy('s.id', 'DESC')
        ->getQuery()
        ->getResult();
    }

    public function find($id, $lockMode = NULL, $lockVersion = NULL)
    {
        return $this->createQueryBuilder('s')
                    ->select('s')
                    ->where('s.id = :id')
                    ->setParameter(':id', $id)
                    ->getQuery()
                    ->getSingleResult();
    }

    public function findByStatus($statusId)
    {
        return $this->createQueryBuilder('st')
                    ->select('st.id, st.name, st.folderName, st.themeImageName, st.themeType, st.createdAt, st.status')
                    //->leftJoin('OfficeBrainSubShopBundle:SubShop', 's', 'WITH', 's.shopThemeId = st.id')
                    ->where('st.status = :statusId AND st.deletedAt IS NULL')
                    //->groupBy('s.shopThemeId')
                    ->setParameter(':statusId', $statusId)
                    ->orderBy('st.id', 'DESC')
                    ->getQuery()
                    ->getResult();
        /*return $this->createQueryBuilder('st')
                    ->select('st.id, st.name, st.folderName, st.themeType, st.createdAt, st.status, COUNT(s.id) AS themeUsedCount')
    				->leftJoin('OfficeBrainSubShopBundle:SubShop', 's', 'WITH', 's.shopThemeId = st.id')
                    ->where('st.status = :statusId AND st.deletedAt IS NULL AND st.createdUid = :userId')
                    ->groupBy('s.shopThemeId')
                    ->setParameter(':statusId', $statusId)
                    ->setParameter(':userId', $this->userId)
                    ->orderBy('st.id', 'DESC')
                    ->getQuery()
                    ->getResult();
                    */
    }

    public function findByType($themeType)
    {
        return $this->createQueryBuilder('st')
                    ->select('st.id, st.name, st.folderName, st.themeImageName, st.themeType, st.createdAt, st.status')
                    ->where('st.status = 1 AND st.themeType = :themeType AND st.deletedAt IS NULL')
                    ->setParameter(':themeType', $themeType)
                    ->orderBy('st.name', 'ASC')
                    ->getQuery()
                    ->getResult();
    }

    public function findByDelete()
    {
        return $this->createQueryBuilder('s')
        ->select('s')
        ->where('s.deletedAt IS NOT NULL')
        ->orderBy('s.id', 'DESC')
        ->getQuery()
        ->getResult();
    }

    public function findByOption($option, $keyword, $status_id)
    {
        $return = $this->createQueryBuilder('s')
                       ->select('s')
                       ->where('s.status= :status_id AND s.deletedAt IS NULL AND s.createdUid = :userId')
                       ->andWhere('s.instanceId  = :instanceId');

        if ($option == 'id') {
            $return->andWhere('s.'.$option.' = :keyword')
                   ->setParameter(':keyword', $keyword);
        } elseif ($option == 'name') {
            $return->andWhere('s.name like :keyword')
                   ->setParameter(':keyword', '%'.$keyword.'%');
        } elseif ($option == 'createdAt') {
            $keyword = str_replace('/', '-', $keyword);
            $return->andWhere('s.'.$option.' BETWEEN :startedDatetime AND :endedDatetime')
                   ->setParameter(':startedDatetime', date('Y-m-d H:i:s', strtotime($keyword.' 00:00:00')))
                   ->setParameter(':endedDatetime', date('Y-m-d H:i:s', strtotime($keyword.' 23:59:59')));
        } else {
            $return->andWhere('s.'.$option.' like :keyword')
            ->setParameter(':keyword', '%'.$keyword.'%');
        }

        $return->setParameter(':status_id', $status_id)
               ->setParameter(':instanceId', $this->instanceId)
               ->setParameter(':userId', $this->userId)
               ->orderBy('s.id', 'DESC')
               ->getQuery()
               ->getResult();

        return $return;
    }

    /**
     * Commit entity.
     */
    private function commit($entity)
    {
        $this->_em->persist($entity);
        $this->_em->flush();

        return $entity->getId();
    }
}
