<?php

namespace OfficeBrain\Bundle\SearchBundle\Form\Data;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContextInterface;
use Doctrine\ORM\EntityManager;
use OfficeBrain\Bundle\CategoryBundle\Entity\CultureCategory;
use Doctrine\Common\Collections\ArrayCollection;

class ProductSearchData
{
    protected $country;
    protected $tag;
    protected $categoryId;
    protected $campaignId;
    protected $categorySlug;
    protected $supplierId;
    protected $brandId;
    protected $minPrice;
    protected $maxPrice;
    protected $minQty;
    protected $maxQty;
    protected $pageNumber;
    protected $sortOn;
    protected $sortOrder;
    protected $keyword;
    protected $attribute;
    protected $sort;
    protected $searchType;
    protected $catalogYear;
    protected $priceValidUntil;
    protected $sizeId;
    protected $inkId;
    protected $productionTimeId;
    protected $fobId;
    // TODO added for promocan api
    protected $lineNameId;
    protected $priceCurrency;
    protected $quantity;

    protected $sku;
    protected $em;
    protected $languageCode;
    protected $callForPrice;
    protected $lineName;
    protected $lineNameIdName;
    protected $categoryName;
    protected $campaignName;
    protected $postName;
    protected $supplierName;
    protected $supplier;
    protected $distributorId;
    protected $customCategoryId;
    protected $productId;
    protected $instanceId;
    
    protected $userRole;
    protected $userId;
    protected $rating;
    protected $productAttribute;
    
    protected $publicCustomTagText;
    protected $privateCustomTagText;
    
    protected $colorMapping;
    protected $serviceGroupId;
    
    protected $categoryIdNot;
    protected $inactiveCategoryId;
    protected $productIdNot;
    protected $tagNot;
    protected $categoryTagId;
    protected $categoryTag;
    protected $quoteSearch;
    protected $originalSupplierId;
    protected $similarSku;
    protected $relatedProductIds;
    protected $recentProductIds;
    protected $displayCampaignProduct;
    
    public function __construct(EntityManager $em, $languageCode = '1')
    {
        $this->em = $em;
        $this->languageCode = $languageCode;
    }

    public static function toSolrSupportedArray($object)
    {
        $returnArray = array();
        foreach (get_class_vars(get_class($object)) as $key=>$value) {
            $tmpKey =  trim(strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $key)));
            $returnArray[$tmpKey] = (in_array($key, array( "brandId"))? (isset($object->$key)?array($object->$key):null) : $object->$key);
        }
        return $returnArray;
    }

    /**
     * @Assert\Callback
     */
    public function validate(ExecutionContextInterface $context)
    {
        if ($this->getCategorySlug()!="") {
            $result = $this->em->getRepository('OfficeBrainCategoryBundle:CultureCategory')->findOneBy(array('slug'=>$this->getCategorySlug(), 'culture'=>$this->languageCode));
            if (!$result) {
                $context->addViolationAt(
                        'categorySlug',
                        'Please provide valid category slug.',
                        array(),
                        null
                );
            } else {
                $this->setCategoryId($result->getCat()->getId());
            }
        }
    }
    
    public function getCategoryTagId()
    {
    	return $this->categoryTagId;
    }
    
    public function setCategoryTagId($categoryTagId)
    {
    	$this->categoryTagId = $categoryTagId;
    	return $this;
    }
    
    public function getCategoryTag()
    {
    	return $this->categoryTag;
    }
    
    public function setCategoryTag($categoryTag)
    {
    	$this->categoryTag = $categoryTag;
    	return $this;
    }
    public function getQuoteSearch()
    {
    	return $this->quoteSearch;
    }
    
    public function setQuoteSearch($quoteSearch)
    {
    	$this->quoteSearch = $quoteSearch;
    	return $this;
    }
    
    
    /************************************Solr call api starts*******************************************/
    public function init($apiCallType)
    {
        $this->isError = false;
        $this->apiCallType = trim($apiCallType);
    }
    
    public function setSearchKeyword($searchKeyword)
    {
        $this->searchKeyword = trim($searchKeyword);
    }
    
    public function setAttributeFilter($attributeFilter)
    {
        $attributeFilter = trim($attributeFilter);
        if ($attributeFilter) {
            $this->attributeFilter = $this->parseAttribute($attributeFilter);
        }
    }
    
    private function parseAttribute($attributeFilter)
    {
        $attributeFilterArray = array();
        $attributeFilter = json_decode($attributeFilter);
    
        foreach ($attributeFilter as $filterData) {
            $filterData = explode('-', $filterData);
            $attributeFilterArray[$filterData[0]][] = $filterData[1];
        }
        return json_encode($attributeFilterArray);
    }
    
    public function setServiceGroupId($id)
    {
        return $this->serviceGroupId = $id;
    }
    public function getServiceGroupId()
    {
    	return $this->serviceGroupId;
    }
    
    /* Similar Sku */
    public function setSimilarSku($similarSku)
    {
        return $this->similarSku = $similarSku;
    }
    public function getSimilarSku()
    {
        return $this->similarSku;
    }
    /* Similar Sku */
    
    /******************************************Solr call api ends**************************************************/
    /******************************************set category filter starts**************************************************/
        
    public function setCategoryFilter($categoryFilter)
    {
        $categoryFilter = trim($categoryFilter);
        if ($categoryFilter) {
            $this->categoryFilter = $this->parseCategoryFilterAttribute($categoryFilter);
        }
    }
    
    private function parseCategoryFilterAttribute($categoryFilter)
    {
        $categoryFilterArray = array();
        $categoryFilter = json_decode($categoryFilter);
        return json_encode($categoryFilter);
    }
    
    public function setBrandFilter($brandFilter)
    {
        $brandFilter = trim($brandFilter);
        if ($brandFilter) {
            $this->brandFilter = $this->parseBrandFilterAttribute($brandFilter);
        }
    }
    
    private function parseBrandFilterAttribute($brandFilter)
    {
        $brandFilterArray = array();
        $brandFilter = json_decode($brandFilter);
        return json_encode($brandFilter);
    }
    
    /******************************************set category filter ends**************************************************/
    /******************************************set sort starts**************************************************/
    public function setSortBy($sortBy)
    {
        $this->sortBy = trim($sortBy);
        if ($this->sortBy) {
            $sortBy = explode('::', $this->sortBy);
            $this->sortField = $sortBy[0];
            $this->sortOrder = strtoupper($sortBy[1]);
        }
    }
    
    /******************************************set sort ends**************************************************/
    /******************************************Feature starts**************************************************/
    public function setNewProduct($newProduct)
    {
        $this->newProduct = ($newProduct)?'yes':'';
    }
    
    public function setFeaturedProduct($featuredProduct)
    {
        $this->featuredProduct = ($featuredProduct)?'yes':'';
    }
    
    public function setSaleProductAdd($addSaleProduct)
    {
        $this->addSale = $addSaleProduct;
    }
    
    public function setCloseoutProduct($closeoutProduct)
    {
        $this->closeoutProduct = ($closeoutProduct)?'yes':'';
    }
    
    public function setSaleCloseoutProduct($saleCloseoutProduct)
    {
        $this->saleCloseoutProduct = ($saleCloseoutProduct)?'yes':'';
    }
    
    /******************************************Featured ends**************************************************/
    
    public function setProductPerPage($productPerPage)
    {
        $this->productPerPage = trim($productPerPage);
    }
    
    public function getCountry()
    {
        return $this->country;
    }

    public function setCountry($country)
    {
        $this->country = $country;
        return $this;
    }

    public function getTag()
    {
        return $this->tag;
    }

    public function setTag($tag)
    {
        $this->tag = $tag;
        return $this;
    }

    public function getCategoryId()
    {
        return $this->categoryId;
    }

    public function setCategoryId($categoryId)
    {
        $this->categoryId = $categoryId;
        return $this;
    }
    
    public function getCampaignId()
    {
    	return $this->campaignId;
    }
    
    public function setCampaignId($campaignId)
    {
    	$this->campaignId = $campaignId;
    	return $this;
    }

    public function getColorMapping()
    {
    	return $this->colorMapping;
    }
    
    public function setColorMapping($colorMapping)
    {
    	$this->colorMapping = $colorMapping;
    	return $this;
    }
    
    public function getSupplierId()
    {
        return $this->supplierId;
    }

    public function setSupplierId($supplierId)
    {
        $this->supplierId = $supplierId;
        return $this;
    }
    /* 
     * Emp ID: 4014
     * Add new field Original Supplier Id
     * */
    public function getOriginalSupplierId()
    {
    	return $this->originalSupplierId;
    }
    
    public function setOriginalSupplierId($originalSupplierId)
    {
    	$this->originalSupplierId = $originalSupplierId;
    	return $this;
    }

    public function getBrandId()
    {
        return $this->brandId;
    }

    public function setBrandId($brandId)
    {
        $this->brandId = $brandId;
        return $this;
    }

    public function getMinPrice()
    {
        return $this->minPrice;
    }

    public function setMinPrice($minPrice)
    {
        $this->minPrice = $minPrice;
        return $this;
    }

    public function getMaxPrice()
    {
        return $this->maxPrice;
    }

    public function setMaxPrice($maxPrice)
    {
        $this->maxPrice = $maxPrice;
        return $this;
    }

    public function getMinQty()
    {
        return $this->minQty;
    }

    public function setMinQty($minQty)
    {
        $this->minQty = $minQty;
        return $this;
    }

    public function getMaxQty()
    {
        return $this->maxQty;
    }

    public function setMaxQty($maxQty)
    {
        $this->maxQty = $maxQty;
        return $this;
    }

    public function getPageNumber()
    {
        return $this->pageNumber;
    }

    public function setPageNumber($pageNumber)
    {
        $this->pageNumber = $pageNumber;
        return $this;
    }

    public function getSortOn()
    {
        return $this->sortOn;
    }

    public function setSortOn($sortOn)
    {
        $this->sortOn = $sortOn;
        return $this;
    }

    public function getSortOrder()
    {
        return $this->sortOrder;
    }

    public function setSortOrder($sortOrder)
    {
        $this->sortOrder = $sortOrder;
        return $this;
    }

    public function getKeyword()
    {
        return $this->keyword;
    }

    public function setKeyword($keyword)
    {
        $this->keyword = $keyword;
        return $this;
    }

    public function getAttribute()
    {
        return $this->attribute;
    }

    public function setAttribute($attribute)
    {
        $this->attribute = $attribute;
        return $this;
    }

    public function getSort()
    {
        return $this->sort;
    }

    public function setSort($sort)
    {
        $this->sort = $sort;
        return $this;
    }

    public function getCategorySlug()
    {
        return $this->categorySlug;
    }

    public function setCategorySlug($categorySlug)
    {
        $this->categorySlug = $categorySlug;
        return $this;
    }

    /**
     * @param mixed $lineNameId
     */
    public function setLineNameId($lineNameId)
    {
        $this->lineNameId = $lineNameId;
    }

    /**
     * @return mixed
     */
    public function getLineNameId()
    {
        return $this->lineNameId;
    }
    
    /**
     * @param mixed $sizeId
     */
    public function setSizeId($sizeId)
    {
    	$this->sizeId = $sizeId;
    }
    
    /**
     * @return mixed
     */
    public function getSizeId()
    {
    	return $this->sizeId;
    }
    
    /**
     * @param mixed $inkId
     */
    public function setInkId($inkId)
    {
    	$this->inkId = $inkId;
    }
    
    /**
     * @return mixed
     */
    public function getInkId()
    {
    	return $this->inkId;
    }
    
    /**
     * @param mixed $productionTimeId
     */
    public function setProductionTimeId($productionTimeId)
    {
    	$this->productionTimeId = $productionTimeId;
    }
    
    /**
     * @return mixed
     */
    public function getProductionTimeId()
    {
    	return $this->productionTimeId;
    }
    
    /**
     * @param mixed $fobId
     */
    public function setFobId($fobId)
    {
    	$this->fobId = $fobId;
    }
    
    /**
     * @return mixed
     */
    public function getFobId()
    {
    	return $this->fobId;
    }

    /**
     * @param mixed $searchType
     */
    public function setSearchType($searchType)
    {
        $this->searchType = $searchType;
    }

    /**
     * @return mixed
     */
    public function getSearchType()
    {
        return $this->searchType;
    }

    /**
     * @param mixed $catalogYear
     */
    public function setCatalogYear($catalogYear)
    {
        $this->catalogYear = $catalogYear;
    }

    /**
     * @return mixed
     */
    public function getCatalogYear()
    {
        return $this->catalogYear;
    }

    /**
     * @param mixed $priceValidUntil
     */
    public function setPriceValidUntil($priceValidUntil)
    {
        $this->priceValidUntil = $priceValidUntil;
    }

    /**
     * @return mixed
     */
    public function getPriceValidUntil()
    {
        return $this->priceValidUntil;
    }

    /**
     * @param string $languageCode
     */
    public function setLanguageCode($languageCode)
    {
        $this->languageCode = $languageCode;
    }

    /**
     * @return string
     */
    public function getLanguageCode()
    {
        return $this->languageCode;
    }

    /**
     * @param mixed $priceCurrency
     */
    public function setPriceCurrency($priceCurrency)
    {
        $this->priceCurrency = $priceCurrency;
    }

    /**
     * @return mixed
     */
    public function getPriceCurrency()
    {
        return $this->priceCurrency;
    }

    /**
     * @param mixed $quantity
     */
    public function setQuantity($quantity)
    {
        $this->quantity = $quantity;
    }

    /**
     * @return mixed
     */
    public function getQuantity()
    {
        return $this->quantity;
    }

    /**
     * @param mixed $sku
     */
    public function setSku($sku)
    {
        $this->sku = $sku;
    }

    /**
     * @return mixed
     */
    public function getSku()
    {
        return $this->sku;
    }

    /**
     * @param mixed $callForPrice
     */
    public function setCallForPrice($callForPrice)
    {
        $this->callForPrice = $callForPrice;
    }

    /**
     * @return mixed
     */
    public function getCallForPrice()
    {
        return $this->callForPrice;
    }

    /**
     * @param mixed $lineName
     */
    public function setLineName($lineName)
    {
        $this->lineName = $lineName;
    }

    /**
     * @return mixed
     */
    public function getLineName()
    {
        return $this->lineName;
    }
    
    /**
     * @param mixed $lineNameIdName
     */
    public function setLineNameIdName($lineNameIdName)
    {
        $this->lineNameIdName = $lineNameIdName;
    }
    
    /**
     * @return mixed
     */
    public function getLineNameIdName()
    {
        return $this->lineNameIdName;
    }

    /**
     * @param mixed $categoryName
     */
    public function setCategoryName($categoryName)
    {
        $this->categoryName = $categoryName;
    }

    /**
     * @return mixed
     */
    public function getCategoryName()
    {
        return $this->categoryName;
    }
    
    /**
     * @param mixed $campaignName
     */
    public function setCampaignName($campaignName)
    {
    	$this->campaignName = $CampaignName;
    }
    
    /**
     * @return mixed
     */
    public function getCampaignName()
    {
    	return $this->campaignName;
    }
    
    /**
     * @param mixed $postName
     */
    public function setPostName($postName)
    {
        $this->postName = $postName;
    }

    /**
     * @return mixed
     */
    public function getPostName()
    {
        return $this->postName;
    }

    /**
     * @param mixed $instanceId
     */
    public function setInstanceId($instanceId)
    {
        $this->instanceId = $instanceId;
    }

    /**
     * @return mixed
     */
    public function getInstanceId()
    {
        return $this->instanceId;
    }
    /**
     * @param mixed $supplierName
     */
    public function setSupplierName($supplierName)
    {
        $this->supplierName = $supplierName;
    }

    /**
     * @return mixed
     */
    public function getSupplierName()
    {
        return $this->supplierName;
    }

    /**
     * @param mixed $distributorId
     */
    public function setDistributorId($distributorId)
    {
        $this->distributorId = $distributorId;
    }

    /**
     * @return mixed
     */
    public function getDistributorId()
    {
        return $this->distributorId;
    }

    /**
     * @param mixed $customCategoryId
     */
    public function setCustomCategoryId($customCategoryId)
    {
        $this->customCategoryId = $customCategoryId;
    }

    /**
     * @return mixed
     */
    public function getCustomCategoryId()
    {
        return $this->customCategoryId;
    }

    /**
     * @param mixed $productId
     */
    public function setProductId($productId)
    {
        $this->productId = $productId;
    }

    /**
     * @return mixed
     */
    public function getProductId()
    {
        return $this->productId;
    }

    /**
     * @param mixed $userRole
     */
    public function setUserRole($userRole)
    {
        $this->userRole = $userRole;
    }
    
    /**
     * @return mixed
     */
    public function getUserRole()
    {
        return $this->userRole;
    }
        
    /**
     * @param mixed $userId
     */
    public function setUserId($userId)
    {
        $this->userId = $userId;
    }
        
    /**
     * @return mixed
     */
    public function getUserId()
    {
        return $this->userId;
    }
    
    /**
     * @param mixed $rating
     */
    public function setRating($rating)
    {
        $this->rating = $rating;
    }
    
    /**
     * @return mixed
     */
    public function getRating()
    {
        return $this->rating;
    }
    
    /**
     * @param mixed $rating
     */
    public function setSupplier($supplier)
    {
    	$this->supplier = $supplier;
    }
    
    /**
     * @return mixed
     */
    public function getSupplier()
    {
    	return $this->supplier;
    }
    
    public function getCategoryIdNot()
    {
    	return $this->categoryIdNot;
    }
    
    public function setCategoryIdNot($categoryIdNot)
    {
    	$this->categoryIdNot = $categoryIdNot;
    	return $this;
    }
    
    public function getInactiveCategoryId()
    {
        return $this->inactiveCategoryId;
    }
    
    public function setInactiveCategoryId($inactiveCategoryId)
    {
        $this->inactiveCategoryId = $inactiveCategoryId;
        return $this;
    }

    /**
     * @param mixed $productIdNot
     */
    public function setProductIdNot($productIdNot)
    {
    	$this->productIdNot = $productIdNot;
    }
    
    /**
     * @return mixed
     */
    public function getProductIdNot()
    {
    	return $this->productIdNot;
    }
    
    public function getTagNot()
    {
    	return $this->tagNot;
    }
    
    public function setTagNot($tagNot)
    {
    	$this->tagNot = $tagNot;
    	return $this;
    }
    
    public function getRelatedProductIds()
    {
        return $this->relatedProductIds;
    }
    
    public function setRelatedProductIds($relatedProductIds)
    {
        $this->relatedProductIds = $relatedProductIds;
        return $this;
    }
    
    public function getRecentProductIds()
    {
        return $this->recentProductIds;
    }
    
    public function setRecentProductIds($recentProductIds)
    {
        $this->recentProductIds = $recentProductIds;
        return $this;
    }
    
    public function getDisplayCampaignProduct()
    {
    	return $this->displayCampaignProduct;
    }
    
    public function setDisplayCampaignProduct($displayCampaignProduct)
    {
    	$this->displayCampaignProduct= $displayCampaignProduct;
    	return $this;
    }
}