<?php

namespace OfficeBrain\Bundle\CmsBundle\Manager\Extended;

use OfficeBrain\Bundle\CmsBundle\Manager\AbstractManager\CmsSearchCriteriaAbstract as CmsSearchCriteriaAbstract;
use OfficeBrain\Bundle\CmsBundle\Entity\CmsSearchCriteria;
use OfficeBrain\Bundle\CmsBundle\Entity\CmsSearchCriteriaLanguage;

class CmsSearchCriteriaExtended extends CmsSearchCriteriaAbstract
{
    /**
     * @author Employee Id: 4589
     *
     * Description : This function is used to get blank object with language
     *
     * @param 1 : Null
     *
     * @return : SearchCriteria Object
     *
     * @throws Exception : Null
     **/
    public function getSearchCriteriaWithLanguage()
    {
        $cmsSearchCriteria = new CmsSearchCriteria();

        foreach ($this->languages as $value) {
            $cmsSearchCriteriaLanguage = new CmsSearchCriteriaLanguage();
            $cmsSearchCriteriaLanguage->setLanguageId($value['id']);
            $cmsSearchCriteria->getlanguageMapping()[] = $cmsSearchCriteriaLanguage;
        }

        return $cmsSearchCriteria;
    }

    /**
     * @author Employee Id: 4589
     *
     * Description : This function is used to get list
     *
     * @param 1 : Null
     *
     * @return : list of array with pagignation
     *
     * @throws Exception : Null
     **/
    public function getSearchCriteriaList()
    {
        $data = array();
        $countryId = $this->request->get('countryId');
        if (!$countryId) {
            $countryId = $this->countryId;
        }

        $cmsSearchCriteriaArray = $this->cmsSearchCriteriaRepository->findByCountryId($countryId);

        if ($cmsSearchCriteriaArray) {
            $data['pagination'] = $this->paginatorManager->paginate($cmsSearchCriteriaArray, $this->request->get('page', 1), $this->recordsPerPage, array(), count($cmsSearchCriteriaArray));
        }

        $data['records_per_page'] = $this->recordsPerPage;
        $data['page'] = $this->request->get('page', 1);
        $data['countryId'] = $countryId;

        //unset variable
        unset($cmsSearchCriteriaArray);
        unset($countryId);

        return $data;
    }

    /**
     * @author Employee Id: 4589
     *
     * Description : This function is used to remove un-use language from object
     *
     * @param 1 : Form Object
     *
     * @return : Form Object
     *
     * @throws Exception : Null
     **/
    public function filterFormData($form)
    {
        $searchCriteria = $form->getData();

        $countryId = $searchCriteria->getCountryId();

        if ($countryId) {
            $countryCode = $this->getCountryField($countryId, 'id', 'code');

            foreach ($searchCriteria->getLanguageMapping() as $index => $language) {
                if (!in_array($language->getLanguageId(), $this->countries[$countryCode]['language'])) {
                    unset($searchCriteria->getLanguageMapping()[$index]);
                } else {
                    if ($language->getTitle() == '') {
                        $form->get('languageMapping')[$index]->get('title')->addError(new \Symfony\Component\Form\FormError($this->translatorManager->trans('cms.search_criteria.admin.form.title.not_blank')));
                    }
                }
            }

            //unset variable
            unset($countryCode);
        }

        //unset variable
        unset($countryId);
        unset($searchCriteria);

        return $form;
    }

    /**
     * @author Employee Id: 4589
     *
     * Description : This function is used to save search criteria
     *
     * @param 1 : SearchCriteria Object
     *
     * @return : Null
     *
     * @throws Exception : Null
     **/
    public function setSearchCriteria($searchCriteria)
    {
        $searchCriteriaId = $searchCriteria->getId();

        if ($searchCriteriaId == '') {
            $searchCriteria->setCreatedUid($this->projectSetting['user_id']);
            $searchCriteria->setInstanceId($this->projectSetting['instance_id']);
            $searchCriteria->setInstanceType($this->projectSetting['instance_type']);
            $searchCriteria->setCreatedAt(new \DateTime());
        }

        $searchCriteria = $this->setEncodeAttribute($searchCriteria);

        $languageMapping = $searchCriteria->getLanguageMapping();

        if ($searchCriteriaId == '') {
            foreach ($languageMapping as $language) {
                $language->setCmsSearchCriteriaId($searchCriteria);
            }
        }
        $this->cmsSearchCriteriaRepository->set($searchCriteria);
    }

    /**
     * @author Employee Id: 4589
     *
     * Description : This function is used to get SearchCriteria single object by id
     *
     * @param 1 : Null
     *
     * @return : Null
     *
     * @throws Exception : Null
     **/
    public function getSearchCriteriaById()
    {
        return $this->cmsSearchCriteriaRepository->find($this->request->get('id'));
    }

    /**
     * @author Employee Id: 4589
     *
     * Description : This function is used to update SearchCriteria
     *
     * @param 1 : SearchCriteria Object
     *
     * @return : SearchCriteria Object
     *
     * @throws Exception : Null
     **/
    public function updateSearchCriteria($searchCriteria)
    {
        $searchCriteria->setUpdatedUid($this->projectSetting['user_id']);
        $searchCriteria->setUpdatedAt(new \DateTime());

        return $this->setSearchCriteria($searchCriteria);
    }

    /**
     * @author Employee Id: 4589
     *
     * Description : This function is used to json encode SearchCriteria attribute
     *
     * @param 1 : SearchCriteria Object
     *
     * @return : SearchCriteria Object
     *
     * @throws Exception : Null
     **/
    public function setEncodeAttribute($searchCriteria)
    {
        $attributes = json_decode($searchCriteria->getAttribute(), true);

        $attributeJson = $this->formatAttribute($attributes);

        $searchCriteria->setAttribute(json_encode($attributeJson));

        //unset variable
        unset($attributes);
        unset($attributeJson);

        return $searchCriteria;
    }

    /**
     * @author Employee Id: 4589
     *
     * Description : This function is used to soft delete SearchCriteria
     *
     * @param 1 : Null
     *
     * @return : True or False
     *
     * @throws Exception : Null
     **/
    public function deleteSearchCriteria()
    {
        $SearchCriteriaObj = $this->cmsSearchCriteriaRepository->find($this->request->get('id'));

        if (is_object($SearchCriteriaObj)) {
            $SearchCriteriaObj->setDeletedAt(new \DateTime());
            $SearchCriteriaObj->setDeletedUid($this->projectSetting['user_id']);

            return $this->cmsSearchCriteriaRepository->set($SearchCriteriaObj);
        }
    }

    /**
     * @author Employee Id: 4589
     *
     * Description : This function is used to search
     *
     * @param 1 : Null
     *
     * @return : list of array with pagignation
     *
     * @throws Exception : Null
     **/
    public function searchSearchCriteria()
    {
        $data = array();
        $countryId = $this->request->get('countryId', $this->countryId);
        $option = $this->request->get('option');
        $keyword = $this->request->get('keyword');

        $cmsSearchCriteriaArray = $this->cmsSearchCriteriaRepository->findByOption($countryId, $option, $keyword);

        if (count($cmsSearchCriteriaArray) > 0) {
            $data['pagination'] = $this->paginatorManager->paginate($cmsSearchCriteriaArray, $this->request->get('page', 1), $this->recordsPerPage, array(), count($cmsSearchCriteriaArray));
        }

        $data['records_per_page'] = $this->recordsPerPage;
        $data['page'] = $this->request->get('page', 1);
        $data['countryId'] = $countryId;

        //unset variable
        unset($countryId);
        unset($cmsSearchCriteriaArray);
        unset($option);
        unset($keyword);

        return $data;
    }
    
    /**
     * @author Employee Id: 4589
     *
     * Description : This function is used to get list
     *
     * @param 1 : Null
     *
     * @return : list of array with pagignation
     *
     * @throws Exception : Null
     **/
    public function getAllSearchCriteriaList($countryId)
    {
        if (!$countryId) {
            $countryId = $this->countryId;
        }
    
        $cmsSearchCriteriaList = $this->cmsSearchCriteriaRepository->findByCountryId($countryId, true);
        
        $cmsSearchCriteriaArray = array();
        foreach ($cmsSearchCriteriaList as $cmsSearchCriteria) {
            $cmsSearchCriteriaArray[] = array(
                "id" => $cmsSearchCriteria->getId(),
                "title" => $cmsSearchCriteria->getLanguageMapping(),
                "search_criteria" => $cmsSearchCriteria->getAttribute()
            );
        }
        
        //unset variable
        unset($cmsSearchCriteriaList);
        unset($countryId);
    
        return $cmsSearchCriteriaArray;
    }
    
    /**
     * @author Employee Id: 5190
     * Description : Change Search Criteria Status
     *
     * @return object
     * */
    public function changeStatus($id)
    {
    	return $this->cmsSearchCriteriaRepository->changeStatus($id);
    }
}
