<?php

global $HTTP_RAW_POST_DATA;
//$this->renderJson($this->_noResults);

class Inbox_TestController extends Inbox_Controller_Base
{
    public function init()
    {
        parent::init();
    }

    public function indexAction()
    {
        $sql = "SELECT * from ui_account";

        try
        {
            $accounts = $this->_dbAdapter->fetchAll($sql);
            $this->renderJson($accounts);
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }

        //$this->renderJson($this->_noResults);
        // $params = $this->getRequest()->getParams();
        // $this->renderJson( array('method' => 'index', 'params' => $params) );
        // $this->restserver->handle( array('method' => 'index') );
    }

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

        $params = $this->getRequest()->getParams();

        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()
    {
        $isJson = $this->isJson();
        $params = $this->getRequest()->getParams();

        try
        {
            // $sql = "INSERT INTO wine (name, grapes, country, region, year, description, picture) VALUES (:name, :grapes, :country, :region, :year, :description, :picture)";

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

            // $temp = $GLOBALS['HTTP_RAW_POST_DATA'];
            $inputJSON = file_get_contents('php://input');
            $input = json_decode( $inputJSON, TRUE );

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

            $stmt->bindParam("userId", $this->_getParam('userId',''));
            $stmt->bindParam("serverAddress", $this->_getParam('serverAddress',''));
            $stmt->bindParam("replyAddress", $this->_getParam('replyAddress',''));
            $stmt->bindParam("accountEmail", $this->_getParam('accountEmail',''));
            $stmt->bindParam("accountName", $this->_getParam('accountName',''));
            $stmt->bindParam("signature", $this->_getParam('signature',''));
            $stmt->bindParam("realName", $this->_getParam('realName',''));
            $stmt->bindParam("protocol", $this->_getParam('protocol','IMAP'));
            $stmt->bindParam("password", $this->_getParam('password',''));
            $stmt->bindParam("nickname", $this->_getParam('nickname',''));
            $stmt->bindParam("capacity", intval($this->_getParam('capacity','1')));
            $stmt->bindParam("primary", $this->_getParam('primary',0));
            $stmt->bindParam("quota", $this->_getParam('quota',0));
            $stmt->bindParam("port", $this->_getParam('port',993));

            $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->fetchAll( $select ));

//  'Account'       'test@test.com'
//  'serverAddress' '127.0.0.1'
//  'replyAddress'  'test@test.com'
//  'accountEmail'  'test@test.com'
//  'accountName'   'gmail'
//  'signature'     'signature'
//  'realName'      'test name'
//  'protocol'      'IMAP'
//  'password'      'pass'
//  'nickname'      'testnick'
//  'capacity'      '1',
//  'primary'       '0',
//  'quota'         '0',
//  'port'          '993',
        }
        catch (Exception $e)
        {
            $this->renderJson(array('error' => array('text' => $e->getMessage())));
        }
    }

    public function putAction()
    {
//        $id = $this->_getParam('id', 0);
//        $params = $this->getRequest()->getParams();
//
//        try
//        {
//            $this->renderJson( array('method' => 'put', 'id' => $id, 'params' => $params) );
//        }
//        catch (Exception $e)
//        {
//            $this->renderJson(array('error' => array('text' => $e->getMessage())));
//        }

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

        $params = $this->getRequest()->getParams();

        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 deleteAction()
    {
//        $id = $this->_getParam('id', 0);
//        $params = $this->getRequest()->getParams();
//
//        try
//        {
//            $this->renderJson( array('method' => 'delete', 'id' => $id, 'params' => $params) );
//            $this->renderJson( array('method' => 'delete', 'id' => $id, 'params' => $params) );
//        }
//        catch (Exception $e)
//        {
//            $this->renderJson(array('error' => array('text' => $e->getMessage())));
//        }

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

        $params = $this->getRequest()->getParams();

        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())));
        }
    }
}
