<?php

namespace Lib\Imap;

use Lib\Imap\Request\Folder;
use Lib\Imap\Request\Folders;
use Lib\Imap\Request\Login;
use Lib\Imap\Request\Logout;
use Lib\Imap\Request\Noop;

class Imap {

    /**
     * 配置
     * @var ImapConfig
     */
    public ImapConfig $config;

    /**
     * @var ImapClient
     */
    public ImapClient $client;

    /**
     * 当前在那个文件夹下面目录
     * @var string
     */
    protected string $checkedFolder = '';


    /**
     * Imap constructor.
     * @param ImapConfig $config
     */
    public function __construct(ImapConfig $config)
    {
        $this->config = $config;

        $this->client = new ImapClient($this->config->getHost());
    }

    /**
     * 设置是否调试
     * @param bool $debug
     * @return $this
     * @author:dc
     * @time 2024/9/19 9:41
     */
    public function debug(bool $debug = true):static {
        $this->client->setDebug($debug);
        return $this;
    }

    /**
     * @param string $host
     * @param string $email
     * @param string $password
     * @return Login
     * @throws \Exception
     * @author:dc
     * @time 2024/9/13 17:45
     */
    public function login():Login {
        // 打开连接
        $this->client->open();

        //有的服务器是强制要求设置id属性 已知 163 有的服务器没有id命令  这里就不处理结果了
        $this->client->request('ID ("name" "测试本地 Client" "version" "1" "os" "测试本地" "os-version" "1.0")');

        // 登录
        return (new Login($this))->exec();

    }

    /**
     * 选择文件夹
     * @param string|\Lib\Imap\Parse\Folder\Folder $folder 目录
     * @param bool $utf7 是否是utf7编码
     * @return Folder
     * @author:dc
     * @time 2024/9/19 9:57
     */
    public function folder(string|\Lib\Imap\Parse\Folder\Folder $folder, bool $utf7 = true):Folder {
        if($folder instanceof \Lib\Imap\Parse\Folder\Folder){
            $folder = $folder->folder;
        }else{
            if(!$utf7) $folder = mb_convert_encoding($folder,"UTF7-IMAP","UTF-8");
        }
        return new Folder($this,$folder);
    }

    /**
     * 读取文件夹
     * @return \Lib\Imap\Parse\Folder\Folders
     * @author:dc
     * @time 2024/9/19 14:10
     */
    public function getFolders():\Lib\Imap\Parse\Folder\Folders {
        $folders = new Folders($this);

        return $folders->get();
    }


    /**
     * 类似ping
     * @return bool
     * @author:dc
     * @time 2024/9/20 14:45
     */
    public function noop():bool {
        return (new Noop($this))->exec()->isOk();
    }


    /**
     * 读取或验证是否在当前目录下面
     * @param string $folder
     * @return bool|string
     * @author:dc
     * @time 2024/9/20 13:58
     */
    public function checkedFolder(string $folder = ''):bool|string {
        if($folder){
            return $folder == $this->checkedFolder;
        }
        return $this->checkedFolder;
    }

    /**
     * 设置当前进入那个文件夹
     * @param string $folder
     * @author:dc
     * @time 2024/9/20 14:00
     */
    public function setCheckedFolder(string $folder):void {
        $this->checkedFolder = $folder;
    }




    public function __destruct()
    {
        // 退出登录
        (new Logout($this))->exec();
        unset($this->client);
        // TODO: Implement __destruct() method.
    }


}