<?php


namespace Heanup\App\Api\Controller\Vestacp;


use Heanup\App\Api\Package\Database\Mail;
use Heanup\App\Api\RestfulController;
use Heanup\Library\Vestacp;
use PhpImap\Exceptions\InvalidParameterException;
use PhpImap\Mailbox;
use ProfiCloS\VestaCP\ClientException;
use ProfiCloS\VestaCP\ProcessException;

class Domain extends RestfulController
{
    /**
     * @var $this
     */
    public $_data;
    /**
     * @var $this
     */
    public $email;
    /**
     * @var array
     */
    public $mail_ids;
    /**
     * @var array
     */
    public $info;

    /**
     * Get Method|获取数据
     * @return void
     * @throws InvalidParameterException
     */
    protected function getData()
    {
        $type = $this->getRequest()->string("type", 'default');
        $this->_data = $_SESSION['data'];
        $login = $this->_data['email'] . "@" . $this->_data['domain'];
        $password = $this->_data['password'];
        $this->email = $this->getQuery()->string("email");
//        $this->showSuccess([$login,$password]);
        if ($this->email) {
            $login = $this->email;
            $account = substr($this->email, 0, strpos($this->email, "@"));
            $domain = substr($this->email, strpos($this->email, "@") + 1);
            $_mail = Mail::getOne(['email' => $account, 'domain' => $domain, 'status' => 1]);
            $password = $_mail['password'];
            $this->_data=$_mail;
        }
        if ($type == "get_mail") {

            $mailbox = new Mailbox(
                '{mail.97admin.com:993/imap/ssl/novalidate-cert}INBOX', // IMAP server and mailbox folder
                $login, // Username for the before configured mailbox
                $password, // Password for the before configured username
                __DIR__, // Directory, where attachments will be saved (optional)
                'UTF-8' // Server encoding (optional)
            );
            $mail_ids = $mailbox->searchMailbox('ALL');
            $mail_info = $mailbox->getMailsInfo($mail_ids);
            $mail_info = array_reverse($mail_info);
            $this->showSuccess($mail_info);
        } elseif ($type == 'get_detail') {

            $mailbox = new Mailbox(
                '{mail.97admin.com:993/imap/ssl/novalidate-cert}INBOX', // IMAP server and mailbox folder
                $login, // Username for the before configured mailbox
                $password, // Password for the before configured username
                __DIR__, // Directory, where attachments will be saved (optional)
                'UTF-8' // Server encoding (optional)
            );
            $mail_id = $this->getQuery()->intval("id");
            $detail = $mailbox->getMail($mail_id);
            $this->info = [
                'subject' => $detail->subject,
                'body' => $detail->textHtml,
                'from' => $detail->fromAddress,
                'fromName' => $detail->fromName,
                'replyTo' => $detail->replyTo,
                'date' => $detail->date,
                'cc' => $detail->cc,
                'bcc' => $detail->bcc,
            ];
            $this->getTemplate()->setTpl("Detail");
            $this->render();
            die();

        }

//        $this->showSuccess($this->_data);
        $this->getTemplate()->setTpl("Domain");
        $this->render();
    }

    /**
     * 新增一个临时邮箱
     * @return void
     */
    protected function postData()
    {
        $domain = $this->getRequest()->string("domain", 'a4sky.com');
        $email = $this->getRequest()->string("email");
        $password = md5(uniqid());
        $data = compact("domain", "email", "password");
        $data['expired_time'] = date("Y-m-d H:i:s", time() + 7200);
        $data['add_time'] = date("Y-m-d H:i:s");
        $exist = Mail::getOne(['email' => $email, 'domain' => $domain, 'status' => 1]);
        $this->getTemplate()->setTpl("Domain");
        $this->_data = $_SESSION['data'] = $data;
        if (!$exist) {
            try {
                $result = Vestacp::getInstance()->getModuleMail("admin")->addAccount($domain, $email, $password);
                if ($result) {
                    Mail::insert($data);
                    $this->render();
                }
            } catch (ClientException $e) {
                $this->showError(400, $e->getMessage());
            } catch (ProcessException $e) {
                $this->showError(400, $e->getMessage());
            }
        }
        $this->render();

    }

    /**
     * Put Method|更新数据
     * @return void
     */
    protected function putData()
    {

    }

    /**
     * Delete Method|删除数据
     * @return void
     */
    protected function deleteData()
    {

    }
}