<?php
/**
 *
 * @category	BlueVisionTec
 * @package    BlueVisionTec_GoogleShoppingApi
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @copyright   Copyright (c) 2015 BlueVisionTec UG (haftungsbeschränkt) (http://www.bluevisiontec.de)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * GoogleShopping Admin Items Controller
 *
 * @category	BlueVisionTec
 * @package    BlueVisionTec_GoogleShoppingApi
 * @author      Magento Core Team <core@magentocommerce.com>
 * @author      BlueVisionTec UG (haftungsbeschränkt) <magedev@bluevisiontec.eu>
*/
class BlueVisionTec_GoogleShoppingApi_Adminhtml_GoogleShoppingApi_ItemsController extends Mage_Adminhtml_Controller_Action
{
    /**
     * Initialize general settings for action
     *
     * @return  lueVisionTec_GoogleShoppingApi_Adminhtml_Googleshopping_ItemsController
     */
    protected function _initAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('catalog/googleshoppingapi/items')
            ->_addBreadcrumb(Mage::helper('adminhtml')->__('Catalog'), Mage::helper('adminhtml')->__('Catalog'))
            ->_addBreadcrumb(Mage::helper('adminhtml')->__('Google Content'), Mage::helper('adminhtml')->__('Google Content'));
        return $this;
    }

    /**
     * Manage Items page with two item grids: Magento products and Google Content items
     */
    public function indexAction()
    {
        $this->_title($this->__('Catalog'))
             ->_title($this->__('Google Content'))
             ->_title($this->__('Manage Items'));

        if (0 === (int)$this->getRequest()->getParam('store')) {
            $this->_redirect('*/*/', array('store' => Mage::app()->getAnyStoreView()->getId(), '_current' => true));
            return;
        }
        
        $storeId = $this->_getStoreId();
        
        if($storeId) {
			$service = Mage::getModel('googleshoppingapi/googleShopping');
			$service->getClient($storeId);
        }
        
        $contentBlock = $this->getLayout()->createBlock('googleshoppingapi/adminhtml_items')->setStore($this->_getStore());

        $this->_getLogger()->setStoreId($storeId);
        if (!$this->_getConfig()->isValidDefaultCurrencyCode($this->_getStore()->getId())) {
            $_countryInfo = $this->_getConfig()->getTargetCountryInfo($this->_getStore()->getId());
            $this->_getLogger()->addNotice(
                Mage::helper('googleshoppingapi')->__("The store's currency should be set to %s for %s in system configuration. Otherwise item prices won't be correct in Google Content.", $_countryInfo['currency_name'], $_countryInfo['name'])
            );
        }

        $this->_initAction()
            ->_addBreadcrumb(Mage::helper('googleshoppingapi')->__('Items'), Mage::helper('googleshoppingapi')->__('Items'))
            ->_addContent($contentBlock)
            ->renderLayout();
    }

    /**
     * Grid with Google Content items
     */
    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
            $this->getLayout()
                ->createBlock('googleshoppingapi/adminhtml_items_item')
                ->setIndex($this->getRequest()->getParam('index'))
                ->toHtml()
           );
    }

    /**
     * Retrieve synchronization process mutex
     *
     * @return lueVisionTec_GoogleShoppingApi_Model_Flag
     */
    protected function _getFlag()
    {
        return Mage::getSingleton('googleshoppingapi/flag')->loadSelf();
    }

    /**
     * Add (export) several products to Google Content
     */
    public function massAddAction()
    {
		$storeId = $this->_getStore()->getId();
		
        $flag = $this->_getFlag();
        if ($flag->isLocked()) {
			$this->_getSession()->addError($this->__('Flag locked!'));
			$this->_redirect('*/*/index', array('store'=>$storeId));
			return;
        }

        ignore_user_abort(true);
        set_time_limit(0);
        
        $this->_getLogger()->setStoreId($storeId);

        $productIds = $this->getRequest()->getParam('product', null);

        try {
            $flag->lock();
            Mage::app()->setCurrentStore($storeId);
            Mage::getModel('googleshoppingapi/massOperations')
                ->setFlag($flag)
                ->addProducts($productIds, $storeId);
        } catch (Exception $e) {
            $flag->unlock();
            $this->_getLogger()->addMajor(
                Mage::helper('googleshoppingapi')->__('An error has occured while adding products to google shopping account.'),
                $e->getMessage()
            );
            Mage::logException($e);
            $this->_redirect('*/*/index', array('store'=>$storeId));
            return $this;
        }

        $flag->unlock();
        
        $this->_redirect('*/*/index', array('store'=>$storeId));
        return $this;
    }

    /**
     * Delete products from Google Content
     */
    public function massDeleteAction()
    {
		$storeId = $this->_getStore()->getId();
		
        $flag = $this->_getFlag();
        if ($flag->isLocked()) {
            $this->_getSession()->addError($this->__('Flag locked!'));
			$this->_redirect('*/*/index', array('store'=>$storeId));
			return;
        }
        $this->_getLogger()->setStoreId($storeId);

        ignore_user_abort(true);
        set_time_limit(0);

        $itemIds = $this->getRequest()->getParam('item');
		
        try {
            $flag->lock();
            Mage::app()->setCurrentStore($storeId);
            Mage::getModel('googleshoppingapi/massOperations')
                ->setFlag($flag)
                ->deleteItems($itemIds);
        } catch (Exception $e) {
            $flag->unlock();
            $this->_getLogger()->addMajor(
                Mage::helper('googleshoppingapi')->__('An error has occured while deleting products from google shopping account.'),
                Mage::helper('googleshoppingapi')->__('One or more products were not deleted from google shopping account. Refer to the log file for details.')
            );
            Mage::logException($e);
            $this->_redirect('*/*/index', array('store'=>$storeId));
            return $this;
        }
		$this->_redirect('*/*/index', array('store'=>$storeId));
        $flag->unlock();
        return $this;
        
    }

    /**
     * Update items statistics and remove the items which are not available in Google Content
     */
    public function refreshAction()
    {
    
        $startTime = time();
		$storeId = $this->_getStore()->getId();
		
        $flag = $this->_getFlag();

        if ($flag->isLocked()) {
			$this->_getSession()->addError($this->__('Flag locked!'));
			$this->_redirect('*/*/index', array('store'=>$storeId));
			return;
        }
        $this->_getLogger()->setStoreId($storeId);

        ignore_user_abort(true);
        set_time_limit(0);

        $itemIds = $this->getRequest()->getParam('item');

        try {
            $flag->lock();
            Mage::app()->setCurrentStore($storeId);
            Mage::getModel('googleshoppingapi/massOperations')
                ->setFlag($flag)
                ->batchSynchronizeItems($itemIds);
        } catch (Exception $e) {
            $flag->unlock();
            $this->_getLogger()->addMajor(
                Mage::helper('googleshoppingapi')->__('An error has occured while deleting products from google shopping account.'),
                Mage::helper('googleshoppingapi')->__('One or more products were not deleted from google shopping account. Refer to the log file for details.')
            );
            Mage::logException($e);
            Mage::log($e->getMessage());
            return;
        }
        $flag->unlock();
        $duration = time() - $startTime;
        //Mage::log($duration."s");
        $this->_redirect('*/*/index', array('store'=>$storeId));
        return $this;
    }

    /**
     * Retrieve background process status
     *
     * @return Zend_Controller_Response_Abstract
     */
    public function statusAction()
    {
        if ($this->getRequest()->isAjax()) {
            $this->getResponse()->setHeader('Content-Type', 'application/json');
            $params = array(
                'is_running' => $this->_getFlag()->isLocked()
            );
            return $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($params));
        }
    }

    /**
     * Get store object, basing on request
     *
     * @return Mage_Core_Model_Store
     * @throws Mage_Core_Exception
     */
    public function _getStore()
    {
        $store = Mage::app()->getStore((int)$this->getRequest()->getParam('store', 0));
        if ((!$store) || 0 == $store->getId()) {
            Mage::throwException($this->__('Unable to select a Store View.'));
        }
        return $store;
    }
    
    public function _getStoreId()
    {
        $store = Mage::app()->getStore((int)$this->getRequest()->getParam('store', 0));
        if($store && $store->getId()) {
			return $store->getId();
        }
        return null;
    }

    /**
     * Get Google Shopping config model
     *
     * @return lueVisionTec_GoogleShoppingApi_Model_Config
     */
    protected function _getConfig()
    {
        return Mage::getSingleton('googleshoppingapi/config');
    }

    /**
     * Check access to this controller
     *
     * @return bool
     */
    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('catalog/googleshoppingapi/items');
    }
    
    /**
     * Retrieve logger
     *
     * @return BlueVisionTec_GoogleShoppingApi_Model_Log
     */
    protected function _getLogger()
    {
        return Mage::getSingleton('googleshoppingapi/log');
    }
}
