<?php
//@*--


class Theme_Designer_Model_Export_Project
{
    private $_name;
    // data
    private $_projectData;
    private $_thumbnails;
    private $_domain;

    private $_fso;
    private $_cfg;

    public function __construct($args)
    {
        list($name, $projectData) = $args;

        $this->_name = $name;
        $this->_projectData = isset($projectData['projectData']) ? $projectData['projectData'] : '';
        $this->_thumbnails = isset($projectData['thumbnails']) ? $projectData['thumbnails'] : array();
        $this->_domain = Mage::app()->getRequest()->getParam('domain', '') ?
            Mage::app()->getRequest()->getParam('domain', '') :
            (isset($projectData['domain']) ? $projectData['domain'] : '');

        if ($this->_projectData && !$this->_domain) {
            throw new Mage_Adminhtml_Exception('Empty domain parameter');
        }
        $this->_fso = Mage::helper('designer/filesystem');
        $this->_cfg = Mage::getSingleton('designer/config');
    }

    public function serializeData()
    {
        return json_encode(array(
            'projectData' => $this->_projectData,
            'domain' => $this->_domain
        ));
    }

    public function getData()
    {
        return $this->_projectData;
    }

    public function getDomain()
    {
        return $this->_domain;
    }

    /**
     * Saves designer user session data
     */
    public function save()
    {
        $saveData = array(
            'projectData' => $this->_projectData,
            'domain' => $this->_domain
        );

        $projectPath = self::getProjectPath($this->_name);
        $this->_fso->write($projectPath, json_encode($saveData));

        $thumbnailPath = MAGENTO_ROOT . $this->_cfg->get('paths/theme/thumbnails', array('theme' => $this->_name));
        foreach ($this->_thumbnails as $thumbnail) {
            $this->_fso->write(
                $thumbnailPath . DS . $thumbnail['name'],
                base64_decode(str_replace('data:image/png;base64,', '', $thumbnail['data']))
            );
        }
    }

    /**
     * Saves designer user session data
     */
    public function saveSession()
    {
        $sessionPath = MAGENTO_ROOT . $this->_cfg->get('paths/theme/session', array('theme' => $this->_name));
        $session = array(
            'user_id' => Mage::getSingleton('admin/session')->getUser()->getUserId(),
            'user_key' => Mage::getSingleton('core/session')->getFormKey()
        );
        $this->_fso->write($sessionPath, json_encode($session));
    }

    /**
     * Loads designer user session data
     * @return array|null
     */
    public function getSession()
    {
        $session = null;
        $sessionPath = MAGENTO_ROOT . $this->_cfg->get('paths/theme/session', array('theme' => $this->_name));
        if (file_exists($sessionPath)) {
            $session = json_decode(file_get_contents($sessionPath), true);
        }
        return $session;
    }

    public function removeSession()
    {
        if (($sessionPath = MAGENTO_ROOT . $this->_cfg->get('paths/theme/session', array('theme' => $this->_name))) && file_exists($sessionPath)) {
            $this->_fso->remove($sessionPath);
        }
    }

    /**
     * @param string $name
     * @return Theme_Designer_Model_Export_Project
     */
    public static function open($name = '')
    {
        if (!$name) {
            throw new Mage_Adminhtml_Exception('Name can\'t be empty');
        }
        $data = array();
        if (self::exists($name)) {
            $data = json_decode(file_get_contents(self::getProjectPath($name)), true);
        }
        return Mage::getModel('designer/export_project', array($name, $data));
    }

    public static function exists($name = '')
    {
        return file_exists(self::getProjectPath($name));
    }

    public static function getProjectPath($themeName)
    {
        return MAGENTO_ROOT . Mage::getSingleton('designer/config')->get('paths/theme/project', array('theme' => $themeName));
    }
}

//--*@