<?php

require_once 'application/models/inbox.php';

class Api_ConnectionController extends Atmail_Controller_Base
{
    public function init()
    {
        //TODO: extend authentication to support multiple user levels/roles/groups
        require_once 'application/models/api.php';
        $this->api = new api();

        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->appBaseUrl = $this->_request->getBaseUrl() . (strpos($this->_request->getBaseUrl(), 'index.php') ? '' : '/index.php');
        $this->view->siteBaseUrl = Zend_Registry::get('siteBaseUrl');
        $this->view->moduleBaseUrl = $this->view->appBaseUrl . ($this->_request->module == 'default' ? '' : DIRECTORY_SEPARATOR . $this->_request->module);

        $this->_helper->viewRenderer->setNoRender();

        // Load all the params
        $this->requestParams = $this->getRequest()->getParams();

        // Use Cookie auth
        if (!Atmail_FormAuth::authenticated())
        {
            return $this->_forward('timeout', 'mail', 'api', $this->requestParams);
        }

        $this->userData = Zend_Auth::getInstance()->getStorage()->read();
        $this->userData = Atmail_Password::processUser($this->userData);

        // Load the session
        $this->session = new Zend_Session_Namespace(ATMAIL_NAMESPACE);

        // Load the contacts class
        $this->_contacts = new contacts(array('Account' => $this->userData['Account']));

        // Find the users settings ( used for theme )
        $this->view->UserSettings = Zend_Registry::get('UserSettings');

        // Used to determine the contact in the global addressbook
        $this->view->Account = $this->userData['Account'];

        // Load the system settings
        $this->_globalConfig = Zend_Registry::get('config')->global;
    }

    public function testconnectionAction()
    {
        $params = $this->getRequest()->getParams();

        $name = $params['name'];
        $email = $params['email'];
        $password = $params['password'];
        $url = $params['url'];
        $protocol = $params['protocol'];

        try
        {
            $inbox = new inbox();
            $success = $inbox->testConnection($url, $protocol, $email, $password);

            $this->renderJson(array(
                'connected' => $success ? 1 : 0
//                'connected' => $success ? 'true' : 'false'
            ));
        }
        catch (Atmail_Exception $e)
        {
            $this->renderFail($e);
        }
    }

    public function updateAction()
    {
        $params = $this->getRequest()->getParams();

        try
        {
            $this->renderJson($params);
        }
        catch (Atmail_Exception $e)
        {
            $this->renderFail($e);
        }
    }

    public function indexAction()
    {
        $params = $this->getRequest()->getParams();

        try
        {
            $this->renderJson($params);
        }
        catch (Atmail_Exception $e)
        {
            $this->renderFail($e);
        }
    }

    public function createAction()
    {
        $params = $this->getRequest()->getParams();

        try
        {
            $this->renderJson($params);
        }
        catch (Atmail_Exception $e)
        {
            $this->renderFail($e);
        }
    }

    public function listAction()
    {
        $params = $this->getRequest()->getParams();

        try
        {
            $this->renderJson($params);
        }
        catch (Atmail_Exception $e)
        {
            $this->renderFail($e);
        }
    }

    public function deleteAction()
    {
        $params = $this->getRequest()->getParams();

        try
        {
            $this->renderJson($params);
        }
        catch (Atmail_Exception $e)
        {
            $this->renderFail($e);
        }
    }

    public function deletefromgroupAction()
    {
        $params = $this->getRequest()->getParams();

        try
        {
            $this->renderJson($params);
        }
        catch (Atmail_Exception $e)
        {
            $this->renderFail($e);
        }
    }

    public function viewAction()
    {
        $params = $this->getRequest()->getParams();

        try
        {
            $this->renderJson($params);
        }
        catch (Atmail_Exception $e)
        {
            $this->renderFail($e);
        }
    }

    public function addtogroupAction()
    {
        $params = $this->getRequest()->getParams();

        try
        {
            $this->renderJson($params);
        }
        catch (Atmail_Exception $e)
        {
            $this->renderFail($e);
        }
    }
}
