<?php
namespace OfficeBrain\Bundle\CoreBundle\Service;

class SessionManager
{
    
    protected $session;
    protected $request;
    protected $locale;
    
    public function __construct($session, $request)
    {
        $this->session = $session;
        $this->request = $request;
        $culture=$this->request->get('_route_params');
        $this->locale=$culture['_locale'];
    }
    
    public function getSessionParameter($parameterName)
    {
        return $this->session->get($parameterName);
    }
    
    public function setSessionParameter($parameterName, $value)
    {
        return $this->session->set($parameterName, $value);
    }

    public function get($parameterName)
    {
        return $this->session->get($parameterName);
    }
    
    public function set($parameterName, $value)
    {
        $this->session->set($parameterName, $value);
    }
    
    public function has($key)
    {
        return $this->session->has($key);
    }
    
    public function removeSession($feature)
    {
        if ($this->session->has($feature)) {
            return $this->session->remove($feature);
        }
    }
    
    public function remove($feature)
    {
        if ($this->session->has($feature)) {
            return $this->session->remove($feature);
        }
    }
    
    public function removeSessionParameter($feature)
    {
        $sessionInfo=$this->getSessionParameter($feature);
        if ($sessionInfo[$this->locale]) {
            if (array_key_exists($this->locale, $sessionInfo)) {
                unset($sessionInfo[$this->locale]);
                $this->setSessionParameter($feature, $sessionInfo);
                return true;
            }
        }
    }

    public function setFlashBagMessage($fld, $val)
    {
        $this->session->getFlashBag()->add($fld, $val);
    }
    
    public function getFlashBagMessage($fld)
    {
        return $this->session->getFlashBag()->get($fld);
    }

    public function setProductToSession($productData, $feature)
    {
        if (count($productData['attribute'])>0) {
            $productDetail=$this->prepareProductDetail($productData);
        } else {
            $productDetail=$productData;
        }
        if ($this->getSessionParameter($feature)) {
            $session_info = $this->getSessionParameter($feature);
            if (array_key_exists($this->getHash($productDetail), $session_info[$this->locale])) {
                $flag=false;
            } else {
                $session_info[$this->locale][$this->getHash($productDetail)] = $productDetail;
                $flag=true;
            }
        } else {
            $session_info[$this->locale] = array();
            $session_info[$this->locale][$this->getHash($productDetail)] = $productDetail;
            $flag=true;
        }
        $this->setSessionParameter($feature, $session_info);
        if ($flag) {
            return true;
        } else {
            return false;
        }
    }
    
    public function prepareProductDetail($productDetail)
    {
        $product = $productDetail;
        if ($productDetail['attribute']['quantity']!='' && $productDetail['attribute']['quantity']>0) {
            $product = $this->getProductInfo($productDetail, $productDetail['attribute']['quantity']);
            unset($product['attribute']['quantity']);
        } else {
            unset($product['attribute']['quantity']);
        }
        if (count($product['attribute'])==0) {
            $product['attribute']=array();
        } else {
            $product=$this->getProductAttributeInfo($product);
        }
        return $product;
    }
    
    public function getProductAttributeInfo($productDetail)
    {
        if (count($productDetail['attribute'])>0 && array_key_exists('attributes', $productDetail)) {
            if (array_key_exists('Color', $productDetail['attributes'])) {
                if (array_key_exists('values', $productDetail['attributes']['Color'])) {
                    if (array_key_exists($productDetail['attribute']['Color'], $productDetail['attributes']['Color']['values'])) {
                        $productDetail['attribute']['Color']=$productDetail['attributes']['Color']['values'][$productDetail['attribute']['Color']];
                    }
                }
            }
                    
            
            if (array_key_exists('Size', $productDetail['attributes'])) {
                if (array_key_exists('values', $productDetail['attributes']['Size'])) {
                    if (array_key_exists($productDetail['attribute']['Size'], $productDetail['attributes']['Size']['values'])) {
                        $productDetail['attribute']['Size']=$productDetail['attributes']['Size']['values'][$productDetail['attribute']['Size']];
                    }
                }
            }
        } else {
            $productDetail['attribute']=array();
        }
        
        if ($productDetail['attribute']['Color']=='') {
            unset($productDetail['attribute']['Color']);
        }
        if ($productDetail['attribute']['Size']=='') {
            unset($productDetail['attribute']['Size']);
        }
        
        return $productDetail;
    }
    public function getProductFromSession($feature)
    {
        $data=$this->getSessionParameter($feature);
        return $data[$this->locale];
    }
    
    public function removeFromCart($hash, $feature)
    {
        $session_info=$this->getSessionParameter($feature);
        if ($session_info[$this->locale]) {
            if (array_key_exists($hash, $session_info[$this->locale])) {
                unset($session_info[$this->locale][$hash]);
                $this->setSessionParameter($feature, $session_info);
                return true;
            }
        }
    }
    
    public function updateQty($hash, $qty, $feature)
    {
        $data=array();
        $session_info=$this->getSessionParameter($feature);
        if (array_key_exists($hash, $session_info[$this->locale])) {
            $product=$this->getProductInfo($session_info[$this->locale][$hash], $qty);
            $newHash=$this->getHash($product);
            if (array_key_exists($newHash, $session_info[$this->locale])) {
                return false;
            }
            $session_info[$this->locale][$newHash]=$product;
            $data[$newHash]=$product;
            unset($session_info[$this->locale][$hash]);
            $this->setSessionParameter($feature, $session_info);
            return $data;
        }
    }
    
    public function getHash($productDetail)
    {
        $data['slug']=$productDetail['basic_information']['slug'];
        $data['code']=$productDetail['code'];
        $data['minimum_quantity']=$productDetail['minimum_quantity'];
        $data['unit_price']=$productDetail['unit_price'];
        $data['regular_price']=$productDetail['regular_price'];
        $data['attribute']=$productDetail['attribute'];
        return md5(json_encode($data));
    }
    
    public function getProductInfo($product, $qty)
    {
        if (count($product['regular_variations_charges'])>0) {
            foreach ($product['regular_variations_charges'] as $key=>$value) {
                if ($qty>=$value['from'] && $qty<=$value['to']) {
                    $rate=$value['rate'];
                    $code=$value['code'];
                    break;
                } elseif ($value['from']>=$qty && $value['to']=="") {
                    $rate=$value['rate'];
                    $code=$value['code'];
                    break;
                } else {
                    $rate=$value['rate'];
                    $code=$value['code'];
                }
            }
            $product['unit_price']=$rate;
            $product['minimum_quantity']=(int) $qty;
            $product['code']=$code;
        }
        return $product;
    }
}
