<?php

class Inbox_AccountController extends Inbox_Controller_Base
{
    protected $userId;
    protected $serverAddress;
    protected $replyAddress;
    protected $accountEmail;
    protected $accountName;
    protected $signature;
    protected $realName;
    protected $protocol;
    protected $password;
    protected $nickname;
    protected $capacity;
    protected $primary;
    protected $quota;
    protected $port;
    protected $unreadMail;

    public function init()
    {
        parent::init();
    }

    protected function populateParameters()
    {
        if ($this->isJson())
        {
            $inputJson = file_get_contents('php://input');
            $input = json_decode( $inputJson, TRUE );

            $this->userId =        $this->getJsonParam($input, 'userId', '');
            $this->serverAddress = $this->getJsonParam($input, 'serverAddress','');
            $this->replyAddress =  $this->getJsonParam($input, 'replyAddress','');
            $this->accountEmail =  $this->getJsonParam($input, 'accountEmail','');
            $this->accountName =   $this->getJsonParam($input, 'accountName','');
            $this->signature =     $this->getJsonParam($input, 'signature','');
            $this->realName =      $this->getJsonParam($input, 'realName','');
            $this->protocol =      $this->getJsonParam($input, 'protocol','IMAP');
            $this->password =      $this->getJsonParam($input, 'password','');
            $this->nickname =      $this->getJsonParam($input, 'nickname','');
            $this->capacity =      $this->getJsonParam($input, 'capacity','1');
            $this->primary =       $this->getJsonParam($input, 'primary',0);
            $this->quota =         $this->getJsonParam($input, 'quota',0);
            $this->port =          $this->getJsonParam($input, 'port', '993');
            $this->unreadMail =    $this->getJsonParam($input, 'unreadMail',0);
        }
        else
        {
            $this->userId =        $this->_getParam('userId','');
            $this->serverAddress = $this->_getParam('serverAddress','');
            $this->replyAddress =  $this->_getParam('replyAddress','');
            $this->accountEmail =  $this->_getParam('accountEmail','');
            $this->accountName =   $this->_getParam('accountName','');
            $this->signature =     $this->_getParam('signature','');
            $this->realName =      $this->_getParam('realName','');
            $this->protocol =      $this->_getParam('protocol','IMAP');
            $this->password =      $this->_getParam('password','');
            $this->nickname =      $this->_getParam('nickname','');
            $this->capacity =      $this->_getParam('capacity','1');
            $this->primary =       $this->_getParam('primary',0);
            $this->quota =         $this->_getParam('quota',0);
            $this->port =          $this->_getParam('port', '993');
            $this->unreadMail =    $this->_getParam('unreadMail',0);
        }
    }

    public function indexAction()
    {
        try
        {
            $userId = $this->_getParam('userid');

            if (null === $userId)
            {
                $sql = 'SELECT * from ui_account';
                $accounts = $this->_dbAdapter->fetchAll($sql);
                $this->renderJson($accounts);
            }
            else
            {
                $sql = 'SELECT * from ui_account where userId=:userId';
                $stmt = $this->_dbAdapter->prepare($sql);
                $stmt->bindParam('userId', $userId);
                $stmt->execute();
                $result = $stmt->fetchAll();
                $this->renderJson($result);
            }
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }

    }

    public function getAction()
    {
        $id = $this->_getParam('id', 0);
        $sql = 'SELECT * from ui_account where id=:id';

        try
        {
            $stmt = $this->_dbAdapter->prepare($sql);
            $stmt->bindParam('id', $id);
            $stmt->execute();
            $result = $stmt->fetchObject();
            $this->renderJson($result);
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }

    public function postAction()
    {
        try
        {
            $sql = <<<EOD
INSERT INTO ui_account (
`userId`,
`serverAddress`,
`replyAddress`,
`accountEmail`,
`accountName`,
`signature`,
`realName`,
`protocol`,
`password`,
`nickname`,
`capacity`,
`primary`,
`quota`,
`port`,
`unreadMail`
) VALUES (
:userId,
:serverAddress,
:replyAddress,
:accountEmail,
:accountName,
:signature,
:realName,
:protocol,
:password,
:nickname,
:capacity,
:primary,
:quota,
:port,
:unreadMail
);
EOD;

            $this->populateParameters();

            $stmt = $this->_dbAdapter->prepare($sql);

            $stmt->bindParam('userId',        $this->userId);
            $stmt->bindParam('serverAddress', $this->serverAddress);
            $stmt->bindParam('replyAddress',  $this->replyAddress);
            $stmt->bindParam('accountEmail',  $this->accountEmail);
            $stmt->bindParam('accountName',   $this->accountName);
            $stmt->bindParam('signature',     $this->signature);
            $stmt->bindParam('realName',      $this->realName);
            $stmt->bindParam('protocol',      $this->protocol);
            $stmt->bindParam('password',      $this->password);
            $stmt->bindParam('nickname',      $this->nickname);
            $stmt->bindParam('capacity',      $this->capacity);
            $stmt->bindParam('primary',       $this->primary);
            $stmt->bindParam('quota',         $this->quota);
            $stmt->bindParam('port',          $this->port);
            $stmt->bindParam('unreadMail',    $this->unreadMail);

            $success = $stmt->execute();
            $id = $this->_dbAdapter->lastInsertId();

            $select = $this->_dbAdapter
                ->select()
                ->from('ui_account')
                ->where('id = ' . $this->_dbAdapter->quote($id, 'INTEGER'));

            $this->renderJson($this->_dbAdapter->fetchRow($select));
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }

    public function putAction()
    {
        $id = $this->_getParam('id', 0);

        try
        {
            $sql = <<<EOD
UPDATE ui_account SET
`userId`        = :userId,
`serverAddress` = :serverAddress,
`replyAddress`  = :replyAddress,
`accountEmail`  = :accountEmail,
`accountName`   = :accountName,
`signature`     = :signature,
`realName`      = :realName,
`protocol`      = :protocol,
`password`      = :password,
`nickname`      = :nickname,
`capacity`      = :capacity,
`primary`       = :primary,
`quota`         = :quota,
`port`          = :port,
`unreadMail`    = :unreadMail
WHERE id=:id
EOD;

            $this->populateParameters();

            $stmt = $this->_dbAdapter->prepare($sql);

            $stmt->bindParam('userId',        $this->userId);
            $stmt->bindParam('serverAddress', $this->serverAddress);
            $stmt->bindParam('replyAddress',  $this->replyAddress);
            $stmt->bindParam('accountEmail',  $this->accountEmail);
            $stmt->bindParam('accountName',   $this->accountName);
            $stmt->bindParam('signature',     $this->signature);
            $stmt->bindParam('realName',      $this->realName);
            $stmt->bindParam('protocol',      $this->protocol);
            $stmt->bindParam('password',      $this->password);
            $stmt->bindParam('nickname',      $this->nickname);
            $stmt->bindParam('capacity',      $this->capacity);
            $stmt->bindParam('primary',       $this->primary);
            $stmt->bindParam('quota',         $this->quota);
            $stmt->bindParam('port',          $this->port);
            $stmt->bindParam('unreadMail',    $this->unreadMail);

            $stmt->bindParam('id',            $id);

            $success = $stmt->execute();

            $select = $this->_dbAdapter
                ->select()
                ->from('ui_account')
                ->where('id = ' . $this->_dbAdapter->quote($id, 'INTEGER'));

            $this->renderJson($this->_dbAdapter->fetchRow($select));
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }

    public function deleteAction()
    {
        $id = $this->_getParam('id', 0);

        try
        {
            $sql = 'DELETE FROM ui_account WHERE id=:id';

            $stmt = $this->_dbAdapter->prepare($sql);
            $stmt->bindParam('id', $id);
            $success = $stmt->execute();
            $this->renderJson($this->_statusOK);
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }
}
